[tor-commits] [snowflake-mobile/master] Added contract interface

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


commit 6c8056d2a6b448ad88d9026b4ca90c11534bc8eb
Author: Hashik Donthineni <HashikDonthineni at gmail.com>
Date:   Sat Jul 25 19:01:02 2020 +0530

    Added contract interface
---
 .../org/torproject/snowflake/MainActivity.java     |  3 +-
 .../snowflake/models/MainActivityModel.java        | 26 ++++++++--------
 .../torproject/snowflake/mvp/MainActivityMVP.java  | 36 ++++++++++++++++++++++
 .../presenters/MainActivityPresenter.java          | 24 +++++----------
 4 files changed, 59 insertions(+), 30 deletions(-)

diff --git a/app/src/main/java/org/torproject/snowflake/MainActivity.java b/app/src/main/java/org/torproject/snowflake/MainActivity.java
index 96b27da..d888ffd 100644
--- a/app/src/main/java/org/torproject/snowflake/MainActivity.java
+++ b/app/src/main/java/org/torproject/snowflake/MainActivity.java
@@ -18,12 +18,13 @@ import org.torproject.snowflake.constants.FragmentConstants;
 import org.torproject.snowflake.fragments.AppSettingsFragment;
 import org.torproject.snowflake.fragments.MainFragment;
 import org.torproject.snowflake.interfaces.MainFragmentCallback;
+import org.torproject.snowflake.mvp.MainActivityMVP;
 import org.torproject.snowflake.presenters.MainActivityPresenter;
 
 /**
  * MainActivity is the main UI of the application.
  */
