]> git.ktnx.net Git - mobile-ledger.git/commitdiff
migrate lastUpdatedate to LiveData
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Tue, 16 Apr 2019 16:51:30 +0000 (19:51 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 19 Apr 2019 20:46:03 +0000 (23:46 +0300)
re-inventing a pissibly brooken wheel doesn't sound like a good idea

app/src/main/java/net/ktnx/mobileledger/model/Data.java
app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java

index 1c67956677188a1f86bfbc02f456134478cdc7ee..57e2774dee137c4a642c50a265d42a4c2a6f7ee6 100644 (file)
@@ -29,11 +29,13 @@ import net.ktnx.mobileledger.utils.ObservableValue;
 import java.util.ArrayList;
 import java.util.Date;
 
+import androidx.lifecycle.MutableLiveData;
+
 public final class Data {
     public static ObservableList<TransactionListItem> transactions = new ObservableList<>(new ArrayList<>());
     public static ObservableList<LedgerAccount> accounts = new ObservableList<>(new ArrayList<>());
     public static ObservableAtomicInteger backgroundTaskCount = new ObservableAtomicInteger(0);
-    public static ObservableValue<Date> lastUpdateDate = new ObservableValue<>();
+    public static MutableLiveData<Date> lastUpdateDate = new MutableLiveData<>();
     public static ObservableValue<MobileLedgerProfile> profile = new ObservableValue<>();
     public static ObservableList<MobileLedgerProfile> profiles =
             new ObservableList<>(new ArrayList<>());
index 93a954f0a674bc95354eeb70d6c00831293f487b..f69332ef2d6af9c8c0c18eaddcbb45a5eced14c4 100644 (file)
@@ -388,7 +388,7 @@ public final class MobileLedgerProfile {
         Log.d("db", "Updating transaction value stamp");
         Date now = new Date();
         setLongOption(MLDB.OPT_LAST_SCRAPE, now.getTime());
-        Data.lastUpdateDate.set(now);
+        Data.lastUpdateDate.postValue(now);
     }
     public List<LedgerAccount> loadChildAccountsOf(LedgerAccount acc) {
         List<LedgerAccount> result = new ArrayList<>();
index bc164a55333a9d28450a78d346c7720a66e63bee..5a2e92fded59532fc43db6d3b09c8219b68e86ad 100644 (file)
@@ -102,7 +102,6 @@ public class MainActivity extends ProfileThemedActivity {
     private boolean mBackMeansToAccountList = false;
     private Observer profileObserver;
     private Observer profilesObserver;
-    private Observer lastUpdateDateObserver;
     private Toolbar mToolbar;
     private DrawerLayout.SimpleDrawerListener drawerListener;
     private ActionBarDrawerToggle barDrawerToggle;
@@ -132,8 +131,6 @@ public class MainActivity extends ProfileThemedActivity {
         profileObserver = null;
         Data.profiles.deleteObserver(profilesObserver);
         profilesObserver = null;
-        Data.lastUpdateDate.deleteObserver(lastUpdateDateObserver);
-        lastUpdateDateObserver = null;
         RecyclerView root = findViewById(R.id.nav_profile_list);
         if (root != null) root.setAdapter(null);
         if (drawer != null) drawer.removeDrawerListener(drawerListener);
@@ -247,15 +244,7 @@ public class MainActivity extends ProfileThemedActivity {
         }
         else mAccountFilter = null;
 
-        if (lastUpdateDateObserver == null) {
-            lastUpdateDateObserver = (o, arg) -> {
-                Log.d("main", "lastUpdateDate changed");
-                runOnUiThread(this::updateLastUpdateDisplay);
-            };
-            Data.lastUpdateDate.addObserver(lastUpdateDateObserver);
-        }
-
-        updateLastUpdateDisplay();
+        Data.lastUpdateDate.observe(this, this::updateLastUpdateDisplay);
 
         findViewById(R.id.btn_no_profiles_add)
                 .setOnClickListener(v -> startEditProfileActivity(null));
@@ -319,11 +308,8 @@ public class MainActivity extends ProfileThemedActivity {
         onProfileChanged(null);
 
         updateLastUpdateTextFromDB();
-
-        scheduleDataRetrievalIfStale();
     }
-    private void scheduleDataRetrievalIfStale() {
-        Date lastUpdate = Data.lastUpdateDate.get();
+    private void scheduleDataRetrievalIfStale(Date lastUpdate) {
         long now = new Date().getTime();
         if ((lastUpdate == null) || (now > (lastUpdate.getTime() + (24 * 3600 * 1000)))) {
             if (lastUpdate == null) Log.d("db::", "WEB data never fetched. scheduling a fetch");
@@ -429,24 +415,23 @@ public class MainActivity extends ProfileThemedActivity {
             }
 
             updateLastUpdateTextFromDB();
-
-            scheduleDataRetrievalIfStale();
         });
     }
-    private void updateLastUpdateDisplay() {
+    private void updateLastUpdateDisplay(Date newValue) {
         LinearLayout l = findViewById(R.id.transactions_last_update_layout);
         TextView v = findViewById(R.id.transactions_last_update);
-        Date date = Data.lastUpdateDate.get();
-        if (date == null) {
+        if (newValue == null) {
             l.setVisibility(View.INVISIBLE);
             Log.d("main", "no last update date :(");
         }
         else {
-            final String text = DateFormat.getDateTimeInstance().format(date);
+            final String text = DateFormat.getDateTimeInstance().format(newValue);
             v.setText(text);
             l.setVisibility(View.VISIBLE);
             Log.d("main", String.format("Date formatted: %s", text));
         }
+
+        scheduleDataRetrievalIfStale(newValue);
     }
     @Override
     public void finish() {
@@ -598,18 +583,15 @@ public class MainActivity extends ProfileThemedActivity {
         }
     }
     public void updateLastUpdateTextFromDB() {
-        {
-            final MobileLedgerProfile profile = Data.profile.get();
-            long last_update =
-                    (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
+        final MobileLedgerProfile profile = Data.profile.get();
+        long last_update = (profile != null) ? profile.getLongOption(MLDB.OPT_LAST_SCRAPE, 0L) : 0;
 
-            Log.d("transactions", String.format("Last update = %d", last_update));
-            if (last_update == 0) {
-                Data.lastUpdateDate.set(null);
-            }
-            else {
-                Data.lastUpdateDate.set(new Date(last_update));
-            }
+        Log.d("transactions", String.format("Last update = %d", last_update));
+        if (last_update == 0) {
+            Data.lastUpdateDate.postValue(null);
+        }
+        else {
+            Data.lastUpdateDate.postValue(new Date(last_update));
         }
     }
     public void scheduleTransactionListRetrieval() {