Login stabilized

This commit is contained in:
Caesar2011
2018-07-19 10:20:23 +02:00
parent c0ad3df829
commit 8fe4899571
3 changed files with 11 additions and 22 deletions

View File

@@ -227,19 +227,15 @@ public class MainActivity extends AppCompatActivity
private void checkAndDoLogin() { private void checkAndDoLogin() {
changeFragment(FRAGMENT_STARTUP); changeFragment(FRAGMENT_STARTUP);
this.getKVV().startUpdate();
this.getGoogleAuth().connect(() -> getGoogleAuth().getLoginState(credentials -> { this.getGoogleAuth().connect(() -> getGoogleAuth().getLoginState(credentials -> {
if (credentials == null || credentials.getUsername() == null || credentials.getPassword() == null) { if (credentials == null || credentials.getUsername() == null || credentials.getPassword() == null) {
this.getKVV().endUpdate();
toLogoutState(); toLogoutState();
return; return;
} }
this.getKVV().login(credentials.getUsername(), credentials.getPassword(), success -> { this.getKVV().login(credentials.getUsername(), credentials.getPassword(), success -> {
this.getKVV().endUpdate();
toLoginState(success, getDefaultFragmentAfterLogin(), ""); toLoginState(success, getDefaultFragmentAfterLogin(), "");
}, error -> { }, error -> {
log.e(error); log.e(error);
this.getKVV().endUpdate();
toLogoutState(); toLogoutState();
}); });
})); }));

View File

@@ -10,7 +10,6 @@ import java.util.HashMap;
import de.sebse.fuplanner.services.KVV.types.LoginToken; import de.sebse.fuplanner.services.KVV.types.LoginToken;
import de.sebse.fuplanner.services.KVV.types.Modules; import de.sebse.fuplanner.services.KVV.types.Modules;
import de.sebse.fuplanner.tools.logging.Logger;
import de.sebse.fuplanner.tools.network.NetworkCallback; import de.sebse.fuplanner.tools.network.NetworkCallback;
import de.sebse.fuplanner.tools.network.NetworkErrorCallback; import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
@@ -21,20 +20,19 @@ import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
public class KVV { public class KVV {
private Context context; private Context context;
private LoginToken lastToken; private LoginToken lastToken;
private boolean isUpdating; private boolean isLoginPending = true;
private ArrayList<LastTokenCallback> updatingList; private ArrayList<LastTokenCallback> updatingList;
private HashMap<String, Object> addons = new HashMap<>(); private HashMap<String, Object> addons = new HashMap<>();
private Logger log = new Logger(this);
public KVV(Context context) { public KVV(Context context) {
this.context = context; this.context = context;
this.isUpdating = false;
this.updatingList = new ArrayList<>(); this.updatingList = new ArrayList<>();
} }
public LoginToken easyLogin() { public LoginToken easyLogin() {
KVVLogin login = new KVVLogin(this.context); KVVLogin login = new KVVLogin(this.context);
lastToken = login.easyLogin(); lastToken = login.easyLogin();
this.endUpdate();
return lastToken; return lastToken;
} }
@@ -42,13 +40,17 @@ public class KVV {
KVVLogin login = new KVVLogin(this.context); KVVLogin login = new KVVLogin(this.context);
login.login(username, password, success -> { login.login(username, password, success -> {
lastToken = success; lastToken = success;
this.endUpdate();
try { try {
login.saveOffline(this.context); login.saveOffline(this.context);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
callback.onResponse(success); callback.onResponse(success);
}, error); }, error1 -> {
this.endUpdate();
error.onError(error1);
});
} }
public void logout() { public void logout() {
@@ -61,6 +63,7 @@ public class KVV {
modules.deleteModulesOffline(this.context); modules.deleteModulesOffline(this.context);
} }
addons.clear(); addons.clear();
this.isLoginPending = true;
} }
public void getModule(String id, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error) { public void getModule(String id, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error) {
@@ -84,7 +87,6 @@ public class KVV {
public void getModuleList(final NetworkCallback<Modules> callback, final NetworkErrorCallback error, boolean forceRefresh) { public void getModuleList(final NetworkCallback<Modules> callback, final NetworkErrorCallback error, boolean forceRefresh) {
this.getLastToken(token -> { this.getLastToken(token -> {
log.d("token here", token);
KVVModuleList modules = (KVVModuleList) addons.get("modules"); KVVModuleList modules = (KVVModuleList) addons.get("modules");
if (modules == null) { if (modules == null) {
modules = new KVVModuleList(KVV.this.context, token); modules = new KVVModuleList(KVV.this.context, token);
@@ -164,19 +166,15 @@ public class KVV {
} }
private void getLastToken(LastTokenCallback lastTokenCallback) { private void getLastToken(LastTokenCallback lastTokenCallback) {
if (this.isUpdating) { if (this.isLoginPending) {
this.updatingList.add(lastTokenCallback); this.updatingList.add(lastTokenCallback);
} else { } else {
lastTokenCallback.onReceived(this.lastToken); lastTokenCallback.onReceived(this.lastToken);
} }
} }
public void startUpdate() { private void endUpdate() {
this.isUpdating = true; this.isLoginPending = false;
}
public void endUpdate() {
this.isUpdating = false;
for (LastTokenCallback s: this.updatingList) { for (LastTokenCallback s: this.updatingList) {
s.onReceived(this.lastToken); s.onReceived(this.lastToken);
} }

View File

@@ -212,7 +212,6 @@ public class KVVModuleList extends HTTPService {
JSONArray sites = json.getJSONArray("announcement_collection"); JSONArray sites = json.getJSONArray("announcement_collection");
for (int i = 0; i < sites.length(); i++) { for (int i = 0; i < sites.length(); i++) {
//log.d("ANNOUNCEMENT FOUND!", i);
JSONObject site = sites.getJSONObject(i); JSONObject site = sites.getJSONObject(i);
String id = site.getString("announcementId"); String id = site.getString("announcementId");
String title = site.getString("title"); String title = site.getString("title");
@@ -266,7 +265,6 @@ public class KVVModuleList extends HTTPService {
JSONArray sites = json.getJSONArray("assignment_collection"); JSONArray sites = json.getJSONArray("assignment_collection");
for (int i = 0; i < sites.length(); i++) { for (int i = 0; i < sites.length(); i++) {
//log.d("Assignment FOUND!", i);
JSONObject site = sites.getJSONObject(i); JSONObject site = sites.getJSONObject(i);
String id = site.getString("id"); String id = site.getString("id");
String title = site.getString("title"); String title = site.getString("title");
@@ -282,11 +280,9 @@ public class KVVModuleList extends HTTPService {
ArrayList<String> urls = new ArrayList<>(); ArrayList<String> urls = new ArrayList<>();
for (int j =0; j<attachments.length(); j++){ for (int j =0; j<attachments.length(); j++){
urls.add(attachments.getJSONObject(j).optString("url",null)); urls.add(attachments.getJSONObject(j).optString("url",null));
//log.d("URL:", attachments.getJSONObject(j).optString("url",null));
} }
//log.d("Assignment:", id, dueTime, gradebookItemName, gradeScale);
assignments.add(0, new Assignment(id, title, dueTime, gradebookItemName, gradeScale, urls, instructions)); assignments.add(0, new Assignment(id, title, dueTime, gradebookItemName, gradeScale, urls, instructions));
} }
} catch (JSONException e) { } catch (JSONException e) {
@@ -336,7 +332,6 @@ public class KVVModuleList extends HTTPService {
JSONArray sites = json.getJSONArray("calendar_collection"); JSONArray sites = json.getJSONArray("calendar_collection");
for (int i = 0; i < sites.length(); i++) { for (int i = 0; i < sites.length(); i++) {
//log.d("CALENDAR ENTRY FOUND!", i);
JSONObject site = sites.getJSONObject(i); JSONObject site = sites.getJSONObject(i);
String id = site.getString("eventId"); String id = site.getString("eventId");
String type = site.getString("type"); String type = site.getString("type");