[tor-commits] [snowflake-mobile/master] Moved SharedPreferences to constants to increase cohesion in presenter

cohosh at torproject.org cohosh at torproject.org
Thu Jul 30 14:47:12 UTC 2020


commit 3c049a864187e1fc7740f0bcd5947a9e5f354d22
Author: Hashik Donthineni <HashikDonthineni at gmail.com>
Date:   Sat Jul 25 18:40:14 2020 +0530

    Moved SharedPreferences to constants to increase cohesion in presenter
---
 .../torproject/snowflake/MyPersistentService.java  | 11 +++++-----
 .../constants/AppPreferenceConstants.java          |  8 ++++++++
 .../snowflake/models/MainActivityModel.java        | 24 +++++++++++-----------
 .../presenters/MainActivityPresenter.java          | 11 +++++-----
 .../main/res/values/strings_preference_keys.xml    |  4 ----
 5 files changed, 32 insertions(+), 26 deletions(-)

diff --git a/app/src/main/java/org/torproject/snowflake/MyPersistentService.java b/app/src/main/java/org/torproject/snowflake/MyPersistentService.java
index c8c2304..2444255 100644
--- a/app/src/main/java/org/torproject/snowflake/MyPersistentService.java
+++ b/app/src/main/java/org/torproject/snowflake/MyPersistentService.java
@@ -16,6 +16,7 @@ import androidx.annotation.Nullable;
 
 import org.jetbrains.annotations.NotNull;
 import org.json.JSONException;
+import org.torproject.snowflake.constants.AppPreferenceConstants;
 import org.torproject.snowflake.constants.BrokerConstants;
 import org.torproject.snowflake.constants.ForegroundServiceConstants;
 import org.torproject.snowflake.exceptions.EmptySIDException;
