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;
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;
}
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);
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);
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(
}
public void onRetrieveDone(boolean success) {
progressLayout.setVisibility(View.GONE);
- updateLastUpdateText();
+ updateLastUpdateTextFromDB();
}
public void onRetrieveStart() {
progressBar.setIndeterminate(true);