Skip to content

Commit

Permalink
Changing to use NewConfig - Phase 4
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryptically committed Aug 14, 2016
1 parent 6a0a174 commit 91bc3df
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/me/corriekay/pokegoutil/BlossomsPoGoManager.java
Expand Up @@ -9,6 +9,8 @@
import javafx.stage.StageStyle;
import me.corriekay.pokegoutil.DATA.controllers.AccountController;
import me.corriekay.pokegoutil.utils.Config;
import me.corriekay.pokegoutil.utils.ConfigKey;
import me.corriekay.pokegoutil.utils.ConfigNew;
import me.corriekay.pokegoutil.utils.ui.Console;
import me.corriekay.pokegoutil.utils.helpers.UIHelper;

Expand All @@ -28,7 +30,7 @@ public static void main(String[] args) {

@Override
public void start(Stage primaryStage) {
if (Config.getConfig().getBool("develop", false))
if (ConfigNew.getConfig().getBool(ConfigKey.DEVELOPFLAG))
openGUIChooser(primaryStage);
else
openOldGui();
Expand Down
Expand Up @@ -7,6 +7,8 @@
import com.pokegoapi.auth.PtcCredentialProvider;
import me.corriekay.pokegoutil.utils.helpers.Browser;
import me.corriekay.pokegoutil.utils.Config;
import me.corriekay.pokegoutil.utils.ConfigKey;
import me.corriekay.pokegoutil.utils.ConfigNew;
import okhttp3.OkHttpClient;

import javax.swing.*;
Expand All @@ -30,7 +32,7 @@ public final class AccountControllerNew {
protected OkHttpClient http;
protected CredentialProvider cp;

private static Config config = Config.getConfig();
private static ConfigNew config = ConfigNew.getConfig();

private AccountControllerNew() {

Expand All @@ -50,16 +52,16 @@ public static void logOnPTC(String username, String password) {
S_INSTANCE.http = new OkHttpClient();
while (!S_INSTANCE.logged) {
//Using PTC, remove Google infos
config.delete("login.GoogleAuthToken");
config.delete(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN);
try {
S_INSTANCE.cp = new PtcCredentialProvider(S_INSTANCE.http, username, password);
config.setString("login.PTCUsername", username);
if (config.getBool("login.SaveAuth", false) || checkSaveAuth()) {
config.setString("login.PTCPassword", password);
config.setBool("login.SaveAuth", true);
config.setString(ConfigKey.LOGIN_PTC_USERNAME, username);
if (config.getBool(ConfigKey.LOGIN_SAVE_AUTH) || checkSaveAuth()) {
config.setString(ConfigKey.LOGIN_PTC_PASSWORD, password);
config.setBool(ConfigKey.LOGIN_SAVE_AUTH, true);
} else {
config.delete("login.PTCPassword");
config.delete("login.SaveAuth");
config.delete(ConfigKey.LOGIN_PTC_PASSWORD);
config.delete(ConfigKey.LOGIN_SAVE_AUTH);
}
} catch (Exception e) {
alertFailedLogin(e.getMessage());
Expand All @@ -74,9 +76,9 @@ public static void logOnGoogleAuth() {
S_INSTANCE.http = new OkHttpClient();
while (!S_INSTANCE.logged) {
//Using Google, remove PTC infos
config.delete("login.PTCUsername");
config.delete("login.PTCPassword");
String authCode = config.getString("login.GoogleAuthToken", null);
config.delete(ConfigKey.LOGIN_PTC_USERNAME);
config.delete(ConfigKey.LOGIN_PTC_PASSWORD);
String authCode = config.getString(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN);
boolean refresh = false;
if (authCode == null) {
//We need to get the auth code, as we do not have it yet.
Expand Down Expand Up @@ -107,13 +109,13 @@ public static void logOnGoogleAuth() {
if (refresh) provider.refreshToken(authCode);
else provider.login(authCode);
S_INSTANCE.cp = provider;
if (config.getBool("login.SaveAuth", false) || checkSaveAuth()) {
if (config.getBool(ConfigKey.LOGIN_SAVE_AUTH) || checkSaveAuth()) {
if (!refresh)
config.setString("login.GoogleAuthToken", provider.getRefreshToken());
config.setBool("login.SaveAuth", true);
config.setString(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN, provider.getRefreshToken());
config.setBool(ConfigKey.LOGIN_SAVE_AUTH, true);
} else {
config.delete("login.GoogleAuthToken");
config.delete("login.SaveAuth");
config.delete(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN);
config.delete(ConfigKey.LOGIN_SAVE_AUTH);
}
} catch (Exception e) {
alertFailedLogin(e.getMessage());
Expand All @@ -137,7 +139,7 @@ private static boolean checkSaveAuth() {
}

private static LoginType checkSavedConfig() {
if (!config.getBool("login.SaveAuth", false)) {
if (!config.getBool(ConfigKey.LOGIN_SAVE_AUTH)) {
return LoginType.NONE;
} else {
if (getLoginData(LoginType.GOOGLE) != null) return LoginType.GOOGLE;
Expand All @@ -149,11 +151,11 @@ private static LoginType checkSavedConfig() {
private static List<String> getLoginData(LoginType type) {
switch (type) {
case GOOGLE:
String token = config.getString("login.GoogleAuthToken", null);
String token = config.getString(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN);
return (token != null) ? Collections.singletonList(token) : null;
case PTC:
String username = config.getString("login.PTCUsername", null);
String password = config.getString("login.PTCPassword", null);
String username = config.getString(ConfigKey.LOGIN_PTC_USERNAME);
String password = config.getString(ConfigKey.LOGIN_PTC_PASSWORD);
return (username != null && password != null) ? Arrays.asList(username, password) : null;
default:
return null;
Expand All @@ -166,17 +168,17 @@ private static void deleteLoginData(LoginType type) {
}

private static void deleteLoginData(LoginType type, boolean justCleanup) {
if (!justCleanup) config.delete("login.SaveAuth");
if (!justCleanup) config.delete(ConfigKey.LOGIN_SAVE_AUTH);
switch (type) {
case BOTH:
config.delete("login.GoogleAuthToken");
config.delete("login.PTCUsername");
config.delete("login.PTCPassword");
config.delete(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN);
config.delete(ConfigKey.LOGIN_PTC_USERNAME);
config.delete(ConfigKey.LOGIN_PTC_PASSWORD);
case GOOGLE:
config.delete("login.GoogleAuthToken");
config.delete(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN);
case PTC:
config.delete("login.PTCUsername");
config.delete("login.PTCPassword");
config.delete(ConfigKey.LOGIN_PTC_USERNAME);
config.delete(ConfigKey.LOGIN_PTC_PASSWORD);
default:
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/me/corriekay/pokegoutil/utils/ConfigKey.java
@@ -1,6 +1,14 @@
package me.corriekay.pokegoutil.utils;

public enum ConfigKey {

DEVELOPFLAG("develop", false, Boolean.class),

LOGIN_SAVE_AUTH("login.SaveAuth", false, Boolean.class),
LOGIN_GOOGLE_AUTH_TOKEN("login.GoogleAuthToken", null, String.class),
LOGIN_PTC_USERNAME("login.PTCUsername", null, String.class),
LOGIN_PTC_PASSWORD("login.PTCPassword", null, String.class),

WINDOW_WIDTH("options.window.width", 800, Integer.class),
WINDOW_HEIGHT("options.window.height", 650, Integer.class),
WINDOW_POS_X("options.window.posx", 0, Integer.class),
Expand Down
4 changes: 3 additions & 1 deletion src/me/corriekay/pokegoutil/utils/pokemon/PokeHandler.java
Expand Up @@ -8,6 +8,8 @@
import com.pokegoapi.exceptions.RemoteServerException;
import com.pokegoapi.util.PokeNames;
import me.corriekay.pokegoutil.utils.Config;
import me.corriekay.pokegoutil.utils.ConfigKey;
import me.corriekay.pokegoutil.utils.ConfigNew;
import me.corriekay.pokegoutil.utils.Utilities;
import org.apache.commons.lang3.StringUtils;

Expand Down Expand Up @@ -114,7 +116,7 @@ private static NicknamePokemonResponse.Result renWPattern(String pattern, Pokemo
*/
public static String getLocalPokeName(int id) {
// TODO: change call to getConfigItem to config class once implemented
String lang = Config.getConfig().getString("options.lang", "en");
String lang = ConfigNew.getConfig().getString(ConfigKey.LANGUAGE);

Locale locale;
String[] langar = lang.split("_");
Expand Down

0 comments on commit 91bc3df

Please sign in to comment.