-public class MainActivity extends AppCompatActivity implements MainFragmentCallback, MainActivityPresenter.View {
+public class MainActivity extends AppCompatActivity implements MainFragmentCallback, MainActivityMVP.View {
     private static final String TAG = "MainActivity";
     int currentFragment;
     MainActivityPresenter presenter;
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 4354913..b04de57 100644
--- a/app/src/main/java/org/torproject/snowflake/models/MainActivityModel.java
+++ b/app/src/main/java/org/torproject/snowflake/models/MainActivityModel.java
@@ -5,7 +5,7 @@ import android.util.Log;
 
 import org.torproject.snowflake.GlobalApplication;
 import org.torproject.snowflake.constants.AppPreferenceConstants;
-import org.torproject.snowflake.presenters.MainActivityPresenter;
+import org.torproject.snowflake.mvp.MainActivityMVP;
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
@@ -19,22 +19,22 @@ import io.reactivex.rxjava3.schedulers.Schedulers;
 /**
  * Model for MainActivity to handle network calls, Shared preferences.
  */
-public class MainActivityModel {
+public class MainActivityModel implements MainActivityMVP.Model {
     private static final String TAG = "MainActivityModel";
     private static MainActivityModel instance = null;
     private SharedPreferences sharedPreferences;
-    private MainActivityPresenter presenter;
+    private MainActivityMVP.Presenter presenter;
     private int servedCount;
     private SharedPreferences.OnSharedPreferenceChangeListener listener;
 
 
-    private MainActivityModel(MainActivityPresenter presenter) {
+    private MainActivityModel(MainActivityMVP.Presenter presenter) {
         sharedPreferences = GlobalApplication.getAppPreferences();
         this.presenter = presenter;
         servedCount = 0;
     }
 
-    public static MainActivityModel getInstance(MainActivityPresenter presenter) {
+    public static MainActivityModel getInstance(MainActivityMVP.Presenter presenter) {
         if (instance == null) {
             synchronized (MainActivityModel.class) {
                 instance = new MainActivityModel(presenter);
@@ -43,16 +43,16 @@ public class MainActivityModel {
         return instance;
     }
 
-    public int getServedCount(String key) {
-        return sharedPreferences.getInt(key, 0);
+    public int getServedCount() {
+        return sharedPreferences.getInt(AppPreferenceConstants.USER_SERVED_KEY, 0);
     }
 
-    public boolean getInitialRunBool(String key) {
-        return sharedPreferences.getBoolean(key, true);
+    public boolean getInitialRunBool() {
+        return sharedPreferences.getBoolean(AppPreferenceConstants.INITIAL_RUN_KEY, true);
     }
 
-    public void setInitialRunBool(String key, boolean val) {
-        sharedPreferences.edit().putBoolean(key, val).apply();
+    public void setInitialRunBool(boolean val) {
+        sharedPreferences.edit().putBoolean(AppPreferenceConstants.INITIAL_RUN_KEY, val).apply();
     }
 
     public boolean isServiceRunning() {
@@ -107,7 +107,7 @@ public class MainActivityModel {
 
         try {
             String stringCurrentDate = simpleDateFormat.format(Calendar.getInstance().getTime());
-            String stringRecordedDate = presenter.getDate();
+            String stringRecordedDate = getDate();
 
             //No value for key. Set the date value to current date and users served to 0.
             if (stringRecordedDate.equals("")) {
@@ -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(AppPreferenceConstants.USER_SERVED_KEY));
+                        presenter.updateServedCount(getServedCount());
                         setListenerForCount();
                     });
         }
diff --git a/app/src/main/java/org/torproject/snowflake/mvp/MainActivityMVP.java b/app/src/main/java/org/torproject/snowflake/mvp/MainActivityMVP.java
new file mode 100644
index 0000000..0ea2068
--- /dev/null
+++ b/app/src/main/java/org/torproject/snowflake/mvp/MainActivityMVP.java
@@ -0,0 +1,36 @@
+package org.torproject.snowflake.mvp;
+
+/**
+ * MVP contract Interface
+ */
+public interface MainActivityMVP {
+    interface View {
+        void updateCountInFragment(int i);
+    }
+
+    interface Model {
+        boolean getInitialRunBool();
+
+        void setInitialRunBool(boolean val);
+
+        boolean isServiceRunning();
+
+        void checkDateAsync();
+
+        int getServedCount();
+    }
+
+    interface Presenter {
+        int getServedCount();
+
+        boolean getInitialRunBoolean();
+
+        void setInitialRunBoolean(boolean val);
+
+        boolean isServiceRunning();
+
+        void updateServedCount(int count);
+
+        void checkDate();
+    }
+}
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 d2c0734..dee48f8 100644
--- a/app/src/main/java/org/torproject/snowflake/presenters/MainActivityPresenter.java
+++ b/app/src/main/java/org/torproject/snowflake/presenters/MainActivityPresenter.java
@@ -2,20 +2,18 @@ package org.torproject.snowflake.presenters;
 
 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;
+import org.torproject.snowflake.mvp.MainActivityMVP;
 
 /**
  * Presenter for MainActivity.
  */
-public class MainActivityPresenter {
+public class MainActivityPresenter implements MainActivityMVP.Presenter {
     private static final String TAG = "MainActivityPresenter";
-    View view;
-    MainActivityModel model;
+    MainActivityMVP.View view;
+    MainActivityMVP.Model model;
 
-    public MainActivityPresenter(View view) {
+    public MainActivityPresenter(MainActivityMVP.View view) {
         //Attaching
         this.view = view;
         model = MainActivityModel.getInstance(this);
@@ -33,7 +31,7 @@ public class MainActivityPresenter {
     public int getServedCount() {
         Log.d(TAG, "getServedCount: ");
         if (view != null) {
-            return model.getServedCount(AppPreferenceConstants.USER_SERVED_KEY);
+            return model.getServedCount();
         }
         return 0;
     }
@@ -41,7 +39,7 @@ public class MainActivityPresenter {
     public boolean getInitialRunBoolean() {
         Log.d(TAG, "getInitialRunBoolean: ");
         if (view != null) {
-            return model.getInitialRunBool(AppPreferenceConstants.INITIAL_RUN_KEY);
+            return model.getInitialRunBool();
         }
         return false;
     }
@@ -54,7 +52,7 @@ public class MainActivityPresenter {
     public void setInitialRunBoolean(boolean val) {
         Log.d(TAG, "setInitialRunBoolean: ");
         if (view != null) {
-            model.setInitialRunBool(AppPreferenceConstants.INITIAL_RUN_KEY, val);
+            model.setInitialRunBool(val);
         }
     }
 
@@ -78,12 +76,6 @@ public class MainActivityPresenter {
         }
     }
 
-    /**
-     * Getting the served date.
-     */
-    public String getDate() {
-        return model.getDate();
-    }
 
     public void checkDate() {
         model.checkDateAsync();





More information about the tor-commits mailing list