From 2bdb37a2dfcadc7a74aa770a2f2b08a9486d08d8 Mon Sep 17 00:00:00 2001 From: Ezerous Date: Sat, 27 Jun 2020 12:51:24 +0300 Subject: [PATCH] Refactoring --- .../gr/thmmy/mthmmy/base/BaseApplication.java | 59 ++++++++++++------- .../thmmy/mthmmy/session/SessionManager.java | 48 +++++++-------- app/src/main/res/values/strings.xml | 8 ++- 3 files changed, 68 insertions(+), 47 deletions(-) diff --git a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java b/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java index 51d01bba..390faf5c 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/base/BaseApplication.java +++ b/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 -> { @@ -115,9 +136,9 @@ public class BaseApplication extends MultiDexApplication { HttpUrl oldUrl = chain.request().url(); if (Objects.equals(chain.request().url().host(), "www.thmmy.gr") && !oldUrl.toString().contains("theme=4")) { - //Probably works but needs more testing: - HttpUrl newUrl = oldUrl.newBuilder().addQueryParameter("theme", "4").build(); - request = request.newBuilder().url(newUrl).build(); + //Probably works but needs more testing: + HttpUrl newUrl = oldUrl.newBuilder().addQueryParameter("theme", "4").build(); + request = request.newBuilder().url(newUrl).build(); } return chain.proceed(request); }) @@ -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(); } @@ -213,7 +230,7 @@ public class BaseApplication extends MultiDexApplication { return displayRelativeTime; } - //--------------------Firebase-------------------- + //-------------------- Firebase -------------------- public void logFirebaseAnalyticsEvent(String event, Bundle params) { firebaseAnalytics.logEvent(event, params); 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 e87cdf05..2c5cc42b 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/session/SessionManager.java +++ b/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; } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index d1c69acb..f881b88d 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -209,7 +209,7 @@ Analytics data reports Automatically send us anonymized data for analytical purposes - + Black Red Yellow @@ -229,8 +229,12 @@ Link text Required - + New topic Create topic Link copied + + + SessionSharedPrefs +