Merge branch 'login_callback_refactoring'

This commit is contained in:
Caesar2011
2018-07-29 03:44:34 +02:00
11 changed files with 161 additions and 49 deletions

View File

@@ -60,6 +60,7 @@ public class MainActivity extends AppCompatActivity
private int fragmentPage = FRAGMENT_NONE;
private String fragmentData = "";
private CanteenBrowser mCanteenBrowser;
private boolean mOfflineMode = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
@@ -101,12 +102,12 @@ public class MainActivity extends AppCompatActivity
checkAndDoLogin();
}
this.getCanteenBrowser().getCanteens(success -> {
/*this.getCanteenBrowser().getCanteens(success -> {
Canteen canteen = success.get(0);
this.getCanteenBrowser().getCanteen(canteen, success1 -> {
this.getCanteenBrowser().getDay(canteen.get(0), log::d, log::e, true);
}, log::e, true);
}, log::e, true);
}, log::e, true);*/
}
@Override
@@ -227,22 +228,24 @@ public class MainActivity extends AppCompatActivity
private void toLogoutState() {
changeFragment(FRAGMENT_LOGIN);
setOfflineBanner(true);
}
private void toLoginState(LoginToken loginToken, int newFragment, String newData) {
if (loginToken == null) {
toLogoutState();
} else {
toLoginState(loginToken.getFullname(), loginToken.getEmail(), newFragment, newData);
toLoginState(loginToken.getFullname(), loginToken.getEmail(), newFragment, newData, true);
}
}
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);
setOfflineBanner(onlineMode);
}
private void checkAndDoLogin() {
@@ -344,6 +347,24 @@ public class MainActivity extends AppCompatActivity
this.fragmentData = newData;
}
private void setOfflineBanner(boolean onlineMode) {
View offline_header = findViewById(R.id.offline_msg);
if (onlineMode)
offline_header.setVisibility(View.GONE);
else
offline_header.setVisibility(View.VISIBLE);
mOfflineMode = !onlineMode;
}
private void setRefreshFailedBanner(boolean refreshFailed) {
View viewNoConnection = findViewById(R.id.no_connection_msg);
if (!mOfflineMode && refreshFailed)
viewNoConnection.setVisibility(View.VISIBLE);
else
viewNoConnection.setVisibility(View.GONE);
}
private void afterAnyMenuInflate() {
getCanteenBrowser().getCanteens(success -> {
@@ -365,8 +386,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 +401,14 @@ public class MainActivity extends AppCompatActivity
public void onTitleTextChange(@StringRes int titleId) {
setTitle(titleId);
}
public void loginTokenInvalid(boolean doPrecheck) {
getKVV().invalidate();
checkAndDoLogin();
//log.d("Login token invalid!");
}
public void refreshFailed(boolean isFailed) {
setRefreshFailedBanner(isFailed);
}
}

View File

@@ -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);
}
}

View File

