From: Damyan Ivanov Date: Sat, 19 Sep 2020 07:02:36 +0000 (+0300) Subject: last update text also includes transaction count X-Git-Tag: v0.15.0~11 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=9a56eed6dcbfe4434a9a46b198320c16b288d86f;p=mobile-ledger.git last update text also includes transaction count and is maintained by the main activity, which has access to all the relevant bits - string resources, live data observation, up to date transaction count --- diff --git a/app/src/main/java/net/ktnx/mobileledger/async/TransactionAccumulator.java b/app/src/main/java/net/ktnx/mobileledger/async/TransactionAccumulator.java index 43d36e1f..b8c8e696 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/TransactionAccumulator.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/TransactionAccumulator.java @@ -30,6 +30,7 @@ public class TransactionAccumulator { private SimpleDate earliestDate, latestDate; private SimpleDate lastDate = SimpleDate.today(); private boolean done; + private int transactionCount = 0; public TransactionAccumulator(MainModel model) { this.model = model; } @@ -53,10 +54,11 @@ public class TransactionAccumulator { list.add(new TransactionListItem(transaction)); lastDate = date; + transactionCount++; } public void done() { done = true; - model.setDisplayedTransactions(list); + model.setDisplayedTransactions(list, transactionCount); model.setFirstTransactionDate(earliestDate); model.setLastTransactionDate(latestDate); } 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 66738cb0..6d51287f 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -56,8 +56,10 @@ public final class Data { public static final MutableLiveData currencyGap = new MutableLiveData<>(true); public static final MutableLiveData locale = new MutableLiveData<>(); public static final MutableLiveData drawerOpen = new MutableLiveData<>(false); - public static final MutableLiveData lastUpdateLiveData = new MutableLiveData<>(null); - public static final ObservableValue lastUpdate = new ObservableValue<>(); + public static final MutableLiveData lastUpdateDate = new MutableLiveData<>(null); + public static final MutableLiveData lastUpdateTransactionCount = + new MutableLiveData<>(0); + public static final ObservableValue lastUpdateText = new ObservableValue<>(); private static final MutableLiveData profile = new InertMutableLiveData<>(); private static final AtomicInteger backgroundTaskCount = new AtomicInteger(0); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java index 6d640d62..4d232319 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java @@ -124,11 +124,11 @@ public class MainModel extends ViewModel { return merged; } - private void setLastUpdateStamp() { + private void setLastUpdateStamp(long transactionCount) { debug("db", "Updating transaction value stamp"); Date now = new Date(); profile.setLongOption(MLDB.OPT_LAST_SCRAPE, now.getTime()); - Data.lastUpdateLiveData.postValue(now); + Data.lastUpdateDate.postValue(now); } public void scheduleTransactionListReload() { UpdateTransactionsTask task = new UpdateTransactionsTask(); @@ -147,8 +147,9 @@ public class MainModel extends ViewModel { public LiveData> getDisplayedTransactions() { return displayedTransactions; } - public void setDisplayedTransactions(List list) { + public void setDisplayedTransactions(List list, int transactionCount) { displayedTransactions.postValue(list); + Data.lastUpdateTransactionCount.postValue(transactionCount); } public SimpleDate getFirstTransactionDate() { return firstTransactionDate; @@ -227,7 +228,7 @@ public class MainModel extends ViewModel { List accounts, List transactions) { profile.storeAccountAndTransactionListAsync(accounts, transactions); - setLastUpdateStamp(); + setLastUpdateStamp(transactions.size()); mergeAccountListFromWeb(accounts); updateDisplayedAccounts(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java index c6c90915..cc6cff18 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java @@ -20,7 +20,6 @@ package net.ktnx.mobileledger.ui.account_summary; import android.content.Context; import android.content.res.Resources; import android.text.TextUtils; -import android.text.format.DateUtils; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -257,32 +256,29 @@ public class AccountSummaryAdapter break; case HEADER: - setLastUpdateText(Data.lastUpdate.get()); + setLastUpdateText(Data.lastUpdateText.get()); break; default: throw new IllegalStateException("Unexpected value: " + newType); } } - void setLastUpdateText(long lastUpdate) { - final int formatFlags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | - DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NUMERIC_DATE; - tvLastUpdate.setText((lastUpdate == 0) ? "----" : DateUtils.formatDateTime( - tvLastUpdate.getContext(), lastUpdate, formatFlags)); + void setLastUpdateText(String text) { + tvLastUpdate.setText(text); } private void initLastUpdateObserver() { if (lastUpdateObserver != null) return; - lastUpdateObserver = (o, arg) -> setLastUpdateText(Data.lastUpdate.get()); + lastUpdateObserver = (o, arg) -> setLastUpdateText(Data.lastUpdateText.get()); - Data.lastUpdate.addObserver(lastUpdateObserver); + Data.lastUpdateText.addObserver(lastUpdateObserver); } private void dropLastUpdateObserver() { if (lastUpdateObserver == null) return; - Data.lastUpdate.deleteObserver(lastUpdateObserver); + Data.lastUpdateText.deleteObserver(lastUpdateObserver); lastUpdateObserver = null; } private void setType(AccountListItem.Type newType) { 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 9082695f..03617c59 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 @@ -27,6 +27,7 @@ import android.graphics.Color; import android.graphics.drawable.Icon; import android.os.Build; import android.os.Bundle; +import android.text.format.DateUtils; import android.util.Log; import android.view.View; import android.view.animation.AnimationUtils; @@ -69,6 +70,7 @@ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Locale; +import java.util.Objects; /* * TODO: reports @@ -331,6 +333,9 @@ public class MainActivity extends ProfileThemedActivity { .show(); mainModel.clearUpdateError(); }); + Data.locale.observe(this, l -> refreshLastUpdateInfo()); + Data.lastUpdateDate.observe(this, date -> refreshLastUpdateInfo()); + Data.lastUpdateTransactionCount.observe(this, date -> refreshLastUpdateInfo()); } private void scheduleDataRetrievalIfStale(long lastUpdate) { long now = new Date().getTime(); @@ -462,7 +467,8 @@ public class MainActivity extends ProfileThemedActivity { // un-hook all observed LiveData Data.removeProfileObservers(this); Data.profiles.removeObservers(this); - Data.lastUpdateLiveData.removeObservers(this); + Data.lastUpdateTransactionCount.removeObservers(this); + Data.lastUpdateDate.removeObservers(this); recreate(); } @@ -554,26 +560,26 @@ public class MainActivity extends ProfileThemedActivity { Logger.debug("transactions", String.format(Locale.ENGLISH, "Last update = %d", lastUpdate)); if (lastUpdate == 0) { - Data.lastUpdateLiveData.postValue(null); + Data.lastUpdateDate.postValue(null); } else { - Data.lastUpdateLiveData.postValue(new Date(lastUpdate)); + Data.lastUpdateDate.postValue(new Date(lastUpdate)); } - // this is unfortunate, but it appears we need a two-stage rocket to make - // a value reach a recycler view item holder. first stage is a regular - // LiveData that can be observed by an activity (this). - // the second stage forwards the changes, in the UI thread, to the - // observable value, observed by the view holders. - // view holders can't observe the LiveData because they don't have - // access to lifecycle owners. oh, also the value is updated by a thread - // so it must be tunnelled by an activity for it to reach the view - // holders in the UI thread - Data.lastUpdateLiveData.observe(this, date -> runOnUiThread( - () -> Data.lastUpdate.set((date == null) ? 0 : date.getTime()))); scheduleDataRetrievalIfStale(lastUpdate); } + private void refreshLastUpdateInfo() { + final int formatFlags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | + DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NUMERIC_DATE; + String template = getResources().getString(R.string.transaction_count_summary); + Integer transactionCount = Data.lastUpdateTransactionCount.getValue(); + Date lastUpdate = Data.lastUpdateDate.getValue(); + Data.lastUpdateText.set((lastUpdate == null) ? "----" : String.format( + Objects.requireNonNull(Data.locale.getValue()), template, + (transactionCount == null) ? 0 : transactionCount, + DateUtils.formatDateTime(this, lastUpdate.getTime(), formatFlags))); + } public void onStopTransactionRefreshClick(View view) { Logger.debug("interactive", "Cancelling transactions refresh"); mainModel.stopTransactionsRetrieval(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java index 94712cbd..d9f4cf16 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java @@ -166,7 +166,7 @@ public class TransactionListAdapter extends RecyclerView.Adapter setLastUpdateText(Data.lastUpdate.get()); + lastUpdateObserver = (o, arg) -> setLastUpdateText(Data.lastUpdateText.get()); - Data.lastUpdate.addObserver(lastUpdateObserver); + Data.lastUpdateText.addObserver(lastUpdateObserver); } - void setLastUpdateText(long lastUpdate) { - final int formatFlags = DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR | - DateUtils.FORMAT_SHOW_TIME | DateUtils.FORMAT_NUMERIC_DATE; - tvLastUpdate.setText((lastUpdate == 0) ? "----" - : DateUtils.formatDateTime(tvLastUpdate.getContext(), - lastUpdate, formatFlags)); + void setLastUpdateText(String text) { + tvLastUpdate.setText(text); } private void dropLastUpdateObserver() { if (lastUpdateObserver == null) return; - Data.lastUpdate.deleteObserver(lastUpdateObserver); + Data.lastUpdateText.deleteObserver(lastUpdateObserver); lastUpdateObserver = null; } void setType(TransactionListItem.Type newType) { diff --git a/app/src/main/res/layout/last_update_layout.xml b/app/src/main/res/layout/last_update_layout.xml index 2150affe..b1640f32 100644 --- a/app/src/main/res/layout/last_update_layout.xml +++ b/app/src/main/res/layout/last_update_layout.xml @@ -23,27 +23,18 @@ android:layout_height="wrap_content" android:paddingTop="4dp" > - diff --git a/app/src/main/res/values-bg/strings.xml b/app/src/main/res/values-bg/strings.xml index 0b16b82c..193a9fae 100644 --- a/app/src/main/res/values-bg/strings.xml +++ b/app/src/main/res/values-bg/strings.xml @@ -1,6 +1,6 @@