From 2af55e1acaaaefdf3d62041369a49d0b966da90a Mon Sep 17 00:00:00 2001 From: Ezerous Date: Fri, 26 Jun 2020 23:48:37 +0300 Subject: [PATCH] Add InvalidSessionException, refactoring --- .../activities/main/forum/ForumFragment.java | 2 +- .../main/recent/RecentFragment.java | 2 +- .../main/unread/UnreadFragment.java | 36 +++++++------ .../activities/profile/ProfileActivity.java | 2 +- .../activities/shoutbox/SendShoutTask.java | 4 +- .../activities/shoutbox/ShoutboxFragment.java | 2 +- .../activities/shoutbox/ShoutboxTask.java | 2 +- .../activities/topic/TopicActivity.java | 2 +- .../activities/topic/tasks/DeleteTask.java | 4 +- .../topic/tasks/RemoveVoteTask.java | 4 +- .../topic/tasks/SubmitVoteTask.java | 4 +- .../gr/thmmy/mthmmy/base/BaseActivity.java | 4 +- .../session/InvalidSessionException.java | 13 +++++ .../thmmy/mthmmy/session/SessionManager.java | 50 +++++++++---------- .../java/gr/thmmy/mthmmy/utils/Parcel.java | 1 - .../{ => networking}/NetworkResultCodes.java | 2 +- .../utils/{ => networking}/NetworkTask.java | 13 ++++- .../mthmmy/utils/parsing/NewParseTask.java | 7 ++- .../mthmmy/viewmodel/TopicViewModel.java | 2 +- app/src/main/res/layout/fragment_unread.xml | 1 + 20 files changed, 92 insertions(+), 65 deletions(-) create mode 100644 app/src/main/java/gr/thmmy/mthmmy/session/InvalidSessionException.java rename app/src/main/java/gr/thmmy/mthmmy/utils/{ => networking}/NetworkResultCodes.java (94%) rename app/src/main/java/gr/thmmy/mthmmy/utils/{ => networking}/NetworkTask.java (86%) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java index 061db9d2..3344d089 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java @@ -30,7 +30,7 @@ import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.Board; import gr.thmmy.mthmmy.model.Category; import gr.thmmy.mthmmy.session.SessionManager; -import gr.thmmy.mthmmy.utils.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; import gr.thmmy.mthmmy.utils.parsing.NewParseTask; import gr.thmmy.mthmmy.utils.parsing.ParseException; import gr.thmmy.mthmmy.views.CustomRecyclerView; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java index 5b27c570..625f7440 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/recent/RecentFragment.java @@ -26,7 +26,7 @@ import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.TopicSummary; import gr.thmmy.mthmmy.session.SessionManager; -import gr.thmmy.mthmmy.utils.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; import gr.thmmy.mthmmy.utils.parsing.NewParseTask; import gr.thmmy.mthmmy.utils.parsing.ParseException; import gr.thmmy.mthmmy.views.CustomRecyclerView; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java index f8097a81..a8ba7f10 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/unread/UnreadFragment.java @@ -29,9 +29,10 @@ import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.base.BaseFragment; import gr.thmmy.mthmmy.model.TopicSummary; +import gr.thmmy.mthmmy.session.InvalidSessionException; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.session.ValidateSessionTask; -import gr.thmmy.mthmmy.utils.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; import gr.thmmy.mthmmy.utils.parsing.NewParseTask; import gr.thmmy.mthmmy.utils.parsing.ParseException; import gr.thmmy.mthmmy.views.CustomRecyclerView; @@ -266,6 +267,8 @@ public class UnreadFragment extends BaseFragment { hideProgressUI(); if (resultCode == NetworkResultCodes.NETWORK_ERROR) Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); + else if (resultCode == SessionManager.INVALID_SESSION) + Toast.makeText(getContext(), "Session verification failed. Please try logging in again.", Toast.LENGTH_LONG).show(); else Toast.makeText(getContext(), "Unexpected error," + " please contact the developers with the details", Toast.LENGTH_LONG).show(); @@ -278,7 +281,10 @@ public class UnreadFragment extends BaseFragment { } @Override - protected ArrayList parse(Document document, Response response) throws ParseException { + protected ArrayList parse(Document document, Response response) throws ParseException, InvalidSessionException { + if(!document.select("td:containsOwn(Only registered members are allowed to access this section.)").isEmpty()) + throw new InvalidSessionException(); + Elements unread = document.select("table.bordercolor[cellspacing=1] tr:not(.titlebg)"); ArrayList fetchedTopicSummaries = new ArrayList<>(); if (!unread.isEmpty()) { @@ -338,42 +344,42 @@ public class UnreadFragment extends BaseFragment { progressBar.setVisibility(ProgressBar.VISIBLE); } - private void onMarkReadTaskFinished(int resultCode, Boolean isSessionVerified) { + private void onMarkReadTaskFinished(int resultCode, Void isSessionVerified) { hideProgressUI(); - if (resultCode == NetworkResultCodes.SUCCESSFUL) { - if (!isSessionVerified){ - Toast.makeText(getContext(), "Session verification failed", Toast.LENGTH_SHORT).show(); - startValidateSessionTask(); - } - else + if (resultCode == NetworkResultCodes.SUCCESSFUL) startUnreadTask(); - } else{ hideProgressUI(); if (resultCode == NetworkResultCodes.NETWORK_ERROR) Toast.makeText(getContext(), "Network error", Toast.LENGTH_SHORT).show(); + else if (resultCode == SessionManager.INVALID_SESSION){ + Toast.makeText(getContext(), "Session verification failed. Please try logging out and back in again", Toast.LENGTH_LONG).show(); + startValidateSessionTask(); + } else Toast.makeText(getContext(), "Unexpected error," + " please contact the developers with the details", Toast.LENGTH_LONG).show(); } } - private class MarkReadTask extends NewParseTask { - MarkReadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener onParseTaskFinishedListener) { + private class MarkReadTask extends NewParseTask { + MarkReadTask(OnTaskStartedListener onTaskStartedListener, OnNetworkTaskFinishedListener onParseTaskFinishedListener) { super(onTaskStartedListener, onParseTaskFinishedListener); } @Override - protected Boolean parse(Document document, Response response) throws ParseException { + protected Void parse(Document document, Response response) throws ParseException { Elements sessionVerificationFailed = document.select("td:containsOwn(Session " + "verification failed. Please try logging out and back in again, and then try " + "again.), td:containsOwn(Η επαλήθευση συνόδου απέτυχε. Παρακαλούμε κάντε " + "αποσύνδεση, επανασύνδεση και ξαναδοκιμάστε.)"); - return sessionVerificationFailed.isEmpty(); + if(!sessionVerificationFailed.isEmpty()) + throw new InvalidSessionException(); + return null; } @Override - protected int getResultCode(Response response, Boolean isSessionVerified) { + protected int getResultCode(Response response, Void v) { return NetworkResultCodes.SUCCESSFUL; } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java index 016009ca..223f36e9 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java @@ -45,8 +45,8 @@ import gr.thmmy.mthmmy.activities.topic.TopicActivity; import gr.thmmy.mthmmy.base.BaseActivity; import gr.thmmy.mthmmy.model.PostSummary; import gr.thmmy.mthmmy.model.ThmmyPage; -import gr.thmmy.mthmmy.utils.NetworkResultCodes; import gr.thmmy.mthmmy.utils.Parcel; +import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; import gr.thmmy.mthmmy.utils.parsing.NewParseTask; import gr.thmmy.mthmmy.utils.parsing.ParseException; import gr.thmmy.mthmmy.utils.ui.CenterVerticalSpan; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/SendShoutTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/SendShoutTask.java index 3b2af22f..171be1e3 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/SendShoutTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/SendShoutTask.java @@ -4,8 +4,8 @@ import org.jsoup.nodes.Document; import java.io.IOException; -import gr.thmmy.mthmmy.utils.NetworkResultCodes; -import gr.thmmy.mthmmy.utils.NetworkTask; +import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.networking.NetworkTask; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; import okhttp3.Request; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java index 0049fa6b..e776423f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxFragment.java @@ -21,7 +21,7 @@ import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseApplication; import gr.thmmy.mthmmy.model.Shout; import gr.thmmy.mthmmy.model.Shoutbox; -import gr.thmmy.mthmmy.utils.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; import gr.thmmy.mthmmy.viewmodel.ShoutboxViewModel; import gr.thmmy.mthmmy.views.CustomRecyclerView; import gr.thmmy.mthmmy.views.editorview.EditorView; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java index 8a1d3768..5fbb1415 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/shoutbox/ShoutboxTask.java @@ -7,7 +7,7 @@ import java.util.ArrayList; import gr.thmmy.mthmmy.model.Shout; import gr.thmmy.mthmmy.model.Shoutbox; -import gr.thmmy.mthmmy.utils.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; import gr.thmmy.mthmmy.utils.parsing.NewParseTask; import gr.thmmy.mthmmy.utils.parsing.ParseException; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java index 907b1fb6..3621057f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java @@ -54,7 +54,7 @@ import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.ThmmyPage; import gr.thmmy.mthmmy.model.TopicItem; import gr.thmmy.mthmmy.utils.HTMLUtils; -import gr.thmmy.mthmmy.utils.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import gr.thmmy.mthmmy.viewmodel.TopicViewModel; import gr.thmmy.mthmmy.views.CustomLinearLayoutManager; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java index b277d2bd..96600b7b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/DeleteTask.java @@ -4,8 +4,8 @@ import org.jsoup.nodes.Document; import java.io.IOException; -import gr.thmmy.mthmmy.utils.NetworkResultCodes; -import gr.thmmy.mthmmy.utils.NetworkTask; +import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.networking.NetworkTask; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/RemoveVoteTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/RemoveVoteTask.java index f94ae027..484522c1 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/RemoveVoteTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/RemoveVoteTask.java @@ -2,8 +2,8 @@ package gr.thmmy.mthmmy.activities.topic.tasks; import org.jsoup.nodes.Document; -import gr.thmmy.mthmmy.utils.NetworkResultCodes; -import gr.thmmy.mthmmy.utils.NetworkTask; +import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.networking.NetworkTask; import okhttp3.Response; public class RemoveVoteTask extends NetworkTask { diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/SubmitVoteTask.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/SubmitVoteTask.java index ec3773f7..75dc37e3 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/SubmitVoteTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/tasks/SubmitVoteTask.java @@ -4,8 +4,8 @@ import org.jsoup.nodes.Document; import java.io.IOException; -import gr.thmmy.mthmmy.utils.NetworkResultCodes; -import gr.thmmy.mthmmy.utils.NetworkTask; +import gr.thmmy.mthmmy.utils.networking.NetworkResultCodes; +import gr.thmmy.mthmmy.utils.networking.NetworkTask; import okhttp3.MultipartBody; import okhttp3.OkHttpClient; import okhttp3.Request; diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java index 5adfd788..defaa6d3 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/base/BaseActivity.java @@ -463,8 +463,7 @@ public abstract class BaseActivity extends AppCompatActivity { private void updateDrawer() { if (drawer != null) { - if (!sessionManager.isLoggedIn()) //When logged out or if user is guest - { + if (!sessionManager.isLoggedIn()){ //When logged out or if user is guest drawer.removeItem(DOWNLOADS_ID); drawer.removeItem(UPLOAD_ID); loginLogoutItem.withName(R.string.login).withIcon(loginIcon); //Swap logout with login @@ -486,7 +485,6 @@ public abstract class BaseActivity extends AppCompatActivity { } accountHeader.updateProfile(profileDrawerItem); drawer.updateItem(loginLogoutItem); - } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/session/InvalidSessionException.java b/app/src/main/java/gr/thmmy/mthmmy/session/InvalidSessionException.java new file mode 100644 index 00000000..37bc2ddd --- /dev/null +++ b/app/src/main/java/gr/thmmy/mthmmy/session/InvalidSessionException.java @@ -0,0 +1,13 @@ +package gr.thmmy.mthmmy.session; + +public class InvalidSessionException extends RuntimeException { + public InvalidSessionException() {} + + public InvalidSessionException(String message) { + super(message); + } + + public InvalidSessionException(String message, Throwable cause) { + super(message, cause); + } +} diff --git a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java index 36490d56..e87cdf05 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -43,15 +43,16 @@ public class SessionManager { private static final String baseMarkAllAsReadLink = "https://www.thmmy.gr/smf/index.php?action=markasread;sa=all;sesc="; private static final String guestName = "Guest"; - //Response Codes - public static final int SUCCESS = 0; - public static final int FAILURE = 1; //Generic Error - public static final int WRONG_USER = 2; - public static final int WRONG_PASSWORD = 3; - public static final int CANCELLED = 4; - public static final int CONNECTION_ERROR = 5; - public static final int EXCEPTION = 6; - public static final int BANNED_USER = 7; + //Response Codes - make sure they do not overlap with NetworkResultCodes, just in case + public static final int SUCCESS = 20; + public static final int FAILURE = 21; //Generic Error + public static final int WRONG_USER = 22; + public static final int WRONG_PASSWORD = 23; + public static final int CANCELLED = 24; + public static final int CONNECTION_ERROR = 25; + public static final int EXCEPTION = 26; + public static final int BANNED_USER = 27; + public static final int INVALID_SESSION = 28; // Client & Cookies private final OkHttpClient client; @@ -63,12 +64,12 @@ public class SessionManager { private final SharedPreferences draftsPrefs; private static final String USERNAME = "Username"; private static final String USER_ID = "UserID"; - private static final String AVATAR_LINK = "AvatarLink"; - private static final String HAS_AVATAR = "HasAvatar"; + public static final String AVATAR_LINK = "AvatarLink"; + public static final String HAS_AVATAR = "HasAvatar"; private static final String SESC = "Sesc"; private static final String LOGOUT_LINK = "LogoutLink"; private static final String MARK_ALL_AS_READ_LINK = "MarkAllAsReadLink"; - private static final String LOGGED_IN = "LoggedIn"; + public static final String LOGGED_IN = "LoggedIn"; private static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault"; //Constructor @@ -224,8 +225,7 @@ public class SessionManager { Document document = Jsoup.parse(response.body().string()); Elements loginButton = document.select("[value=Login]"); //Attempt to find login button - if (!loginButton.isEmpty()) //If login button exists, logout was successful - { + if (!loginButton.isEmpty()){ //If login button exists, logout was successful Timber.i("Logout successful!"); return SUCCESS; } else { @@ -245,6 +245,16 @@ public class SessionManager { } } + public void clearSessionData() { + cookieJar.clear(); + sharedPrefs.edit().clear().apply(); //Clear session data + sharedPrefs.edit().putString(USERNAME, guestName).apply(); + sharedPrefs.edit().putInt(USER_ID, -1).apply(); + sharedPrefs.edit().putBoolean(LOGGED_IN, false).apply(); //User logs out + draftsPrefs.edit().clear().apply(); //Clear saved drafts + Timber.i("Session data cleared."); + } + public void refreshSescFromUrl(String url){ String sesc = extractSescFromLink(url); if(sesc!=null){ @@ -356,16 +366,6 @@ public class SessionManager { } - private void clearSessionData() { - cookieJar.clear(); - sharedPrefs.edit().clear().apply(); //Clear session data - sharedPrefs.edit().putString(USERNAME, guestName).apply(); - sharedPrefs.edit().putInt(USER_ID, -1).apply(); - sharedPrefs.edit().putBoolean(LOGGED_IN, false).apply(); //User logs out - draftsPrefs.edit().clear().apply(); //Clear saved drafts - Timber.i("Session data cleared."); - } - private void setLoginScreenAsDefault(boolean b){ sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, b).apply(); } @@ -421,7 +421,6 @@ public class SessionManager { return -1; } - @Nullable private String extractAvatarLink(@NonNull Document doc) { Elements avatar = doc.getElementsByClass("avatar"); @@ -461,5 +460,4 @@ public class SessionManager { return baseMarkAllAsReadLink + sesc; } //----------------------------------OTHER FUNCTIONS END----------------------------------------- - } \ No newline at end of file diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java b/app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java index 83e65285..8733bf8c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/Parcel.java @@ -1,7 +1,6 @@ package gr.thmmy.mthmmy.utils; public class Parcel { - private int resultCode; private T data; diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkResultCodes.java b/app/src/main/java/gr/thmmy/mthmmy/utils/networking/NetworkResultCodes.java similarity index 94% rename from app/src/main/java/gr/thmmy/mthmmy/utils/NetworkResultCodes.java rename to app/src/main/java/gr/thmmy/mthmmy/utils/networking/NetworkResultCodes.java index 90cd8771..958435e1 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkResultCodes.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/networking/NetworkResultCodes.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.utils; +package gr.thmmy.mthmmy.utils.networking; public class NetworkResultCodes { /** diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java b/app/src/main/java/gr/thmmy/mthmmy/utils/networking/NetworkTask.java similarity index 86% rename from app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java rename to app/src/main/java/gr/thmmy/mthmmy/utils/networking/NetworkTask.java index a8c42c8b..a12d5757 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/NetworkTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/networking/NetworkTask.java @@ -1,4 +1,4 @@ -package gr.thmmy.mthmmy.utils; +package gr.thmmy.mthmmy.utils.networking; import android.content.SharedPreferences; import android.preference.PreferenceManager; @@ -10,6 +10,10 @@ import java.io.IOException; import gr.thmmy.mthmmy.R; import gr.thmmy.mthmmy.base.BaseApplication; +import gr.thmmy.mthmmy.session.InvalidSessionException; +import gr.thmmy.mthmmy.session.SessionManager; +import gr.thmmy.mthmmy.utils.ExternalAsyncTask; +import gr.thmmy.mthmmy.utils.Parcel; import gr.thmmy.mthmmy.utils.crashreporting.CrashReporter; import gr.thmmy.mthmmy.utils.parsing.ParseException; import okhttp3.OkHttpClient; @@ -63,7 +67,12 @@ public abstract class NetworkTask extends ExternalAsyncTask .getString(R.string.pref_privacy_crashlytics_enable_key), false)) CrashReporter.reportForumInfo(Jsoup.parse(responseBodyString)); return new Parcel<>(NetworkResultCodes.PARSE_ERROR, null); - } catch (Exception e) { + } catch (InvalidSessionException ise) { + //TODO: Uncomment the lines below when UI is ready to auto-adjust to changes in session data + // BaseApplication.getInstance().getSessionManager().clearSessionData(); + // BaseApplication.getInstance().getSessionManager().guestLogin(); + return new Parcel<>(SessionManager.INVALID_SESSION, null); + }catch (Exception e) { Timber.e(e); return new Parcel<>(NetworkResultCodes.PERFORM_TASK_ERROR, null); } diff --git a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java index def14c2f..754e7825 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java +++ b/app/src/main/java/gr/thmmy/mthmmy/utils/parsing/NewParseTask.java @@ -2,7 +2,8 @@ package gr.thmmy.mthmmy.utils.parsing; import org.jsoup.nodes.Document; -import gr.thmmy.mthmmy.utils.NetworkTask; +import gr.thmmy.mthmmy.session.InvalidSessionException; +import gr.thmmy.mthmmy.utils.networking.NetworkTask; import okhttp3.Response; public abstract class NewParseTask extends NetworkTask { @@ -22,8 +23,10 @@ public abstract class NewParseTask extends NetworkTask { protected final T performTask(Document document, Response response) { try { return parse(document, response); + } catch (InvalidSessionException ise) { + throw ise; } catch (Exception e) { - throw new ParseException("Parse failed.", e); + throw new ParseException("Parsing failed", e); } } diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java index 8f8f4c3a..49f619c2 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -30,7 +30,7 @@ import gr.thmmy.mthmmy.model.Post; import gr.thmmy.mthmmy.model.TopicItem; import gr.thmmy.mthmmy.session.SessionManager; import gr.thmmy.mthmmy.utils.ExternalAsyncTask; -import gr.thmmy.mthmmy.utils.NetworkTask; +import gr.thmmy.mthmmy.utils.networking.NetworkTask; import gr.thmmy.mthmmy.utils.parsing.ParseHelpers; import timber.log.Timber; diff --git a/app/src/main/res/layout/fragment_unread.xml b/app/src/main/res/layout/fragment_unread.xml index 4d1d5506..3da5651c 100644 --- a/app/src/main/res/layout/fragment_unread.xml +++ b/app/src/main/res/layout/fragment_unread.xml @@ -61,6 +61,7 @@ android:layout_gravity="bottom|end" android:layout_marginEnd="@dimen/fab_margins" android:layout_marginBottom="@dimen/fab_margins" + android:visibility="gone" app:layout_behavior="gr.thmmy.mthmmy.utils.ui.ScrollAwareFABBehavior" app:srcCompat="@drawable/ic_mark_as_read" /> \ No newline at end of file