@@ -10,6 +10,7 @@ import android.view.View;
import android.view.ViewGroup;
import de.sebse.fuplanner.R;
import de.sebse.fuplanner.services.KVV.types.Modules;
import de.sebse.fuplanner.tools.MainAcitivityListener;
import de.sebse.fuplanner.tools.logging.Logger;
@@ -58,7 +59,11 @@ public class ModDetailFragment extends Fragment implements ModDetailListener {
}
if (mListener != null) {
mListener.onTitleTextChange(R.string.courses);
mListener.getKVV().getModuleList(success -> mListener.onTitleTextChange(success.get(mItemPos).title), log::e);
mListener.getKVV().getModuleList(success -> {
Modules.Module module = success.get(mItemPos);
if (module != null)
mListener.onTitleTextChange(module.title);
}, log::e);
}
}

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.Modules;
import de.sebse.fuplanner.tools.MainAcitivityListener;
import de.sebse.fuplanner.tools.network.NetworkCallback;
import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
@@ -23,8 +24,10 @@ 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) {
mListener = (MainAcitivityListener) context;
this.context = context;
this.updatingList = new ArrayList<>();
}
@@ -42,7 +45,7 @@ public class KVV {
lastToken = success;
this.endUpdate();
try {
login.saveOffline(this.context);
login.saveOffline();
} catch (IOException e) {
e.printStackTrace();
}
@@ -54,14 +57,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) {
new KVVLogin(context).deleteOffline();
lastToken = null;
}
addons.clear();
this.isLoginPending = true;
}
@@ -71,14 +78,7 @@ public class KVV {
}
public void getModule(String id, final NetworkCallback<Modules.Module> callback, final NetworkErrorCallback error, boolean forceRefresh) {
this.getLastToken(token -> {
KVVModuleList modules = (KVVModuleList) addons.get("modules");
if (modules == null) {
modules = new KVVModuleList(KVV.this.context, token);
addons.put("modules", modules);
}
modules.getModule(id, saveOnCallback(modules, callback), error, forceRefresh);
});
getModulePart(modules -> modules.getModule(id, saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh));
}
public void getModuleList(final NetworkCallback<Modules> callback, final NetworkErrorCallback error) {
@@ -86,14 +86,7 @@ public class KVV {
}
public void getModuleList(final NetworkCallback<Modules> callback, final NetworkErrorCallback error, boolean forceRefresh) {
this.getLastToken(token -> {
KVVModuleList modules = (KVVModuleList) addons.get("modules");
if (modules == null) {
modules = new KVVModuleList(KVV.this.context, token);
addons.put("modules", modules);
}
modules.getModuleList(saveOnCallback(modules, callback), error, forceRefresh);
});
getModulePart(modules -> modules.getModuleList(saveOnCallback(modules, callback, forceRefresh), errorOnCallback(error), forceRefresh));
}
public void getModuleDetails(Modules.Module module, final NetworkCallback<Pair<Modules.Module, Boolean>> callback, final NetworkErrorCallback error) {
@@ -101,7 +94,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 +102,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 +110,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 +118,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 +126,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));
}
@@ -142,24 +135,36 @@ public class KVV {
this.getLastToken(token -> {
KVVModuleList modules = (KVVModuleList) addons.get("modules");
if (modules == null) {
modules = new KVVModuleList(KVV.this.context, token);
modules = new KVVModuleList(this.context, token);
addons.put("modules", modules);
}
func.apply(modules);
});
}
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(false);
else
mListener.refreshFailed(true);
errorCallback.onError(error);
});
}
@FunctionalInterface
interface ModListFunction {
void apply(KVVModuleList mod);

View File

@@ -21,10 +21,12 @@ import de.sebse.fuplanner.tools.network.NetworkErrorCallback;
*/
class KVVLogin extends HTTPService {
private final Context mContext;
private LoginToken loginToken;
KVVLogin(Context context) {
super(context, false);
this.mContext = context;
try {
this.loginToken = LoginToken.load(context);
} catch (IOException e) {
@@ -57,14 +59,14 @@ class KVVLogin extends HTTPService {
}
}
public void deleteOffline(Context context) {
public void deleteOffline() {
if (this.loginToken != null)
this.loginToken.delete(context);
this.loginToken.delete(mContext);
}
public void saveOffline(Context context) throws IOException {
public void saveOffline() throws IOException {
if (this.loginToken != null)
this.loginToken.save(context);
this.loginToken.save(mContext);
}
@@ -103,7 +105,7 @@ class KVVLogin extends HTTPService {
loginToken.setAdditionals(displayName, email);
callback.onResponse(loginToken);
} catch (JSONException e) {
errorCallback.onError(new NetworkError(100201, 403, "Cannot parse announcements!"));
errorCallback.onError(new NetworkError(100201, 403, "Cannot parse profile!"));
return;
}
}, error -> errorCallback.onError(new NetworkError(100200, error.networkResponse.statusCode, "Testing login failed!")));

View File

@@ -120,7 +120,11 @@ public class KVVModuleList extends HTTPService {
errorCallback.onError(new NetworkError(101103, 403, "Cannot parse module list!"));
return;
}
callback.onResponse(modules);
// Empty module may *may be* because token is invalid -> check
if (modules.size() == 0)
testLogin(token, token -> callback.onResponse(modules), errorCallback);
else
callback.onResponse(modules);
}, error -> errorCallback.onError(new NetworkError(101104, error.networkResponse.statusCode, "Cannot get module list!")));
}
@@ -228,7 +232,11 @@ public class KVVModuleList extends HTTPService {
errorCallback.onError(new NetworkError(101202, 403, "Cannot parse announcements!"));
return;
}
callback.onResponse(announcements);
// Empty announcements may *may be* because token is invalid -> check
if (announcements.size() == 0)
testLogin(token, token -> callback.onResponse(announcements), errorCallback);
else
callback.onResponse(announcements);
}, error -> errorCallback.onError(new NetworkError(101203, error.networkResponse.statusCode, "Cannot get announcements!")));
}
@@ -293,7 +301,11 @@ public class KVVModuleList extends HTTPService {
errorCallback.onError(new NetworkError(101302, 403, "Cannot parse announcements!"));
return;
}
callback.onResponse(assignments);
// Empty assignments may *may be* because token is invalid -> check
if (assignments.size() == 0)
testLogin(token, token -> callback.onResponse(assignments), errorCallback);
else
callback.onResponse(assignments);
}, error -> errorCallback.onError(new NetworkError(101303, error.networkResponse.statusCode, "Cannot get assignments!")));
@@ -350,7 +362,11 @@ public class KVVModuleList extends HTTPService {
return;
}
events.sort();
callback.onResponse(events);
// Empty events may *may be* because token is invalid -> check
if (events.size() == 0)
testLogin(token, token -> callback.onResponse(events), errorCallback);
else
callback.onResponse(events);
}, error -> errorCallback.onError(new NetworkError(101403, error.networkResponse.statusCode, "Cannot get calendar entries!")));
}
@@ -375,7 +391,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 ->{
@@ -409,4 +425,27 @@ public class KVVModuleList extends HTTPService {
}
// TODO Better, more elegant solution than duplicate code KVVLogin
private void testLogin(LoginToken loginToken, NetworkCallback<LoginToken> callback, NetworkErrorCallback errorCallback) {
get(String.format("https://kvv.imp.fu-berlin.de/direct/profile/%s.json", loginToken.getUsername()), loginToken.getCookies(), response -> {
String body = response.getParsed();
try {
JSONObject json = new JSONObject(body);
String displayName = json.getString("displayName");
String email = json.getString("email");
loginToken.setAdditionals(displayName, email);
callback.onResponse(loginToken);
} catch (JSONException e) {
errorCallback.onError(new NetworkError(100201, 403, "Cannot parse profile!"));
return;
}
}, error -> errorCallback.onError(new NetworkError(100200, error.networkResponse.statusCode, "Testing login failed!")));
}
}

View File

@@ -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;

View File

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

View File

@@ -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

View File

@@ -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>

View File

@@ -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>