]> git.ktnx.net Git - mobile-ledger.git/commitdiff
the last update UI observes the data in Data.lastUpdateDate
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 5 Jan 2019 13:02:33 +0000 (13:02 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 5 Jan 2019 13:02:33 +0000 (13:02 +0000)
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListFragment.java

index 8d1b2f8b1a0936026a6eabf4dc89b1411430fde8..90e02c9a6a49ed54f8a28d9e7f7a37c83cf47f50 100644 (file)
@@ -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);
index 92b95fb402f5936cb08d0172a7a88b59d9201aa8..e3496a086f6035c28e540cd54bdc667d14bc8625 100644 (file)
@@ -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);