Browse Source

Experimental simplified SessionManager, background color changed to match the color of the animated gif

pull/24/head
Ezerous 8 years ago
parent
commit
15473af739
  1. 6
      app/src/main/java/gr/thmmy/mthmmy/activities/base/BaseActivity.java
  2. 3
      app/src/main/java/gr/thmmy/mthmmy/activities/main/MainActivity.java
  3. 6
      app/src/main/java/gr/thmmy/mthmmy/activities/main/forum/ForumFragment.java
  4. 24
      app/src/main/java/gr/thmmy/mthmmy/activities/profile/ProfileActivity.java
  5. 24
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java
  6. 35
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java
  7. 6
      app/src/main/res/values/colors.xml

6
app/src/main/java/gr/thmmy/mthmmy/activities/base/BaseActivity.java

@ -174,7 +174,7 @@ public abstract class BaseActivity extends AppCompatActivity
//Drawer Items //Drawer Items
homeItem = new PrimaryDrawerItem().withIdentifier(HOME_ID).withName(R.string.home).withIcon(homeIcon); 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); loginLogoutItem = new PrimaryDrawerItem().withIdentifier(LOG_ID).withName(R.string.login).withIcon(loginIcon).withSelectable(false);
else else
loginLogoutItem = new PrimaryDrawerItem().withIdentifier(LOG_ID).withName(R.string.logout).withIcon(logoutIcon).withSelectable(false); 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)) 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); Intent intent = new Intent(BaseActivity.this, LoginActivity.class);
startActivity(intent); startActivity(intent);
@ -258,7 +258,7 @@ public abstract class BaseActivity extends AppCompatActivity
{ {
if(drawer!=null) 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 loginLogoutItem.withName(R.string.login).withIcon(loginIcon); //Swap logout with login
profileDrawerItem.withName(sessionManager.getUsername()).withIcon(new IconicsDrawable(this) profileDrawerItem.withName(sessionManager.getUsername()).withIcon(new IconicsDrawable(this)

3
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.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_TITLE;
import static gr.thmmy.mthmmy.activities.topic.TopicActivity.EXTRAS_TOPIC_URL; 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 { 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); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
if (sessionManager.getLogStatus()== LOGGED_OUT) { //If not logged in if (sessionManager.isLoginScreenDefault()) {
//Go to login //Go to login
Intent intent = new Intent(MainActivity.this, LoginActivity.class); Intent intent = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent); startActivity(intent);

6
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() { forumAdapter.setExpandCollapseListener(new ExpandableRecyclerAdapter.ExpandCollapseListener() {
@Override @Override
public void onParentExpanded(int parentPosition) { public void onParentExpanded(int parentPosition) {
if(BaseActivity.getSessionManager().getLogStatus()== LOGGED_IN) if(BaseActivity.getSessionManager().isLoggedIn())
{ {
if(forumTask.getStatus()== AsyncTask.Status.RUNNING) if(forumTask.getStatus()== AsyncTask.Status.RUNNING)
forumTask.cancel(true); forumTask.cancel(true);
@ -118,7 +118,7 @@ public class ForumFragment extends BaseFragment
@Override @Override
public void onParentCollapsed(int parentPosition) { public void onParentCollapsed(int parentPosition) {
if(BaseActivity.getSessionManager().getLogStatus()== LOGGED_IN) if(BaseActivity.getSessionManager().isLoggedIn())
{ {
if(forumTask.getStatus()== AsyncTask.Status.RUNNING) if(forumTask.getStatus()== AsyncTask.Status.RUNNING)
forumTask.cancel(true); forumTask.cancel(true);
@ -212,7 +212,7 @@ public class ForumFragment extends BaseFragment
String categoryUrl = categoryElement.attr("href"); String categoryUrl = categoryElement.attr("href");
Category category = new Category(categoryElement.text(), categoryUrl); 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); category.setExpanded(true);
Elements boardsElements = categoryBlock.select("b [name]"); Elements boardsElements = categoryBlock.select("b [name]");

24
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.USERNAME_INDEX;
import static gr.thmmy.mthmmy.activities.profile.ProfileParser.parseProfileSummary; 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.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 <b>String</b> * Activity for user's profile. When creating an Intent of this activity you need to bundle a <b>String</b>
@ -102,25 +101,15 @@ public class ProfileActivity extends BaseActivity {
personalText = (TextView) findViewById(R.id.profile_activity_personal_text); personalText = (TextView) findViewById(R.id.profile_activity_personal_text);
mainContent = (LinearLayout) findViewById(R.id.profile_activity_content); 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.setEnabled(false);
replyFAB.setOnClickListener(new View.OnClickListener() { replyFAB.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
SharedPreferences sharedPrefs = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE); if (sessionManager.isLoggedIn()) {
int tmp_curr_status = sharedPrefs.getInt(LOGIN_STATUS, -1); //TODO
if (tmp_curr_status == -1) { //PM
Report.e(TAG, "Error while getting LOGIN_STATUS"); } else {
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) {
new AlertDialog.Builder(ProfileActivity.this) new AlertDialog.Builder(ProfileActivity.this)
.setMessage("You need to be logged in to sent a personal message!") .setMessage("You need to be logged in to sent a personal message!")
.setPositiveButton("Login", new DialogInterface.OnClickListener() { .setPositiveButton("Login", new DialogInterface.OnClickListener() {
@ -138,9 +127,6 @@ public class ProfileActivity extends BaseActivity {
} }
}) })
.show(); .show();
} else {
//TODO
//PM
} }
} }
}); });

24
app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicActivity.java

@ -37,7 +37,6 @@ import okhttp3.Request;
import okhttp3.Response; import okhttp3.Response;
import static gr.thmmy.mthmmy.session.SessionManager.LOGGED_IN; 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 <b>String</b> * Activity for topics. When creating an Intent of this activity you need to bundle a <b>String</b>
@ -126,25 +125,15 @@ public class TopicActivity extends BaseActivity {
recyclerView.setAdapter(new TopicAdapter(getApplicationContext(), progressBar, postsList, recyclerView.setAdapter(new TopicAdapter(getApplicationContext(), progressBar, postsList,
new TopicTask())); 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.setEnabled(false);
replyFAB.setOnClickListener(new View.OnClickListener() { replyFAB.setOnClickListener(new View.OnClickListener() {
@Override @Override
public void onClick(View view) { public void onClick(View view) {
SharedPreferences sharedPrefs = getSharedPreferences(SHARED_PREFS_NAME, MODE_PRIVATE); if (sessionManager.isLoggedIn()) {
int tmp_curr_status = sharedPrefs.getInt(LOGIN_STATUS, -1); //TODO
if (tmp_curr_status == -1) { //Reply
Report.e(TAG, "Error while getting LOGIN_STATUS"); } else {
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) {
new AlertDialog.Builder(TopicActivity.this) new AlertDialog.Builder(TopicActivity.this)
.setMessage("You need to be logged in to reply!") .setMessage("You need to be logged in to reply!")
.setPositiveButton("Login", new DialogInterface.OnClickListener() { .setPositiveButton("Login", new DialogInterface.OnClickListener() {
@ -162,9 +151,6 @@ public class TopicActivity extends BaseActivity {
} }
}) })
.show(); .show();
} else {
//TODO
//Reply
} }
} }
}); });

35
app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java

@ -1,6 +1,7 @@
package gr.thmmy.mthmmy.session; package gr.thmmy.mthmmy.session;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.support.annotation.Nullable;
import com.franmontiel.persistentcookiejar.PersistentCookieJar; import com.franmontiel.persistentcookiejar.PersistentCookieJar;
import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor; import com.franmontiel.persistentcookiejar.persistence.SharedPrefsCookiePersistor;
@ -49,11 +50,6 @@ public class SessionManager
public static final int CONNECTION_ERROR = 5; public static final int CONNECTION_ERROR = 5;
public static final int EXCEPTION = 6; 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 // Client & Cookies
private OkHttpClient client; private OkHttpClient client;
private PersistentCookieJar cookieJar; private PersistentCookieJar cookieJar;
@ -65,7 +61,8 @@ public class SessionManager
public static final String AVATAR_LINK = "AvatarLink"; public static final String AVATAR_LINK = "AvatarLink";
public static final String HAS_AVATAR = "HasAvatar"; public static final String HAS_AVATAR = "HasAvatar";
public static final String LOGOUT_LINK = "LogoutLink"; 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 //Constructor
public SessionManager(OkHttpClient client, PersistentCookieJar cookieJar, public SessionManager(OkHttpClient client, PersistentCookieJar cookieJar,
@ -124,7 +121,8 @@ public class SessionManager
setPersistentCookieSession(); //Store cookies setPersistentCookieSession(); //Store cookies
//Edit SharedPreferences, save session's data //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(); sharedPrefs.edit().putString(USERNAME, extractUserName(document)).apply();
String avatar = extractAvatarLink(document); String avatar = extractAvatarLink(document);
if (avatar!=null) if (avatar!=null)
@ -178,7 +176,7 @@ public class SessionManager
/** /**
* A function that checks the validity of the current saved session (if it exists). * 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 * 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 * (e.g. no internet connection), at least until a more thorough handling of different
* exceptions is implemented (if considered mandatory). * exceptions is implemented (if considered mandatory).
@ -189,17 +187,16 @@ public class SessionManager
{ {
Report.i(TAG, "Validating session..."); Report.i(TAG, "Validating session...");
//Check if user is currently logged in if(isLoggedIn())
int status = sharedPrefs.getInt(LOGIN_STATUS,LOGGED_OUT);
if(status==LOGGED_IN)
{ {
int loginResult = login(); int loginResult = login();
if(loginResult != FAILURE) if(loginResult != FAILURE)
return; return;
} }
else if(status==AS_GUEST) else if(isLoginScreenDefault())
return; return;
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, true).apply();
clearSessionData(); clearSessionData();
} }
@ -210,7 +207,7 @@ public class SessionManager
{ {
Report.i("TAG", "Continuing as a guest, as chosen by the user."); Report.i("TAG", "Continuing as a guest, as chosen by the user.");
clearSessionData(); 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); return sharedPrefs.getBoolean(HAS_AVATAR, false);
} }
public int getLogStatus() { public boolean isLoggedIn() {
return sharedPrefs.getInt(LOGIN_STATUS, LOGGED_OUT); return sharedPrefs.getBoolean(LOGGED_IN, false);
}
public boolean isLoginScreenDefault() {
return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true);
} }
//--------------------------------------GETTERS END--------------------------------------------- //--------------------------------------GETTERS END---------------------------------------------
@ -299,10 +300,11 @@ public class SessionManager
cookieJar.clear(); cookieJar.clear();
sharedPrefs.edit().clear().apply(); //Clear session data sharedPrefs.edit().clear().apply(); //Clear session data
sharedPrefs.edit().putString(USERNAME, guestName).apply(); 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."); Report.i(TAG,"Session data cleared.");
} }
@Nullable
private String extractUserName(Document doc) private String extractUserName(Document doc)
{ {
if (doc != null) { if (doc != null) {
@ -321,6 +323,7 @@ public class SessionManager
return null; return null;
} }
@Nullable
private String extractAvatarLink(Document doc) private String extractAvatarLink(Document doc)
{ {
if (doc != null) { if (doc != null) {

6
app/src/main/res/values/colors.xml

@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<resources> <resources>
<!-- <!--
When changing the colors be sure to also change lines 322 & 323 When changing the colors be sure to also change lines "color" and "background" from
in the style.css file! ".post, .personalmessage", in style.css
--> -->
<!--Dark--> <!--Dark-->
@ -13,7 +13,7 @@
<color name="accent">#26A69A</color> <color name="accent">#26A69A</color>
<color name="primary_text">#E7E7E7</color> <color name="primary_text">#E7E7E7</color>
<color name="secondary_text">#757575</color> <color name="secondary_text">#757575</color>
<color name="background">#303234</color> <color name="background">#323232</color>
<color name="card_background">#3C3F41</color> <color name="card_background">#3C3F41</color>
<color name="divider">#8B8B8B</color> <color name="divider">#8B8B8B</color>

Loading…
Cancel
Save