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

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.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);

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() {
@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]");

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.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 <b>String</b>
@ -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
}
}
});

24
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 <b>String</b>
@ -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
}
}
});

35
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) {

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

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

Loading…
Cancel
Save