Login Refactoring

This commit is contained in:
Caesar2011
2018-07-28 19:49:14 +02:00
parent 1608894635
commit f05e151a7a
9 changed files with 96 additions and 25 deletions

View File

@@ -91,12 +91,12 @@ public class MainActivity extends AppCompatActivity
LoginToken loginToken = getKVV().easyLogin(); LoginToken loginToken = getKVV().easyLogin();
if (newFragmentPage != FRAGMENT_LOGIN && newFragmentPage != FRAGMENT_STARTUP && newFragmentPage != FRAGMENT_NONE) { if (newFragmentPage != FRAGMENT_LOGIN && newFragmentPage != FRAGMENT_STARTUP && newFragmentPage != FRAGMENT_NONE) {
if (loginToken != null) if (loginToken != null)
toLoginState(loginToken, newFragmentPage, newFragmentData); toLoginState(loginToken, newFragmentPage, newFragmentData, true);
else else
checkAndDoLogin(); checkAndDoLogin();
} else { } else {
if (loginToken != null) if (loginToken != null)
toLoginState(loginToken, getDefaultFragmentAfterLogin(), ""); toLoginState(loginToken, getDefaultFragmentAfterLogin(), "", true);
else else
checkAndDoLogin(); checkAndDoLogin();
} }
@@ -227,22 +227,29 @@ public class MainActivity extends AppCompatActivity
private void toLogoutState() { private void toLogoutState() {
changeFragment(FRAGMENT_LOGIN); changeFragment(FRAGMENT_LOGIN);
View offline_header = findViewById(R.id.offline_msg);
offline_header.setVisibility(View.GONE);
} }
private void toLoginState(LoginToken loginToken, int newFragment, String newData) { private void toLoginState(LoginToken loginToken, int newFragment, String newData, boolean onlineMode) {
if (loginToken == null) { if (loginToken == null) {
toLogoutState(); toLogoutState();
} else { } else {
toLoginState(loginToken.getFullname(), loginToken.getEmail(), newFragment, newData); toLoginState(loginToken.getFullname(), loginToken.getEmail(), newFragment, newData, onlineMode);
} }
} }
private void toLoginState(String fullname, String email, int newFragment, String newData) { private void toLoginState(String fullname, String email, int newFragment, String newData, boolean onlineMode) {
changeFragment(newFragment, newData); changeFragment(newFragment, newData);
View header = mNavigationView.getHeaderView(0); View header = mNavigationView.getHeaderView(0);
((TextView) header.findViewById(R.id.login_name)).setText(fullname); ((TextView) header.findViewById(R.id.login_name)).setText(fullname);
((TextView) header.findViewById(R.id.login_mail)).setText(email); ((TextView) header.findViewById(R.id.login_mail)).setText(email);
View offline_header = findViewById(R.id.offline_msg);
if (onlineMode)
offline_header.setVisibility(View.GONE);
else
offline_header.setVisibility(View.VISIBLE);
} }
private void checkAndDoLogin() { private void checkAndDoLogin() {
@@ -253,7 +260,7 @@ public class MainActivity extends AppCompatActivity
return; return;
} }
this.getKVV().login(credentials.getUsername(), credentials.getPassword(), success -> { this.getKVV().login(credentials.getUsername(), credentials.getPassword(), success -> {
toLoginState(success, getDefaultFragmentAfterLogin(), ""); toLoginState(success, getDefaultFragmentAfterLogin(), "", true);
}, error -> { }, error -> {
log.e(error); log.e(error);
toLogoutState(); toLogoutState();
@@ -365,8 +372,8 @@ public class MainActivity extends AppCompatActivity
public void onLoginFragmentInteraction(LoginToken loginToken) { public void onLoginFragmentInteraction(LoginToken loginToken, boolean onlineMode) {
toLoginState(loginToken.getFullname(), loginToken.getEmail(), getDefaultFragmentAfterLogin(), ""); toLoginState(loginToken.getFullname(), loginToken.getEmail(), getDefaultFragmentAfterLogin(), "", onlineMode);
} }
public void onModulesFragmentInteraction(final String itemID) { public void onModulesFragmentInteraction(final String itemID) {
@@ -380,4 +387,18 @@ public class MainActivity extends AppCompatActivity
public void onTitleTextChange(@StringRes int titleId) { public void onTitleTextChange(@StringRes int titleId) {
setTitle(titleId); setTitle(titleId);
} }
public void loginTokenInvalid() {
//getKVV().invalidate();
//checkAndDoLogin();
log.d("Login token invalid!");
}
public void refreshFailed(boolean isFailed) {
View viewNoConnection = findViewById(R.id.no_connection_msg);
if (isFailed)
viewNoConnection.setVisibility(View.VISIBLE);
else
viewNoConnection.setVisibility(View.GONE);
}
} }

View File

@@ -69,7 +69,7 @@ public class LoginFragment extends Fragment {
Button offline_btn = v.findViewById(R.id.btn_offline); Button offline_btn = v.findViewById(R.id.btn_offline);
offline_btn.setVisibility(View.VISIBLE); offline_btn.setVisibility(View.VISIBLE);
offline_btn.setText(v.getResources().getString(R.string.enter_offline_mode, modules.getToken().getUsername())); offline_btn.setText(v.getResources().getString(R.string.enter_offline_mode, modules.getToken().getUsername()));
offline_btn.setOnClickListener(v1 -> mListener.onLoginFragmentInteraction(modules.getToken())); offline_btn.setOnClickListener(v1 -> mListener.onLoginFragmentInteraction(modules.getToken(), false));
} }
} }
} catch (IOException e) { } catch (IOException e) {
@@ -102,7 +102,7 @@ public class LoginFragment extends Fragment {
progressDialog.dismiss(); progressDialog.dismiss();
gauth.setLoginState(username, password); gauth.setLoginState(username, password);
if (mListener != null) if (mListener != null)
mListener.onLoginFragmentInteraction(success); mListener.onLoginFragmentInteraction(success, true);
}, error -> { }, error -> {
progressDialog.dismiss(); progressDialog.dismiss();
log.e("Error on KVV login!", error); log.e("Error on KVV login!", error);
@@ -148,6 +148,6 @@ public class LoginFragment extends Fragment {
* >Communicating with Other Fragments</a> for more information. * >Communicating with Other Fragments</a> for more information.
*/ */
public interface OnLoginFragmentInteractionListener { public interface OnLoginFragmentInteractionListener {
void onLoginFragmentInteraction(LoginToken loginToken); void onLoginFragmentInteraction(LoginToken loginToken, boolean onlineMode);
} }
} }

View File

@@ -10,6 +10,7 @@ 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.MainAcitivityListener;
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;
@@ -23,8 +24,11 @@ public class KVV {
private boolean isLoginPending = true; 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 MainAcitivityListener mListener;
public KVV(Context context) { public KVV(Context context) {
if (context instanceof MainAcitivityListener)
mListener = (MainAcitivityListener) context;
this.context = context; this.context = context;
this.updatingList = new ArrayList<>(); this.updatingList = new ArrayList<>();
} }
@@ -54,14 +58,18 @@ public class KVV {
} }
public void logout() { public void logout() {
if (lastToken != null) {
lastToken.delete(this.context);
lastToken = null;
}
KVVModuleList modules = (KVVModuleList) addons.get("modules"); KVVModuleList modules = (KVVModuleList) addons.get("modules");
if (modules != null) { if (modules != null) {
modules.deleteModulesOffline(this.context); modules.deleteModulesOffline(this.context);
} }
invalidate();
}
public void invalidate() {
if (lastToken != null) {
lastToken.delete(this.context);
lastToken = null;
}
addons.clear(); addons.clear();
this.isLoginPending = true; this.isLoginPending = true;
} }
@@ -77,7 +85,7 @@ public class KVV {
modules = new KVVModuleList(KVV.this.context, token); modules = new KVVModuleList(KVV.this.context, token);
addons.put("modules", modules); addons.put("modules", modules);
} }
modules.getModule(id, saveOnCallback(modules, callback), error, forceRefresh); modules.getModule(id, saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh);
}); });
} }
@@ -92,7 +100,7 @@ public class KVV {
modules = new KVVModuleList(KVV.this.context, token); modules = new KVVModuleList(KVV.this.context, token);
addons.put("modules", modules); addons.put("modules", modules);
} }
modules.getModuleList(saveOnCallback(modules, callback), error, forceRefresh); modules.getModuleList(saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh);
}); });
} }
@@ -101,7 +109,7 @@ public class KVV {
} }
public void getModuleDetails(Modules.Module module, final NetworkCallback<Pair<Modules.Module, Boolean>> callback, final NetworkErrorCallback error, boolean forceRefresh) { public void getModuleDetails(Modules.Module module, final NetworkCallback<Pair<Modules.Module, Boolean>> callback, final NetworkErrorCallback error, boolean forceRefresh) {
getModulePart(modules -> modules.getModuleDetails(module, saveOnCallback(modules, callback), error, forceRefresh)); getModulePart(modules -> modules.getModuleDetails(module, saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh));
} }
public void getModuleAnnouncements(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error) { public void getModuleAnnouncements(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error) {
@@ -109,7 +117,7 @@ public class KVV {
} }
public void getModuleAnnouncements(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error, boolean forceRefresh) { public void getModuleAnnouncements(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error, boolean forceRefresh) {
getModulePart(modules -> modules.getAnnouncements(module, saveOnCallback(modules, callback), error, forceRefresh)); getModulePart(modules -> modules.getAnnouncements(module, saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh));
} }
public void getModuleAssignments(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error) { public void getModuleAssignments(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error) {
@@ -117,7 +125,7 @@ public class KVV {
} }
public void getModuleAssignments(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error, boolean forceRefresh) { public void getModuleAssignments(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error, boolean forceRefresh) {
getModulePart(modules -> modules.getAssignments(module, saveOnCallback(modules, callback), error, forceRefresh)); getModulePart(modules -> modules.getAssignments(module, saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh));
} }
public void getModuleEvents(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error) { public void getModuleEvents(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error) {
@@ -125,7 +133,7 @@ public class KVV {
} }
public void getModuleEvents(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error, boolean forceRefresh) { public void getModuleEvents(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error, boolean forceRefresh) {
getModulePart(modules -> modules.getEvents(module, saveOnCallback(modules, callback), error, forceRefresh)); getModulePart(modules -> modules.getEvents(module, saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh));
} }
public void getModuleGradebook(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error) { public void getModuleGradebook(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error) {
@@ -133,7 +141,7 @@ public class KVV {
} }
public void getModuleGradebook(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error, boolean forceRefresh) { public void getModuleGradebook(Modules.Module module, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error, boolean forceRefresh) {
getModulePart(modules -> modules.getGradebook(module, saveOnCallback(modules, callback), error, forceRefresh)); getModulePart(modules -> modules.getGradebook(module, saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh));
} }
@@ -149,17 +157,29 @@ public class KVV {
}); });
} }
private<T> NetworkCallback<T> saveOnCallback(KVVModuleList modules, NetworkCallback<T> callback){ private<T> NetworkCallback<T> saveOnCallback(KVVModuleList modules, NetworkCallback<T> callback, boolean forceRefresh){
return (success -> { return (success -> {
try { try {
modules.saveModulesOffline(this.context); modules.saveModulesOffline(this.context);
} catch (IOException e) { } catch (IOException e) {
e.printStackTrace(); e.printStackTrace();
} }
if (forceRefresh)
mListener.refreshFailed(false);
callback.onResponse(success); callback.onResponse(success);
}); });
} }
private NetworkErrorCallback errorOnCallback(NetworkErrorCallback errorCallback){
return (error -> {
if (error.getHttpStatus() == 401 || error.getHttpStatus() == 403)
mListener.loginTokenInvalid();
else
mListener.refreshFailed(true);
errorCallback.onError(error);
});
}
@FunctionalInterface @FunctionalInterface
interface ModListFunction { interface ModListFunction {
void apply(KVVModuleList mod); void apply(KVVModuleList mod);

View File

@@ -375,7 +375,7 @@ public class KVVModuleList extends HTTPService {
private void getGradebookUpgrade(String ID, final NetworkCallback<ArrayList<Gradebook>> callback, final NetworkErrorCallback errorCallback) { private void getGradebookUpgrade(String ID, final NetworkCallback<ArrayList<Gradebook>> callback, final NetworkErrorCallback errorCallback) {
if (token == null) { if (token == null) {
errorCallback.onError(new NetworkError(101104, 500, "Currently running in offline mode!")); errorCallback.onError(new NetworkError(101504, 500, "Currently running in offline mode!"));
return; return;
} }
get(String.format("https://kvv.imp.fu-berlin.de/direct/gradebook/site/%s.json", ID ), token.getCookies(), response ->{ get(String.format("https://kvv.imp.fu-berlin.de/direct/gradebook/site/%s.json", ID ), token.getCookies(), response ->{

View File

@@ -16,7 +16,7 @@ import java.util.HashMap;
*/ */
public class LoginToken implements Serializable { public class LoginToken implements Serializable {
private static final long EASY_LOGIN_TIME_MILLIS = 1000 * 60 * 60 * 3; private static final long EASY_LOGIN_TIME_MILLIS = 1000 * 60 * 60 * 300;
private static final String FILE_NAME = "LoginTokenSaving"; private static final String FILE_NAME = "LoginTokenSaving";
private final String username; private final String username;

View File

@@ -13,4 +13,8 @@ public interface MainAcitivityListener {
KVV getKVV(); KVV getKVV();
GoogleAuth getGoogleAuth(); GoogleAuth getGoogleAuth();
void loginTokenInvalid();
void refreshFailed(boolean isFailed);
} }

View File

@@ -19,6 +19,28 @@
android:background="@color/colorFUGreen" android:background="@color/colorFUGreen"
app:popupTheme="@style/AppTheme.PopupOverlay" /> app:popupTheme="@style/AppTheme.PopupOverlay" />
<TextView
android:id="@+id/offline_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorFURed"
android:textColor="@color/colorFUWhite"
android:gravity="center"
android:textStyle="bold"
android:visibility="gone"
android:text="@string/offline_mode" />
<TextView
android:id="@+id/no_connection_msg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorFUOrange"
android:textColor="@color/colorFUWhite"
android:gravity="center"
android:textStyle="bold"
android:visibility="gone"
android:text="@string/refresh_failed" />
</android.support.design.widget.AppBarLayout> </android.support.design.widget.AppBarLayout>
<FrameLayout <FrameLayout

View File

@@ -8,4 +8,6 @@
<color name="colorFUBlack">#333333</color> <color name="colorFUBlack">#333333</color>
<color name="colorFURedLight">#f5cccc</color> <color name="colorFURedLight">#f5cccc</color>
<color name="colorFURedLight2">#cca3a3</color> <color name="colorFURedLight2">#cca3a3</color>
<color name="colorFURed">#CC0000</color>
<color name="colorFUOrange">#FF9900</color>
</resources> </resources>

View File

@@ -38,4 +38,6 @@
<string name="events">Events</string> <string name="events">Events</string>
<string name="gradebook">Gradebook</string> <string name="gradebook">Gradebook</string>
<string name="Current_percentage">Current Percentage</string> <string name="Current_percentage">Current Percentage</string>
<string name="offline_mode">Offline Mode</string>
<string name="refresh_failed">Refresh failed...</string>
</resources> </resources>