From 13478d849714d11fd4d3b65a80c18fd6e30522be Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sat, 5 Jan 2019 13:02:33 +0000 Subject: [PATCH] the last update UI observes the data in Data.lastUpdateDate --- .../ui/activity/MainActivity.java | 62 ++++++++++++++----- .../TransactionListFragment.java | 2 +- 2 files changed, 48 insertions(+), 16 deletions(-) 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 8d1b2f8b..90e02c9a 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 @@ -40,6 +40,7 @@ import android.widget.TextView; import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.async.RetrieveTransactionsTask; +import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerAccount; import net.ktnx.mobileledger.ui.MobileLedgerListFragment; import net.ktnx.mobileledger.ui.account_summary.AccountSummaryFragment; @@ -50,6 +51,8 @@ import java.lang.ref.WeakReference; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; +import java.util.Observable; +import java.util.Observer; public class MainActivity extends AppCompatActivity { DrawerLayout drawer; @@ -89,9 +92,6 @@ public class MainActivity extends AppCompatActivity { } tvLastUpdate = findViewById(R.id.transactions_last_update); - updateLastUpdateText(); - long last_update = MLDB.get_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP, 0L); - Log.d("transactions", String.format("Last update = %d", last_update)); bTransactionListCancelDownload = findViewById(R.id.transaction_list_cancel_download); @@ -105,6 +105,44 @@ public class MainActivity extends AppCompatActivity { fragmentManager = getSupportFragmentManager(); onAccountSummaryClicked(null); + + Data.lastUpdateDate.addObserver(new Observer() { + @Override + public void update(Observable o, Object arg) { + Log.d("main", "lastUpdateDate changed"); + runOnUiThread(new Runnable() { + @Override + public void run() { + Date date = Data.lastUpdateDate.get(); + if (date == null) { + tvLastUpdate.setText(R.string.transaction_last_update_never); + } + else { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault()) + .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); + } + else { + tvLastUpdate.setText(date.toLocaleString()); + } + } + } + }); + } + }); + + updateLastUpdateTextFromDB(); + Date lastUpdate = Data.lastUpdateDate.get(); + + 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"); + else Log.d("db", + String.format("WEB data last fetched at %1.3f and now is %1.3f. re-fetching", + lastUpdate.getTime() / 1000f, now / 1000f)); + + scheduleTransactionListRetrieval(); + } } public void fab_new_transaction_clicked(View view) { Intent intent = new Intent(this, NewTransactionActivity.class); @@ -234,26 +272,20 @@ public class MainActivity extends AppCompatActivity { super.onBackPressed(); } } - public void updateLastUpdateText() { + public void updateLastUpdateTextFromDB() { { long last_update = MLDB.get_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP, 0L); + Log.d("transactions", String.format("Last update = %d", last_update)); if (last_update == 0) { - tvLastUpdate.setText(getString(R.string.transaction_last_update_never)); + Data.lastUpdateDate.set(null); } else { - Date date = new Date(last_update); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - tvLastUpdate.setText(date.toInstant().atZone(ZoneId.systemDefault()) - .format(DateTimeFormatter.ISO_LOCAL_DATE_TIME)); - } - else { - tvLastUpdate.setText(date.toLocaleString()); - } + Data.lastUpdateDate.set(new Date(last_update)); } } } - public void update_transactions() { + public void scheduleTransactionListRetrieval() { retrieveTransactionsTask = new RetrieveTransactionsTask(new WeakReference<>(this)); RetrieveTransactionsTask.Params params = new RetrieveTransactionsTask.Params( @@ -269,7 +301,7 @@ public class MainActivity extends AppCompatActivity { } public void onRetrieveDone(boolean success) { progressLayout.setVisibility(View.GONE); - updateLastUpdateText(); + updateLastUpdateTextFromDB(); } public void onRetrieveStart() { progressBar.setIndeterminate(true); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java index 92b95fb4..e3496a08 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java @@ -158,7 +158,7 @@ public class TransactionListFragment extends MobileLedgerListFragment { swiper.setOnRefreshListener(() -> { Log.d("ui", "refreshing transactions via swipe"); - mActivity.update_transactions(); + mActivity.scheduleTransactionListRetrieval(); }); swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent); -- 2.39.5