@@ -80,7 +81,7 @@ public class MyPersistentService extends Service {
     public int onStartCommand(Intent intent, int flags, int startId) {
         Log.d(TAG, "onStartCommand: executed with startId: " + startId);
         sharedPreferences = getSharedPreferences(getString(R.string.sharedpreference_file), MODE_PRIVATE);
-        isServiceStarted = sharedPreferences.getBoolean(getString(R.string.is_service_running_bool_key), false);
+        isServiceStarted = sharedPreferences.getBoolean(AppPreferenceConstants.IS_SERVICE_RUNNING_KEY, false);
 
         if (intent != null) {
             if (intent.getAction().equals(ForegroundServiceConstants.ACTION_START))
@@ -132,10 +133,10 @@ public class MyPersistentService extends Service {
 
         if (setState == ForegroundServiceConstants.SERVICE_RUNNING) {
             isServiceStarted = true;
-            editor.putBoolean(getString(R.string.is_service_running_bool_key), true);
+            editor.putBoolean(AppPreferenceConstants.IS_SERVICE_RUNNING_KEY, true);
         } else {
             isServiceStarted = false;
-            editor.putBoolean(getString(R.string.is_service_running_bool_key), false);
+            editor.putBoolean(AppPreferenceConstants.IS_SERVICE_RUNNING_KEY, false);
         }
         editor.apply();
     }
@@ -146,8 +147,8 @@ public class MyPersistentService extends Service {
     private void updateServedCount() {
         SharedPreferences sp = GlobalApplication.getAppPreferences();
         sp.edit()
-                .putInt(getString(R.string.users_served_key),
-                        sp.getInt(getString(R.string.users_served_key), 0) + 1)
+                .putInt(AppPreferenceConstants.USER_SERVED_KEY,
+                        sp.getInt(AppPreferenceConstants.USER_SERVED_KEY, 0) + 1)
                 .apply();
     }
 
diff --git a/app/src/main/java/org/torproject/snowflake/constants/AppPreferenceConstants.java b/app/src/main/java/org/torproject/snowflake/constants/AppPreferenceConstants.java
new file mode 100644
index 0000000..58400a4
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/constants/AppPreferenceConstants.java
@@ -0,0 +1,8 @@
+package org.torproject.snowflake.constants;
+
+public class AppPreferenceConstants {
+    public static final String USER_SERVED_KEY = "users_served";
+    public static final String DATE_KEY = "date";
+    public static final String INITIAL_RUN_KEY = "initial_run";
+    public static final String IS_SERVICE_RUNNING_KEY = "is_service_running";
+}
diff --git a/app/src/main/java/org/torproject/snowflake/models/MainActivityModel.java b/app/src/main/java/org/torproject/snowflake/models/MainActivityModel.java
index 7aaf0e5..4354913 100644
--- a/app/src/main/java/org/torproject/snowflake/models/MainActivityModel.java
+++ b/app/src/main/java/org/torproject/snowflake/models/MainActivityModel.java
@@ -4,6 +4,7 @@ import android.content.SharedPreferences;
 import android.util.Log;
 
 import org.torproject.snowflake.GlobalApplication;
+import org.torproject.snowflake.constants.AppPreferenceConstants;
 import org.torproject.snowflake.presenters.MainActivityPresenter;
 
 import java.text.ParseException;
@@ -13,7 +14,6 @@ import java.util.Date;
 
 import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
 import io.reactivex.rxjava3.core.Single;
-import io.reactivex.rxjava3.disposables.Disposable;
 import io.reactivex.rxjava3.schedulers.Schedulers;
 
 /**
@@ -55,8 +55,8 @@ public class MainActivityModel {
         sharedPreferences.edit().putBoolean(key, val).apply();
     }
 
-    public boolean isServiceRunning(String key) {
-        return sharedPreferences.getBoolean(key, false);
+    public boolean isServiceRunning() {
+        return sharedPreferences.getBoolean(AppPreferenceConstants.IS_SERVICE_RUNNING_KEY, false);
     }
 
     /**
@@ -71,7 +71,7 @@ public class MainActivityModel {
         listener = (prefs, key) -> {
             Log.d(TAG, "setListenerForCount: Listener: Key = " + key);
 
-            if (key.equals("users_served")) {
+            if (key.equals(AppPreferenceConstants.USER_SERVED_KEY)) {
                 servedCount = sharedPreferences.getInt(key, 0);
                 if (presenter != null)
                     presenter.updateServedCount(servedCount);
@@ -81,17 +81,17 @@ public class MainActivityModel {
         sharedPreferences.registerOnSharedPreferenceChangeListener(listener);
     }
 
-    public String getDate(String dateKey) {
-        return sharedPreferences.getString(dateKey, "");
+    public String getDate() {
+        return sharedPreferences.getString(AppPreferenceConstants.DATE_KEY, "");
     }
 
     /**
      * Setting the users served date and value.
      */
-    public void setDateAndServed(String dateKey, String valueKey, String date, int value) {
+    public void setDateAndServed(String date, int value) {
         SharedPreferences.Editor editor = sharedPreferences.edit();
-        editor.putString(dateKey, date);
-        editor.putInt(valueKey, value);
+        editor.putString(AppPreferenceConstants.DATE_KEY, date);
+        editor.putInt(AppPreferenceConstants.USER_SERVED_KEY, value);
         editor.apply();
     }
 
@@ -111,7 +111,7 @@ public class MainActivityModel {
 
             //No value for key. Set the date value to current date and users served to 0.
             if (stringRecordedDate.equals("")) {
-                setDateAndServed("date", "users_served", stringCurrentDate, 0);
+                setDateAndServed(stringCurrentDate, 0);
             } else {
                 //Check if the current system date is greater than recorded date, if so reset the "served" flag.
                 Date recordedDate = simpleDateFormat.parse(stringRecordedDate);
@@ -125,7 +125,7 @@ public class MainActivityModel {
                     return true;
                 } else {
                     //Current date is bigger than recorded date. Reset the values. i.e comparision > 0
-                    setDateAndServed("date", "users_served", simpleDateFormat.format(currentDate), 0);
+                    setDateAndServed(simpleDateFormat.format(currentDate), 0);
                 }
             }
 
@@ -146,7 +146,7 @@ public class MainActivityModel {
                     .observeOn(AndroidSchedulers.mainThread())
                     .subscribe((status) -> { //Runs on main thread
                         //By this point the servedCount must be reset or left as is after checking the dates.
-                        presenter.updateServedCount(getServedCount("users_served"));
+                        presenter.updateServedCount(getServedCount(AppPreferenceConstants.USER_SERVED_KEY));
                         setListenerForCount();
                     });
         }
diff --git a/app/src/main/java/org/torproject/snowflake/presenters/MainActivityPresenter.java b/app/src/main/java/org/torproject/snowflake/presenters/MainActivityPresenter.java
index 1958622..d2c0734 100644
--- a/app/src/main/java/org/torproject/snowflake/presenters/MainActivityPresenter.java
+++ b/app/src/main/java/org/torproject/snowflake/presenters/MainActivityPresenter.java
@@ -4,6 +4,7 @@ import android.util.Log;
 
 import org.torproject.snowflake.MainActivity;
 import org.torproject.snowflake.R;
+import org.torproject.snowflake.constants.AppPreferenceConstants;
 import org.torproject.snowflake.models.MainActivityModel;
 
 /**
@@ -32,7 +33,7 @@ public class MainActivityPresenter {
     public int getServedCount() {
         Log.d(TAG, "getServedCount: ");
         if (view != null) {
-            return model.getServedCount(((MainActivity) view).getString(R.string.users_served_key));
+            return model.getServedCount(AppPreferenceConstants.USER_SERVED_KEY);
         }
         return 0;
     }
@@ -40,7 +41,7 @@ public class MainActivityPresenter {
     public boolean getInitialRunBoolean() {
         Log.d(TAG, "getInitialRunBoolean: ");
         if (view != null) {
-            return model.getInitialRunBool(((MainActivity) view).getString(R.string.initial_run_boolean_key));
+            return model.getInitialRunBool(AppPreferenceConstants.INITIAL_RUN_KEY);
         }
         return false;
     }
@@ -53,7 +54,7 @@ public class MainActivityPresenter {
     public void setInitialRunBoolean(boolean val) {
         Log.d(TAG, "setInitialRunBoolean: ");
         if (view != null) {
-            model.setInitialRunBool(((MainActivity) view).getString(R.string.initial_run_boolean_key), val);
+            model.setInitialRunBool(AppPreferenceConstants.INITIAL_RUN_KEY, val);
         }
     }
 
@@ -65,7 +66,7 @@ public class MainActivityPresenter {
     public boolean isServiceRunning() {
         Log.d(TAG, "isServiceRunning: ");
         if (view != null) {
-            return model.isServiceRunning(((MainActivity) view).getString(R.string.is_service_running_bool_key));
+            return model.isServiceRunning();
         }
         return true;
     }
@@ -81,7 +82,7 @@ public class MainActivityPresenter {
      * Getting the served date.
      */
     public String getDate() {
-        return model.getDate("date");
+        return model.getDate();
     }
 
     public void checkDate() {
diff --git a/app/src/main/res/values/strings_preference_keys.xml b/app/src/main/res/values/strings_preference_keys.xml
index 2727405..44d2ee0 100644
--- a/app/src/main/res/values/strings_preference_keys.xml
+++ b/app/src/main/res/values/strings_preference_keys.xml
@@ -1,8 +1,4 @@
 <?xml version="1.0" encoding="utf-8"?>
 <resources>
     <string name="sharedpreference_file">org.torproject.snowflake.snowflake_preferences</string>
-    <string name="users_served_key">users_served</string>
-    <string name="served_date_key">date</string>
-    <string name="initial_run_boolean_key">initial_run</string>
-    <string name="is_service_running_bool_key">is_service_running</string>
 </resources>
\ No newline at end of file





More information about the tor-commits mailing list