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

[tor-commits] [snowflake-mobile/master] Launched checkServedDate on another thread and fixed date parsing exception



commit 78ef0ce64c1a75fc1586694e8dcb0fd6d53c3154
Author: Hashik Donthineni <HashikDonthineni@xxxxxxxxx>
Date:   Sat Jul 11 01:51:51 2020 +0530

    Launched checkServedDate on another thread and fixed date parsing exception
---
 .../org/torproject/snowflake/MainActivity.java     | 32 ++++++++++++++++++----
 1 file changed, 26 insertions(+), 6 deletions(-)

diff --git a/app/src/main/java/org/torproject/snowflake/MainActivity.java b/app/src/main/java/org/torproject/snowflake/MainActivity.java
index a1103f4..4807285 100644
--- a/app/src/main/java/org/torproject/snowflake/MainActivity.java
+++ b/app/src/main/java/org/torproject/snowflake/MainActivity.java
@@ -27,7 +27,6 @@ import java.util.Calendar;
 import java.util.Date;
 
 import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers;
-import io.reactivex.rxjava3.core.Observable;
 import io.reactivex.rxjava3.core.Single;
 import io.reactivex.rxjava3.disposables.Disposable;
 import io.reactivex.rxjava3.schedulers.Schedulers;
@@ -40,6 +39,7 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
     int currentFragment;
     private SharedPreferences sharedPreferences;
     private Button settingsButton;
+    private Disposable disposable;
 
     @Override
     protected void onCreate(Bundle savedInstanceState) {
@@ -47,9 +47,16 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
         setContentView(R.layout.activity_main);
         setSupportActionBar(findViewById(R.id.toolbar));
         settingsButton = findViewById(R.id.settings_button);
-
         sharedPreferences = GlobalApplication.getAppPreferences();
 
+
+        disposable = Single.fromCallable(this::checkServedDate)
+                .subscribeOn(Schedulers.io())
+                .observeOn(AndroidSchedulers.mainThread())
+                .subscribe((status) -> {
+                    //Update The TextView showing users served to user.
+                });
+
         //Creating notification channel if app is being run for the first time
         if (sharedPreferences.getBoolean(getString(R.string.initial_run_boolean), true)) {
             createNotificationChannel();
@@ -135,29 +142,33 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
     /**
      * Function to check and update the date and users served.
      * Resets served count if the past served date is greater than 24hrs.
+     *
+     * @return True if the date parsing is done right without errors.
      */
-    public void checkServedDate() {
+    public boolean checkServedDate() {
         Log.d(TAG, "checkServedDate: ");
         SharedPreferences.Editor editor = sharedPreferences.edit();
         SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd-MMM-yyyy");
 
         try {
-            Date currentDate = simpleDateFormat.parse(Calendar.getInstance().getTime().toString());
+            String stringCurrentDate = simpleDateFormat.format(Calendar.getInstance().getTime());
             String stringRecordedDate = sharedPreferences.getString(getString(R.string.served_date), "");
 
             //No value for key. Set the date value to current date and users served to 0.
             if (stringRecordedDate.equals("")) {
-                editor.putString(getString(R.string.served_date), simpleDateFormat.format(currentDate));
+                editor.putString(getString(R.string.served_date), stringCurrentDate);
                 editor.putInt(getString(R.string.users_served), 0);
             } else {
                 //Check if the current system date is greater than recorded date, if so reset the "served" flag.
                 Date recordedDate = simpleDateFormat.parse(stringRecordedDate);
+                Date currentDate = simpleDateFormat.parse(stringCurrentDate);
+
                 Log.d(TAG, "checkServedDate: Current Date:" + currentDate.toString() + "  Recorded Date:" + recordedDate.toString());
                 int comparision = currentDate.compareTo(recordedDate);
 
                 if (comparision == 0) {
                     //Current date is same as recordedDate no need to reset. Since it's less than 24hrs.
-                    return;
+                    return true;
                 } else {
                     //Current date is bigger than recorded date. Reset the values. i.e comparision > 0
                     editor.putString(getString(R.string.served_date), simpleDateFormat.format(currentDate));
@@ -170,7 +181,9 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
         } catch (ParseException e) {
             e.printStackTrace();
             Log.e(TAG, "checkServedDate: Invalid Date Parsing");
+            return false;
         }
+        return true;
     }
 
 
@@ -182,4 +195,11 @@ public class MainActivity extends AppCompatActivity implements MainFragmentCallb
         else
             super.onBackPressed();
     }
+
+    @Override
+    protected void onDestroy() {
+        //Killing of thread
+        disposable.dispose();
+        super.onDestroy();
+    }
 }



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