Browse Source

Refactoring

pull/70/head
Ezerous 5 years ago
parent
commit
2bdb37a2df
  1. 51
      app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java
  2. 48
      app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java
  3. 4
      app/src/main/res/values/strings.xml

51
app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java

@ -48,6 +48,7 @@ import timber.log.Timber;
import static gr.thmmy.mthmmy.activities.settings.SettingsActivity.DISPLAY_RELATIVE_TIME;
// TODO: Replace MultiDexApplication with Application after KitKat support is dropped
public class BaseApplication extends MultiDexApplication {
private static BaseApplication baseApplication; //BaseApplication singleton
@ -63,9 +64,6 @@ public class BaseApplication extends MultiDexApplication {
private boolean displayRelativeTime;
//TODO: maybe use PreferenceManager.getDefaultSharedPreferences here as well?
private static final String SHARED_PREFS = "ThmmySharedPrefs";
//Display Metrics
private static float widthDp;
private static int widthPxl, heightPxl;
@ -84,10 +82,32 @@ public class BaseApplication extends MultiDexApplication {
Timber.plant(new Timber.DebugTree());
//Shared Preferences
SharedPreferences sharedPrefs = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE);
SharedPreferences sessionSharedPrefs = getSharedPreferences(getString(R.string.session_shared_prefs), MODE_PRIVATE);
SharedPreferences settingsSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences draftsPrefs = getSharedPreferences(getString(R.string.pref_topic_drafts_key), MODE_PRIVATE);
initFirebase(settingsSharedPrefs);
SharedPrefsCookiePersistor sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(getApplicationContext());
PersistentCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor);
initOkHttp(cookieJar);
sessionManager = new SessionManager(client, cookieJar, sharedPrefsCookiePersistor, sessionSharedPrefs, draftsPrefs);
//Sets up upload service
UploadService.NAMESPACE = BuildConfig.APPLICATION_ID;
UploadService.HTTP_STACK = new OkHttpStack(client);
//Initialize and create the image loader logic for the drawer
initDrawerImageLoader();
setDisplayMetrics();
displayRelativeTime = settingsSharedPrefs.getBoolean(DISPLAY_RELATIVE_TIME, true);
}
private void initFirebase(SharedPreferences settingsSharedPrefs){
if (settingsSharedPrefs.getBoolean(getString(R.string.pref_privacy_crashlytics_enable_key), false)){
Timber.i("Starting app with Firebase Crashlytics enabled.");
setFirebaseCrashlyticsEnabled(true);
@ -105,9 +125,10 @@ public class BaseApplication extends MultiDexApplication {
Timber.i("Starting app with Firebase Analytics enabled.");
else
Timber.i("Starting app with Firebase Analytics disabled.");
}
private void initOkHttp(PersistentCookieJar cookieJar){
SharedPrefsCookiePersistor sharedPrefsCookiePersistor = new SharedPrefsCookiePersistor(getApplicationContext());
PersistentCookieJar cookieJar = new PersistentCookieJar(new SetCookieCache(), sharedPrefsCookiePersistor);
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.cookieJar(cookieJar)
.addInterceptor(chain -> {
@ -143,14 +164,9 @@ public class BaseApplication extends MultiDexApplication {
builder.addInterceptor(new OkHttpProfilerInterceptor());
client = builder.build();
}
sessionManager = new SessionManager(client, cookieJar, sharedPrefsCookiePersistor, sharedPrefs, draftsPrefs);
//Sets up upload service
UploadService.NAMESPACE = BuildConfig.APPLICATION_ID;
UploadService.HTTP_STACK = new OkHttpStack(client);
//Initialize and create the image loader logic
private void initDrawerImageLoader(){
DrawerImageLoader.init(new AbstractDrawerImageLoader() {
@Override
public void set(ImageView imageView, Uri uri, Drawable placeholder, String tag) {
@ -167,24 +183,25 @@ public class BaseApplication extends MultiDexApplication {
if (DrawerImageLoader.Tags.PROFILE.name().equals(tag)) {
return new IconicsDrawable(ctx).icon(FontAwesome.Icon.faw_user)
.paddingDp(10)
.color(ContextCompat.getColor(ctx, R.color.primary_light))
.color(ContextCompat.getColor(ctx, R.color.iron))
.backgroundColor(ContextCompat.getColor(ctx, R.color.primary));
}
return super.placeholder(ctx, tag);
}
});
}
private void setDisplayMetrics(){
DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
widthPxl = displayMetrics.widthPixels;
widthDp = widthPxl / displayMetrics.density;
heightPxl = displayMetrics.heightPixels;
displayRelativeTime = settingsSharedPrefs.getBoolean(DISPLAY_RELATIVE_TIME, true);
}
//Getters
//-------------------- Getters --------------------
public Context getContext() {
return getApplicationContext();
}

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

@ -60,25 +60,25 @@ public class SessionManager {
private final SharedPrefsCookiePersistor cookiePersistor; //Used to explicitly edit cookies in cookieJar
//Shared Preferences & its keys
private final SharedPreferences sharedPrefs;
private final SharedPreferences sessionSharedPrefs;
private final SharedPreferences draftsPrefs;
private static final String USERNAME = "Username";
private static final String USER_ID = "UserID";
public static final String AVATAR_LINK = "AvatarLink";
public static final String HAS_AVATAR = "HasAvatar";
private static final String AVATAR_LINK = "AvatarLink";
private 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";
public static final String LOGGED_IN = "LoggedIn";
private static final String LOGGED_IN = "LoggedIn";
private static final String LOGIN_SCREEN_AS_DEFAULT = "LoginScreenAsDefault";
//Constructor
public SessionManager(OkHttpClient client, PersistentCookieJar cookieJar,
SharedPrefsCookiePersistor cookiePersistor, SharedPreferences sharedPrefs, SharedPreferences draftsPrefs) {
SharedPrefsCookiePersistor cookiePersistor, SharedPreferences sessionSharedPrefs, SharedPreferences draftsPrefs) {
this.client = client;
this.cookiePersistor = cookiePersistor;
this.cookieJar = cookieJar;
this.sharedPrefs = sharedPrefs;
this.sessionSharedPrefs = sessionSharedPrefs;
this.draftsPrefs = draftsPrefs;
}
@ -124,7 +124,7 @@ public class SessionManager {
setPersistentCookieSession(); //Store cookies
//Edit SharedPreferences, save session's data
SharedPreferences.Editor editor = sharedPrefs.edit();
SharedPreferences.Editor editor = sessionSharedPrefs.edit();
setLoginScreenAsDefault(false);
editor.putBoolean(LOGGED_IN, true);
editor.putString(USERNAME, extractUserName(document));
@ -247,10 +247,10 @@ 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
sessionSharedPrefs.edit().clear().apply(); //Clear session data
sessionSharedPrefs.edit().putString(USERNAME, guestName).apply();
sessionSharedPrefs.edit().putInt(USER_ID, -1).apply();
sessionSharedPrefs.edit().putBoolean(LOGGED_IN, false).apply(); //User logs out
draftsPrefs.edit().clear().apply(); //Clear saved drafts
Timber.i("Session data cleared.");
}
@ -267,15 +267,15 @@ public class SessionManager {
//---------------------------------------GETTERS------------------------------------------------
public String getUsername() {
return sharedPrefs.getString(USERNAME, USERNAME);
return sessionSharedPrefs.getString(USERNAME, USERNAME);
}
public int getUserId() {
return sharedPrefs.getInt(USER_ID, -1);
return sessionSharedPrefs.getInt(USER_ID, -1);
}
public String getAvatarLink() {
return sharedPrefs.getString(AVATAR_LINK, AVATAR_LINK);
return sessionSharedPrefs.getString(AVATAR_LINK, AVATAR_LINK);
}
public Cookie getThmmyCookie() {
@ -288,7 +288,7 @@ public class SessionManager {
}
public String getMarkAllAsReadLink() {
String markAsReadLink = sharedPrefs.getString(MARK_ALL_AS_READ_LINK, null);
String markAsReadLink = sessionSharedPrefs.getString(MARK_ALL_AS_READ_LINK, null);
if(markAsReadLink == null){ //For older versions, extract it from logout link (otherwise user would have to login again)
String sesc = extractSescFromLink(getLogoutLink());
if(sesc!=null) {
@ -302,38 +302,38 @@ public class SessionManager {
}
private String getLogoutLink() {
return sharedPrefs.getString(LOGOUT_LINK, null);
return sessionSharedPrefs.getString(LOGOUT_LINK, null);
}
public boolean hasAvatar() {
return sharedPrefs.getBoolean(HAS_AVATAR, false);
return sessionSharedPrefs.getBoolean(HAS_AVATAR, false);
}
public boolean isLoggedIn() {
return sharedPrefs.getBoolean(LOGGED_IN, false);
return sessionSharedPrefs.getBoolean(LOGGED_IN, false);
}
public boolean isLoginScreenDefault() {
return sharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true);
return sessionSharedPrefs.getBoolean(LOGIN_SCREEN_AS_DEFAULT, true);
}
//--------------------------------------GETTERS END---------------------------------------------
//---------------------------------------SETTERS------------------------------------------------
private void setSesc(String sesc){
SharedPreferences.Editor editor = sharedPrefs.edit();
SharedPreferences.Editor editor = sessionSharedPrefs.edit();
editor.putString(SESC, sesc);
editor.apply();
}
private void setMarkAsReadLink(String markAllAsReadLink){
SharedPreferences.Editor editor = sharedPrefs.edit();
SharedPreferences.Editor editor = sessionSharedPrefs.edit();
editor.putString(MARK_ALL_AS_READ_LINK, markAllAsReadLink);
editor.apply();
}
private void setLogoutLink(String logoutLink){
SharedPreferences.Editor editor = sharedPrefs.edit();
SharedPreferences.Editor editor = sessionSharedPrefs.edit();
editor.putString(LOGOUT_LINK, logoutLink);
editor.apply();
}
@ -367,7 +367,7 @@ public class SessionManager {
}
private void setLoginScreenAsDefault(boolean b){
sharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, b).apply();
sessionSharedPrefs.edit().putBoolean(LOGIN_SCREEN_AS_DEFAULT, b).apply();
}
@NonNull
@ -448,7 +448,7 @@ public class SessionManager {
if (matcher.find())
return matcher.group(1);
}
Timber.e(new ParseException("Parsing failed(extractSescFromLogoutLink)"),"ParseException");
Timber.e(new ParseException("Parsing failed(extractSescFromLink)"),"ParseException");
return null;
}

4
app/src/main/res/values/strings.xml

@ -233,4 +233,8 @@
<string name="new_topic_toolbar">New topic</string>
<string name="create_topic">Create topic</string>
<string name="link_copied_msg">Link copied</string>
<!--SessionManager-->
<string name="session_shared_prefs">SessionSharedPrefs</string>
</resources>

Loading…
Cancel
Save