Login Refactoring
This commit is contained in:
@@ -91,12 +91,12 @@ public class MainActivity extends AppCompatActivity
|
||||
LoginToken loginToken = getKVV().easyLogin();
|
||||
if (newFragmentPage != FRAGMENT_LOGIN && newFragmentPage != FRAGMENT_STARTUP && newFragmentPage != FRAGMENT_NONE) {
|
||||
if (loginToken != null)
|
||||
toLoginState(loginToken, newFragmentPage, newFragmentData);
|
||||
toLoginState(loginToken, newFragmentPage, newFragmentData, true);
|
||||
else
|
||||
checkAndDoLogin();
|
||||
} else {
|
||||
if (loginToken != null)
|
||||
toLoginState(loginToken, getDefaultFragmentAfterLogin(), "");
|
||||
toLoginState(loginToken, getDefaultFragmentAfterLogin(), "", true);
|
||||
else
|
||||
checkAndDoLogin();
|
||||
}
|
||||
@@ -227,22 +227,29 @@ public class MainActivity extends AppCompatActivity
|
||||
|
||||
private void toLogoutState() {
|
||||
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) {
|
||||
toLogoutState();
|
||||
} 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);
|
||||
|
||||
View header = mNavigationView.getHeaderView(0);
|
||||
((TextView) header.findViewById(R.id.login_name)).setText(fullname);
|
||||
((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() {
|
||||
@@ -253,7 +260,7 @@ public class MainActivity extends AppCompatActivity
|
||||
return;
|
||||
}
|
||||
this.getKVV().login(credentials.getUsername(), credentials.getPassword(), success -> {
|
||||
toLoginState(success, getDefaultFragmentAfterLogin(), "");
|
||||
toLoginState(success, getDefaultFragmentAfterLogin(), "", true);
|
||||
}, error -> {
|
||||
log.e(error);
|
||||
toLogoutState();
|
||||
@@ -365,8 +372,8 @@ public class MainActivity extends AppCompatActivity
|
||||
|
||||
|
||||
|
||||
public void onLoginFragmentInteraction(LoginToken loginToken) {
|
||||
toLoginState(loginToken.getFullname(), loginToken.getEmail(), getDefaultFragmentAfterLogin(), "");
|
||||
public void onLoginFragmentInteraction(LoginToken loginToken, boolean onlineMode) {
|
||||
toLoginState(loginToken.getFullname(), loginToken.getEmail(), getDefaultFragmentAfterLogin(), "", onlineMode);
|
||||
}
|
||||
|
||||
public void onModulesFragmentInteraction(final String itemID) {
|
||||
@@ -380,4 +387,18 @@ public class MainActivity extends AppCompatActivity
|
||||
public void onTitleTextChange(@StringRes int 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ public class LoginFragment extends Fragment {
|
||||
Button offline_btn = v.findViewById(R.id.btn_offline);
|
||||
offline_btn.setVisibility(View.VISIBLE);
|
||||
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) {
|
||||
@@ -102,7 +102,7 @@ public class LoginFragment extends Fragment {
|
||||
progressDialog.dismiss();
|
||||
gauth.setLoginState(username, password);
|
||||
if (mListener != null)
|
||||
mListener.onLoginFragmentInteraction(success);
|
||||
mListener.onLoginFragmentInteraction(success, true);
|
||||
}, error -> {
|
||||
progressDialog.dismiss();
|
||||
log.e("Error on KVV login!", error);
|
||||
@@ -148,6 +148,6 @@ public class LoginFragment extends Fragment {
|
||||
* >Communicating with Other Fragments</a> for more information.
|
||||
*/
|
||||
public interface OnLoginFragmentInteractionListener {
|
||||
void onLoginFragmentInteraction(LoginToken loginToken);
|
||||
void onLoginFragmentInteraction(LoginToken loginToken, boolean onlineMode);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,6 +10,7 @@ import java.util.HashMap;
|
||||
|
||||
import de.sebse.fuplanner.services.KVV.types.LoginToken;
|
||||
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.NetworkErrorCallback;
|
||||
|
||||
@@ -23,8 +24,11 @@ public class KVV {
|
||||
private boolean isLoginPending = true;
|
||||
private ArrayList<LastTokenCallback> updatingList;
|
||||
private HashMap<String, Object> addons = new HashMap<>();
|
||||
private MainAcitivityListener mListener;
|
||||
|
||||
public KVV(Context context) {
|
||||
if (context instanceof MainAcitivityListener)
|
||||
mListener = (MainAcitivityListener) context;
|
||||
this.context = context;
|
||||
this.updatingList = new ArrayList<>();
|
||||
}
|
||||
@@ -54,14 +58,18 @@ public class KVV {
|
||||
}
|
||||
|
||||
public void logout() {
|
||||
if (lastToken != null) {
|
||||
lastToken.delete(this.context);
|
||||
lastToken = null;
|
||||
}
|
||||
KVVModuleList modules = (KVVModuleList) addons.get("modules");
|
||||
if (modules != null) {
|
||||
modules.deleteModulesOffline(this.context);
|
||||
}
|
||||
invalidate();
|
||||
}
|
||||
|
||||
public void invalidate() {
|
||||
if (lastToken != null) {
|
||||
lastToken.delete(this.context);
|
||||
lastToken = null;
|
||||
}
|
||||
addons.clear();
|
||||
this.isLoginPending = true;
|
||||
}
|
||||
@@ -77,7 +85,7 @@ public class KVV {
|
||||
modules = new KVVModuleList(KVV.this.context, token);
|
||||
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);
|
||||
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) {
|
||||
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) {
|
||||
@@ -109,7 +117,7 @@ public class KVV {
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -117,7 +125,7 @@ public class KVV {
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -125,7 +133,7 @@ public class KVV {
|
||||
}
|
||||
|
||||
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) {
|
||||
@@ -133,7 +141,7 @@ public class KVV {
|
||||
}
|
||||
|
||||
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 -> {
|
||||
try {
|
||||
modules.saveModulesOffline(this.context);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
if (forceRefresh)
|
||||
mListener.refreshFailed(false);
|
||||
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
|
||||
interface ModListFunction {
|
||||
void apply(KVVModuleList mod);
|
||||
|
||||
@@ -375,7 +375,7 @@ public class KVVModuleList extends HTTPService {
|
||||
|
||||
private void getGradebookUpgrade(String ID, final NetworkCallback<ArrayList<Gradebook>> callback, final NetworkErrorCallback errorCallback) {
|
||||
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;
|
||||
}
|
||||
get(String.format("https://kvv.imp.fu-berlin.de/direct/gradebook/site/%s.json", ID ), token.getCookies(), response ->{
|
||||
|
||||
@@ -16,7 +16,7 @@ import java.util.HashMap;
|
||||
*/
|
||||
|
||||
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 final String username;
|
||||
|
||||
@@ -13,4 +13,8 @@ public interface MainAcitivityListener {
|
||||
KVV getKVV();
|
||||
|
||||
GoogleAuth getGoogleAuth();
|
||||
|
||||
void loginTokenInvalid();
|
||||
|
||||
void refreshFailed(boolean isFailed);
|
||||
}
|
||||
|
||||
@@ -19,6 +19,28 @@
|
||||
android:background="@color/colorFUGreen"
|
||||
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>
|
||||
|
||||
<FrameLayout
|
||||
|
||||
@@ -8,4 +8,6 @@
|
||||
<color name="colorFUBlack">#333333</color>
|
||||
<color name="colorFURedLight">#f5cccc</color>
|
||||
<color name="colorFURedLight2">#cca3a3</color>
|
||||
<color name="colorFURed">#CC0000</color>
|
||||
<color name="colorFUOrange">#FF9900</color>
|
||||
</resources>
|
||||
|
||||
@@ -38,4 +38,6 @@
|
||||
<string name="events">Events</string>
|
||||
<string name="gradebook">Gradebook</string>
|
||||
<string name="Current_percentage">Current Percentage</string>
|
||||
<string name="offline_mode">Offline Mode</string>
|
||||
<string name="refresh_failed">Refresh failed...</string>
|
||||
</resources>
|
||||
|
||||
Reference in New Issue
Block a user