Browse Source

Refactoring

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

53
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; 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 { public class BaseApplication extends MultiDexApplication {
private static BaseApplication baseApplication; //BaseApplication singleton private static BaseApplication baseApplication; //BaseApplication singleton
@ -63,9 +64,6 @@ public class BaseApplication extends MultiDexApplication {
private boolean displayRelativeTime; private boolean displayRelativeTime;
//TODO: maybe use PreferenceManager.getDefaultSharedPreferences here as well?
private static final String SHARED_PREFS = "ThmmySharedPrefs";
//Display Metrics //Display Metrics
private static float widthDp; private static float widthDp;
private static int widthPxl, heightPxl; private static int widthPxl, heightPxl;
@ -84,10 +82,32 @@ public class BaseApplication extends MultiDexApplication {
Timber.plant(new Timber.DebugTree()); Timber.plant(new Timber.DebugTree());
//Shared Preferences //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 settingsSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences draftsPrefs = getSharedPreferences(getString(R.string.pref_topic_drafts_key), MODE_PRIVATE); 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)){ if (settingsSharedPrefs.getBoolean(getString(R.string.pref_privacy_crashlytics_enable_key), false)){
Timber.i("Starting app with Firebase Crashlytics enabled."); Timber.i("Starting app with Firebase Crashlytics enabled.");
setFirebaseCrashlyticsEnabled(true); setFirebaseCrashlyticsEnabled(true);
@ -105,9 +125,10 @@ public class BaseApplication extends MultiDexApplication {
Timber.i("Starting app with Firebase Analytics enabled."); Timber.i("Starting app with Firebase Analytics enabled.");
else else
Timber.i("Starting app with Firebase Analytics disabled."); 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() OkHttpClient.Builder builder = new OkHttpClient.Builder()
.cookieJar(cookieJar) .cookieJar(cookieJar)
.addInterceptor(chain -> { .addInterceptor(chain -> {
@ -143,14 +164,9 @@ public class BaseApplication extends MultiDexApplication {
builder.addInterceptor(new OkHttpProfilerInterceptor()); builder.addInterceptor(new OkHttpProfilerInterceptor());
client = builder.build(); client = builder.build();
}
sessionManager = new SessionManager(client, cookieJar, sharedPrefsCookiePersistor, sharedPrefs, draftsPrefs); private void initDrawerImageLoader(){
//Sets up upload service
UploadService.NAMESPACE = BuildConfig.APPLICATION_ID;
UploadService.HTTP_STACK = new OkHttpStack(client);
//Initialize and create the image loader logic
DrawerImageLoader.init(new AbstractDrawerImageLoader() { DrawerImageLoader.init(new AbstractDrawerImageLoader() {
@Override @Override
public void set(ImageView imageView, Uri uri, Drawable placeholder, String tag) { 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)) { if (DrawerImageLoader.Tags.PROFILE.name().equals(tag)) {
return new IconicsDrawable(ctx).icon(FontAwesome.Icon.faw_user) return new IconicsDrawable(ctx).icon(FontAwesome.Icon.faw_user)
.paddingDp(10) .paddingDp(10)
.color(ContextCompat.getColor(ctx, R.color.primary_light)) .color(ContextCompat.getColor(ctx, R.color.iron))
.backgroundColor(ContextCompat.getColor(ctx, R.color.primary)); .backgroundColor(ContextCompat.getColor(ctx, R.color.primary));
} }
return super.placeholder(ctx, tag); return super.placeholder(ctx, tag);
} }
}); });
}
private void setDisplayMetrics(){
DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics(); DisplayMetrics displayMetrics = getApplicationContext().getResources().getDisplayMetrics();
widthPxl = displayMetrics.widthPixels; widthPxl = displayMetrics.widthPixels;
widthDp = widthPxl / displayMetrics.density; widthDp = widthPxl / displayMetrics.density;
heightPxl = displayMetrics.heightPixels; heightPxl = displayMetrics.heightPixels;
displayRelativeTime = settingsSharedPrefs.getBoolean(DISPLAY_RELATIVE_TIME, true);
} }
//Getters
//-------------------- Getters --------------------
public Context getContext() { public Context getContext() {
return getApplicationContext(); return getApplicationContext();
} }
@ -213,7 +230,7 @@ public class BaseApplication extends MultiDexApplication {
return displayRelativeTime; return displayRelativeTime;
} }
//--------------------Firebase-------------------- //-------------------- Firebase --------------------
public void logFirebaseAnalyticsEvent(String event, Bundle params) { public void logFirebaseAnalyticsEvent(String event, Bundle params) {
firebaseAnalytics.logEvent(event, params); firebaseAnalytics.logEvent(event, params);

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

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

@ -209,7 +209,7 @@
<string name="pref_title_privacy_analytics_enable">Analytics data reports</string> <string name="pref_title_privacy_analytics_enable">Analytics data reports</string>
<string name="pref_summary_privacy_analytics_enable">Automatically send us anonymized data for analytical purposes</string> <string name="pref_summary_privacy_analytics_enable">Automatically send us anonymized data for analytical purposes</string>
<!-- EditorView --> <!--EditorView -->
<string name="black">Black</string> <string name="black">Black</string>
<string name="red">Red</string> <string name="red">Red</string>
<string name="yellow">Yellow</string> <string name="yellow">Yellow</string>
@ -229,8 +229,12 @@
<string name="dialog_link_text_hint">Link text</string> <string name="dialog_link_text_hint">Link text</string>
<string name="input_field_required">Required</string> <string name="input_field_required">Required</string>
<!-- New topic activity --> <!--New topic activity-->
<string name="new_topic_toolbar">New topic</string> <string name="new_topic_toolbar">New topic</string>
<string name="create_topic">Create topic</string> <string name="create_topic">Create topic</string>
<string name="link_copied_msg">Link copied</string> <string name="link_copied_msg">Link copied</string>
<!--SessionManager-->
<string name="session_shared_prefs">SessionSharedPrefs</string>
</resources> </resources>

Loading…
Cancel
Save