From e9852f655ca908b8817de90a42ef1dda9a47285e Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Tue, 16 Apr 2019 19:51:30 +0300 Subject: [PATCH] migrate lastUpdatedate to LiveData re-inventing a pissibly brooken wheel doesn't sound like a good idea --- .../net/ktnx/mobileledger/model/Data.java | 4 +- .../model/MobileLedgerProfile.java | 2 +- .../ui/activity/MainActivity.java | 48 ++++++------------- 3 files changed, 19 insertions(+), 35 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 1c679566..57e2774d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -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 transactions = new ObservableList<>(new ArrayList<>()); public static ObservableList accounts = new ObservableList<>(new ArrayList<>()); public static ObservableAtomicInteger backgroundTaskCount = new ObservableAtomicInteger(0); - public static ObservableValue lastUpdateDate = new ObservableValue<>(); + public static MutableLiveData lastUpdateDate = new MutableLiveData<>(); public static ObservableValue profile = new ObservableValue<>(); public static ObservableList profiles = new ObservableList<>(new ArrayList<>()); diff --git a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java index 93a954f0..f69332ef 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/MobileLedgerProfile.java @@ -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 loadChildAccountsOf(LedgerAccount acc) { List result = new ArrayList<>(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index bc164a55..5a2e92fd 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -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() { -- 2.39.2