From 15473af7391aba866993641206c9cd277322a7ee Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sun, 1 Jan 2017 22:17:22 +0200 Subject: [PATCH] Experimental simplified SessionManager, background color changed to match the color of the animated gif --- .../mthmmy/activities/base/BaseActivity.java | 6 ++-- .../mthmmy/activities/main/MainActivity.java | 3 +- .../activities/main/forum/ForumFragment.java | 6 ++-- .../activities/profile/ProfileActivity.java | 24 +++---------- .../activities/topic/TopicActivity.java | 24 +++---------- .../thmmy/mthmmy/session/SessionManager.java | 35 ++++++++++--------- app/src/main/res/values/colors.xml | 6 ++-- 7 files changed, 39 insertions(+), 65 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/base/BaseActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/base/BaseActivity.java index d7f50bb2..24242eef 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/base/BaseActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/base/BaseActivity.java @@ -174,7 +174,7 @@ public abstract class BaseActivity extends AppCompatActivity //Drawer Items homeItem = new PrimaryDrawerItem().withIdentifier(HOME_ID).withName(R.string.home).withIcon(homeIcon); - if (sessionManager.getLogStatus()!= LOGGED_IN) //When logged out or if user is guest + if (!sessionManager.isLoggedIn()) //When logged out loginLogoutItem = new PrimaryDrawerItem().withIdentifier(LOG_ID).withName(R.string.login).withIcon(loginIcon).withSelectable(false); else loginLogoutItem = new PrimaryDrawerItem().withIdentifier(LOG_ID).withName(R.string.logout).withIcon(logoutIcon).withSelectable(false); @@ -218,7 +218,7 @@ public abstract class BaseActivity extends AppCompatActivity } else if(drawerItem.equals(LOG_ID)) { - if (sessionManager.getLogStatus()!= LOGGED_IN) //When logged out or if user is guest + if (!sessionManager.isLoggedIn()) //When logged out or if user is guest { Intent intent = new Intent(BaseActivity.this, LoginActivity.class); startActivity(intent); @@ -258,7 +258,7 @@ public abstract class BaseActivity extends AppCompatActivity { if(drawer!=null) { - if (sessionManager.getLogStatus()!= LOGGED_IN) //When logged out or if user is guest + if (!sessionManager.isLoggedIn()) //When logged out or if user is guest { loginLogoutItem.withName(R.string.login).withIcon(loginIcon); //Swap logout with login profileDrawerItem.withName(sessionManager.getUsername()).withIcon(new IconicsDrawable(this) diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java index 7be5d9f9..9a1e756d 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java @@ -24,7 +24,6 @@ import static gr.thmmy.mthmmy.activities.board.BoardActivity.EXTRAS_BOARD_TITLE; import static gr.thmmy.mthmmy.activities.board.BoardActivity.EXTRAS_BOARD_URL; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.EXTRAS_TOPIC_TITLE; import static gr.thmmy.mthmmy.activities.topic.TopicActivity.EXTRAS_TOPIC_URL; -import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_OUT; public class MainActivity extends BaseActivity implements RecentFragment.RecentFragmentInteractionListener, ForumFragment.ForumFragmentInteractionListener { @@ -38,7 +37,7 @@ public class MainActivity extends BaseActivity implements RecentFragment.RecentF super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - if (sessionManager.getLogStatus()== LOGGED_OUT) { //If not logged in + if (sessionManager.isLoginScreenDefault()) { //Go to login Intent intent = new Intent(MainActivity.this, LoginActivity.class); startActivity(intent); 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 12b281f8..416c75a9 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 @@ -106,7 +106,7 @@ public class ForumFragment extends BaseFragment forumAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() { @Override public void onParentExpanded(int parentPosition) { - if(BaseActivity.getSessionManager().getLogStatus()== LOGGED_IN) + if(BaseActivity.getSessionManager().isLoggedIn()) { if(forumTask.getStatus()== AsyncTask.Status.RUNNING) forumTask.cancel(true); @@ -118,7 +118,7 @@ public class ForumFragment extends BaseFragment @Override public void onParentCollapsed(int parentPosition) { - if(BaseActivity.getSessionManager().getLogStatus()== LOGGED_IN) + if(BaseActivity.getSessionManager().isLoggedIn()) { if(forumTask.getStatus()== AsyncTask.Status.RUNNING) forumTask.cancel(true); @@ -212,7 +212,7 @@ public class ForumFragment extends BaseFragment String categoryUrl = categoryElement.attr("href"); Category category = new Category(categoryElement.text(), categoryUrl); - if(categoryUrl.contains("sa=collapse")|| BaseActivity.getSessionManager().getLogStatus()!= LOGGED_IN) + if(categoryUrl.contains("sa=collapse")|| !BaseActivity.getSessionManager().isLoggedIn()) { category.setExpanded(true); Elements boardsElements = categoryBlock.select("b [name]"); 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 ab984d39..e48ac81a 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 @@ -43,7 +43,6 @@ import static gr.thmmy.mthmmy.activities.profile.ProfileParser.THUMBNAIL_URL_IND import static gr.thmmy.mthmmy.activities.profile.ProfileParser.USERNAME_INDEX; import static gr.thmmy.mthmmy.activities.profile.ProfileParser.parseProfileSummary; import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_IN; -import static gr.thmmy.mthmmy.session.SessionManager.LOGIN_STATUS; /** * Activity for user's profile. When creating an Intent of this activity you need to bundle a String @@ -102,25 +101,15 @@ public class ProfileActivity extends BaseActivity { personalText = (TextView) findViewById(R.id.profile_activity_personal_text); mainContent = (LinearLayout) findViewById(R.id.profile_activity_content); - replyFAB = (FloatingActionButton) findViewById(R.id.profile_fab); + replyFAB = (FloatingActionButton) findViewById(R.id.profile_fab); //TODO hide fab while logged out replyFAB.setEnabled(false); replyFAB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - SharedPreferences sharedPrefs = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE); - int tmp_curr_status = sharedPrefs.getInt(LOGIN_STATUS, -1); - if (tmp_curr_status == -1) { - Report.e(TAG, "Error while getting LOGIN_STATUS"); - new AlertDialog.Builder(ProfileActivity.this) - .setTitle("ERROR!") - .setMessage("An error occurred while trying to find your LOGIN_STATUS.") - .setNeutralButton("Dismiss", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - } - }) - .show(); - } else if (tmp_curr_status != LOGGED_IN) { + if (sessionManager.isLoggedIn()) { + //TODO + //PM + } else { new AlertDialog.Builder(ProfileActivity.this) .setMessage("You need to be logged in to sent a personal message!") .setPositiveButton("Login", new DialogInterface.OnClickListener() { @@ -138,9 +127,6 @@ public class ProfileActivity extends BaseActivity { } }) .show(); - } else { - //TODO - //PM } } }); 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 bd8ba2e9..a2573910 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 @@ -37,7 +37,6 @@ import okhttp3.Request; import okhttp3.Response; import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_IN; -import static gr.thmmy.mthmmy.session.SessionManager.LOGIN_STATUS; /** * Activity for topics. When creating an Intent of this activity you need to bundle a String @@ -126,25 +125,15 @@ public class TopicActivity extends BaseActivity { recyclerView.setAdapter(new TopicAdapter(getApplicationContext(), progressBar, postsList, new TopicTask())); - replyFAB = (FloatingActionButton) findViewById(R.id.topic_fab); + replyFAB = (FloatingActionButton) findViewById(R.id.topic_fab); //TODO hide fab while logged out replyFAB.setEnabled(false); replyFAB.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { - SharedPreferences sharedPrefs = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE); - int tmp_curr_status = sharedPrefs.getInt(LOGIN_STATUS, -1); - if (tmp_curr_status == -1) { - Report.e(TAG, "Error while getting LOGIN_STATUS"); - new AlertDialog.Builder(TopicActivity.this) - .setTitle("ERROR!") - .setMessage("An error occurred while trying to find your LOGIN_STATUS.") - .setNeutralButton("Dismiss", new DialogInterface.OnClickListener() { - @Override - public void onClick(DialogInterface dialogInterface, int i) { - } - }) - .show(); - } else if (tmp_curr_status != LOGGED_IN) { + if (sessionManager.isLoggedIn()) { + //TODO + //Reply + } else { new AlertDialog.Builder(TopicActivity.this) .setMessage("You need to be logged in to reply!") .setPositiveButton("Login", new DialogInterface.OnClickListener() { @@ -162,9 +151,6 @@ public class TopicActivity extends BaseActivity { } }) .show(); - } else { - //TODO - //Reply } } }); 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 7e16f0a6..16ee1d0f 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java @@ -1,6 +1,7 @@ package gr.thmmy.mthmmy.session; import android.content.SharedPreferences; +import android.support.annotation.Nullable; import com.franmontiel.persistentcookiejar.PersistentCookieJar; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; @@ -49,11 +50,6 @@ public class SessionManager public static final int CONNECTION_ERROR = 5; public static final int EXCEPTION = 6; - //Login status codes - public static final int LOGGED_OUT = 0; - public static final int LOGGED_IN = 1; //Logged in (as a normal user) - public static final int AS_GUEST = 2; //User chose to continue as guest - // Client & Cookies private OkHttpClient client; private PersistentCookieJar cookieJar; @@ -65,7 +61,8 @@ public class SessionManager public static final String AVATAR_LINK = "AvatarLink"; public static final String HAS_AVATAR = "HasAvatar"; public static final String LOGOUT_LINK = "LogoutLink"; - public static final String LOGIN_STATUS = "IsLoggedIn"; + public static final String LOGGED_IN = "LoggedIn"; + public static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault"; //Constructor public SessionManager(OkHttpClient client, PersistentCookieJar cookieJar, @@ -124,7 +121,8 @@ public class SessionManager setPersistentCookieSession(); //Store cookies //Edit SharedPreferences, save session's data - sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_IN).apply(); + sharedPrefs.edit().putBoolean(LOGGED_IN, true).apply(); + sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, false).apply(); sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply(); String avatar = extractAvatarLink(document); if (avatar!=null) @@ -178,7 +176,7 @@ public class SessionManager /** * A function that checks the validity of the current saved session (if it exists). - * If LOGIN_STATUS is true, it will call login() with cookies. On failure, this can only return + * If isLoggedIn() is true, it will call login() with cookies. On failure, this can only return * the code FAILURE. CANCELLED, CONNECTION_ERROR and EXCEPTION are simply considered a SUCCESS * (e.g. no internet connection), at least until a more thorough handling of different * exceptions is implemented (if considered mandatory). @@ -189,17 +187,16 @@ public class SessionManager { Report.i(TAG, "Validating session..."); - //Check if user is currently logged in - int status = sharedPrefs.getInt(LOGIN_STATUS,LOGGED_OUT); - if(status==LOGGED_IN) + if(isLoggedIn()) { int loginResult = login(); if(loginResult != FAILURE) return; } - else if(status==AS_GUEST) + else if(isLoginScreenDefault()) return; + sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, true).apply(); clearSessionData(); } @@ -210,7 +207,7 @@ public class SessionManager { Report.i("TAG", "Continuing as a guest, as chosen by the user."); clearSessionData(); - sharedPrefs.edit().putInt(LOGIN_STATUS, AS_GUEST).apply(); + sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, false).apply(); } @@ -266,8 +263,12 @@ public class SessionManager return sharedPrefs.getBoolean(HAS_AVATAR, false); } - public int getLogStatus() { - return sharedPrefs.getInt(LOGIN_STATUS, LOGGED_OUT); + public boolean isLoggedIn() { + return sharedPrefs.getBoolean(LOGGED_IN, false); + } + + public boolean isLoginScreenDefault() { + return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true); } //--------------------------------------GETTERS END--------------------------------------------- @@ -299,10 +300,11 @@ public class SessionManager cookieJar.clear(); sharedPrefs.edit().clear().apply(); //Clear session data sharedPrefs.edit().putString(USERNAME, guestName).apply(); - sharedPrefs.edit().putInt(LOGIN_STATUS, LOGGED_OUT).apply(); //User logs out + sharedPrefs.edit().putBoolean(LOGGED_IN, false).apply(); //User logs out Report.i(TAG,"Session data cleared."); } + @Nullable private String extractUserName(Document doc) { if (doc != null) { @@ -321,6 +323,7 @@ public class SessionManager return null; } + @Nullable private String extractAvatarLink(Document doc) { if (doc != null) { diff --git a/app/src/main/res/values/colors.xml b/app/src/main/res/values/colors.xml index 9194ce44..1afb6d89 100644 --- a/app/src/main/res/values/colors.xml +++ b/app/src/main/res/values/colors.xml @@ -1,8 +1,8 @@ @@ -13,7 +13,7 @@ #26A69A #E7E7E7 #757575 - #303234 + #323232 #3C3F41 #8B8B8B