Skip to content

Commit

Permalink
Create LoginData model to replace List<Pair>
Browse files Browse the repository at this point in the history
  • Loading branch information
Cryptically committed Aug 27, 2016
1 parent 9322dc4 commit 7acc0c0
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 42 deletions.
32 changes: 15 additions & 17 deletions src/me/corriekay/pokegoutil/DATA/managers/AccountManager.java
Expand Up @@ -9,12 +9,11 @@
import com.pokegoapi.exceptions.RemoteServerException; import com.pokegoapi.exceptions.RemoteServerException;
import javafx.scene.control.Alert; import javafx.scene.control.Alert;
import javafx.util.Pair; import javafx.util.Pair;
import me.corriekay.pokegoutil.DATA.models.LoginData;
import me.corriekay.pokegoutil.utils.ConfigKey; import me.corriekay.pokegoutil.utils.ConfigKey;
import me.corriekay.pokegoutil.utils.ConfigNew; import me.corriekay.pokegoutil.utils.ConfigNew;
import okhttp3.OkHttpClient; import okhttp3.OkHttpClient;


import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;


/*this controller does the login/log off, and different account information (aka player data) /*this controller does the login/log off, and different account information (aka player data)
Expand Down Expand Up @@ -172,26 +171,25 @@ public static void alertFailedLogin(String message) {
alert.showAndWait(); alert.showAndWait();
} }


public static List<Pair> getLoginData(LoginType type) { public static LoginData getLoginData(LoginType type) {
LoginData loginData = new LoginData();

switch (type) { switch (type) {
case GOOGLE: case GOOGLE:
String token = config.getString(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN); loginData.setToken(config.getString(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN));
return (token != null) ? Collections.singletonList( break;
new Pair<>("token", token)) : null;
case PTC: case PTC:
String username = config.getString(ConfigKey.LOGIN_PTC_USERNAME); loginData.setUsername(config.getString(ConfigKey.LOGIN_PTC_USERNAME));
String password = config.getString(ConfigKey.LOGIN_PTC_PASSWORD); loginData.setPassword(config.getString(ConfigKey.LOGIN_PTC_PASSWORD));
return (username != null && password != null) ? Arrays.asList( break;
new Pair<>("username", username), new Pair<>("password", password)) : null;
case BOTH: case BOTH:
String token2 = config.getString(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN); loginData.setToken(config.getString(ConfigKey.LOGIN_GOOGLE_AUTH_TOKEN));
String username2 = config.getString(ConfigKey.LOGIN_PTC_USERNAME); loginData.setUsername(config.getString(ConfigKey.LOGIN_PTC_USERNAME));
String password2 = config.getString(ConfigKey.LOGIN_PTC_PASSWORD); loginData.setPassword(config.getString(ConfigKey.LOGIN_PTC_PASSWORD));
return (username2 != null && password2 != null && token2 != null) ? Arrays.asList( break;
new Pair<>("username", username2), new Pair<>("password", password2), new Pair<>("token", token2)) : null;
default: default:
return null; }
} return loginData;
} }


private static void deleteLoginData(LoginType type) { private static void deleteLoginData(LoginType type) {
Expand Down
43 changes: 43 additions & 0 deletions src/me/corriekay/pokegoutil/DATA/models/LoginData.java
@@ -0,0 +1,43 @@
package me.corriekay.pokegoutil.DATA.models;

public class LoginData {

private String token;
private String username;
private String password;

public String getPassword() {
return password;
}

public String getToken() {
return token;
}

public String getUsername() {
return username;
}

public void setPassword(String password) {
this.password = password;
}

public void setToken(String token) {
this.token = token;
}

public void setUsername(String username) {
this.username = username;
}

public boolean hasUsername(){
return username != null;
}

public boolean hasPassword(){
return password != null;
}
public boolean hasToken(){
return token != null;
}
}
57 changes: 32 additions & 25 deletions src/me/corriekay/pokegoutil/GUI/controller/LoginController.java
Expand Up @@ -17,6 +17,7 @@
import me.corriekay.pokegoutil.BlossomsPoGoManager; import me.corriekay.pokegoutil.BlossomsPoGoManager;
import me.corriekay.pokegoutil.DATA.managers.AccountManager; import me.corriekay.pokegoutil.DATA.managers.AccountManager;
import me.corriekay.pokegoutil.DATA.managers.AccountManager.LoginType; import me.corriekay.pokegoutil.DATA.managers.AccountManager.LoginType;
import me.corriekay.pokegoutil.DATA.models.LoginData;
import me.corriekay.pokegoutil.utils.helpers.Browser; import me.corriekay.pokegoutil.utils.helpers.Browser;


import java.io.IOException; import java.io.IOException;
Expand Down Expand Up @@ -76,31 +77,34 @@ public LoginController() {
@FXML @FXML
private void initialize() { private void initialize() {
AccountManager.initialize(); AccountManager.initialize();
boolean saveCredentials = AccountManager.checkForSavedCredentials();
googleAuthBtn.setOnAction(this::onGoogleAuthBtnClicked); googleAuthBtn.setOnAction(this::onGoogleAuthBtnClicked);
ptcLoginBtn.setOnAction(this::onPTCLoginBtnClicked); ptcLoginBtn.setOnAction(this::onPTCLoginBtnClicked);
saveAuthChkbx.setSelected(saveCredentials);
saveAuthChkbx.setOnAction(this::onAutoRelogChanged); saveAuthChkbx.setOnAction(this::onAutoRelogChanged);
getTokenBtn.setOnAction(this::onGetToken); getTokenBtn.setOnAction(this::onGetToken);
if(saveCredentials){
boolean saveCredentials = AccountManager.checkForSavedCredentials();
saveAuthChkbx.setSelected(saveCredentials);

if (saveCredentials) {
LoginType loginType = AccountManager.checkSavedConfig(); LoginType loginType = AccountManager.checkSavedConfig();
List<Pair> loginData = AccountManager.getLoginData(loginType); LoginData loginData = AccountManager.getLoginData(loginType);
if (loginData != null && !loginData.isEmpty()) {
loginData.forEach(pair -> { if(loginData.hasUsername()){
switch (pair.getKey().toString()) { usernameField.setText(loginData.getUsername());
case "username": usernameField.setDisable(true);
usernameField.setText(pair.getValue().toString()); }
usernameField.setDisable(true);
case "password": if(loginData.hasPassword()){
passwordField.setText(pair.getValue().toString()); passwordField.setText(loginData.getPassword());
passwordField.setDisable(true); passwordField.setDisable(true);
case "token":
tokenField.setText("Using Previous Token");
tokenField.setDisable(true);
getTokenBtn.setDisable(true);
}
});
} }

if(loginData.hasToken()){
tokenField.setText("Using Previous Token");
tokenField.setDisable(true);
getTokenBtn.setDisable(true);
}
} }
} }


Expand All @@ -110,23 +114,23 @@ private void onGetToken(ActionEvent actionEvent) {
} }


private void onAutoRelogChanged(ActionEvent actionEvent) { private void onAutoRelogChanged(ActionEvent actionEvent) {
boolean saveCredentials = ((CheckBox)actionEvent.getSource()).isSelected(); boolean saveCredentials = ((CheckBox) actionEvent.getSource()).isSelected();
AccountManager.setSaveLogin(saveCredentials); AccountManager.setSaveLogin(saveCredentials);
toggleFields(saveCredentials); toggleFields(saveCredentials);
} }


private void toggleFields(boolean save){ private void toggleFields(boolean save) {
if(usernameField.getText().isEmpty() || !save) if (usernameField.getText().isEmpty() || !save)
usernameField.setDisable(false); usernameField.setDisable(false);
else else
usernameField.setDisable(true); usernameField.setDisable(true);


if(passwordField.getText().isEmpty() || !save) if (passwordField.getText().isEmpty() || !save)
passwordField.setDisable(false); passwordField.setDisable(false);
else else
passwordField.setDisable(true); passwordField.setDisable(true);


if(tokenField.getText().isEmpty() || !save) if (tokenField.getText().isEmpty() || !save)
tokenField.setDisable(false); tokenField.setDisable(false);
else else
tokenField.setDisable(true); tokenField.setDisable(true);
Expand All @@ -137,7 +141,10 @@ private void toggleFields(boolean save){
@FXML @FXML
void onGoogleAuthBtnClicked(ActionEvent event) { void onGoogleAuthBtnClicked(ActionEvent event) {
try { try {
AccountManager.login(Collections.singletonList(new Pair<>("token", tokenField.getText())), LoginType.GOOGLE); AccountManager.login(
Collections.singletonList(
new Pair<>("token", tokenField.getText())),
LoginType.GOOGLE);
} catch (Exception e) { } catch (Exception e) {
AccountManager.alertFailedLogin(e.toString()); AccountManager.alertFailedLogin(e.toString());
} }
Expand Down

0 comments on commit 7acc0c0

Please sign in to comment.