[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [snowflake-mobile/master] Fetched the count in the MainFragment and logged it



commit 154331c578ecf5bdaf574792ee5be176af3d9ddb
Author: Hashik Donthineni <HashikDonthineni@xxxxxxxxx>
Date:   Sat Jul 11 02:44:47 2020 +0530

    Fetched the count in the MainFragment and logged it
---
 .../org/torproject/snowflake/MainActivity.java     | 26 ++++++++++++++++++----
 .../snowflake/fragments/MainFragment.java          | 10 ++++++++-
 .../snowflake/interfaces/MainFragmentCallback.java |  1 +
 3 files changed, 32 insertions(+), 5 deletions(-)

diff --git a/app/src/main/java/org/torproject/snowflake/MainActivity.java b/app/src/main/java/org/torproject/snowflake/MainActivity.java
index 4807285..65480cc 100644
--- a/app/src/main/java/org/torproject/snowflake/MainActivity.java
+++ b/app/src/main/java/org/torproject/snowflake/MainActivity.java
@@ -36,6 +36,7 @@ import io.reactivex.rxjava3.schedulers.Schedulers;
  */
 public class MainActivity extends AppCompatActivity implements MainFragmentCallback {
     private static final String TAG = "MainActivity";
+    public int servedCount;
     int currentFragment;
     private SharedPreferences sharedPreferences;
     private Button settingsButton;
@@ -48,13 +49,21 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
         setSupportActionBar(findViewById(R.id.toolbar));
         settingsButton = findViewById(R.id.settings_button);
         sharedPreferences = GlobalApplication.getAppPreferences();
+        servedCount = 0;
 
-
+        //Launching another thread to check, reset served date if need be.
         disposable = Single.fromCallable(this::checkServedDate)
                 .subscribeOn(Schedulers.io())
                 .observeOn(AndroidSchedulers.mainThread())
-                .subscribe((status) -> {
-                    //Update The TextView showing users served to user.
+                .subscribe((status) -> { //Runs on main thread
+                    //By this point the servedCount must be reset or left as is after checking the dates.
+                    servedCount = sharedPreferences.getInt(getString(R.string.users_served), 0);
+
+                    Fragment mainFragment = getSupportFragmentManager().findFragmentByTag(Integer.toString(FragmentConstants.MAIN_FRAGMENT));
+                    //If the fragment is in foreground update the count. Or else ignore.
+                    if (mainFragment != null) {
+                        ((MainFragment) mainFragment).showServed();
+                    }
                 });
 
         //Creating notification channel if app is being run for the first time
@@ -93,7 +102,7 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
         getSupportFragmentManager()
                 .beginTransaction()
                 .replace(R.id.fragment_container,
-                        fragment).commit();
+                        fragment, Integer.toString(currentFragment)).commit();
     }
 
     /**
@@ -120,6 +129,15 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
         return sharedPreferences.getBoolean(getString(R.string.is_service_running_bool), false);
     }
 
+    /**
+     * @return Total served users count in the past 24hrs.
+     */
+    @Override
+    public int getServed() {
+        //By default 0 is returned until the thread finishes executing checkServedDate function.
+        return servedCount;
+    }
+
     /**
      * Used to create a new notification channel if app is started for the first time on a device.
      */
diff --git a/app/src/main/java/org/torproject/snowflake/fragments/MainFragment.java b/app/src/main/java/org/torproject/snowflake/fragments/MainFragment.java
index 0ac2407..705cd0c 100644
--- a/app/src/main/java/org/torproject/snowflake/fragments/MainFragment.java
+++ b/app/src/main/java/org/torproject/snowflake/fragments/MainFragment.java
@@ -2,6 +2,7 @@ package org.torproject.snowflake.fragments;
 
 import android.content.Context;
 import android.os.Bundle;
+import android.util.Log;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -10,7 +11,6 @@ import android.widget.Button;
 import androidx.annotation.NonNull;
 import androidx.fragment.app.Fragment;
 
-import org.torproject.snowflake.BuildConfig;
 import org.torproject.snowflake.R;
 import org.torproject.snowflake.constants.ForegroundServiceConstants;
 import org.torproject.snowflake.interfaces.MainFragmentCallback;
@@ -21,6 +21,7 @@ import org.torproject.snowflake.interfaces.MainFragmentCallback;
  * create an instance of this fragment.
  */
 public class MainFragment extends Fragment {
+    private static final String TAG = "MainFragment";
     MainFragmentCallback callback;
 
     public MainFragment() {
@@ -56,6 +57,9 @@ public class MainFragment extends Fragment {
                 callback.serviceToggle(ForegroundServiceConstants.ACTION_START);
         });
 
+        //Calling this in case we return back to this fragment from a different fragment.
+        showServed();
+
         // Inflate the layout for this fragment
         return rootView;
     }
@@ -65,4 +69,8 @@ public class MainFragment extends Fragment {
         super.onAttach(context);
         callback = (MainFragmentCallback) context;
     }
+
+    public void showServed() {
+        Log.d(TAG, "showServed: " + callback.getServed());
+    }
 }
\ No newline at end of file
diff --git a/app/src/main/java/org/torproject/snowflake/interfaces/MainFragmentCallback.java b/app/src/main/java/org/torproject/snowflake/interfaces/MainFragmentCallback.java
index 08e2d99..40da367 100644
--- a/app/src/main/java/org/torproject/snowflake/interfaces/MainFragmentCallback.java
+++ b/app/src/main/java/org/torproject/snowflake/interfaces/MainFragmentCallback.java
@@ -3,4 +3,5 @@ package org.torproject.snowflake.interfaces;
 public interface MainFragmentCallback {
     boolean isServiceRunning();
     void serviceToggle(String action);
+    int getServed();
 }



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits