X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2FMainModel.java;h=845242bfee305290c724d27eb2f784bfea8d4a46;hb=acdedb28266ec0bde0a3b6775837a70455c339fe;hp=9572ea0d678eeffb152148da5b5123a65847f20d;hpb=5bba2c06a81c87327fdcf3f2a85c3206d932c2f9;p=mobile-ledger.git 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 9572ea0d..845242bf 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java @@ -1,5 +1,5 @@ /* - * Copyright © 2020 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -32,6 +32,7 @@ import net.ktnx.mobileledger.App; import net.ktnx.mobileledger.async.RetrieveTransactionsTask; import net.ktnx.mobileledger.async.TransactionAccumulator; import net.ktnx.mobileledger.async.UpdateTransactionsTask; +import net.ktnx.mobileledger.model.AccountListItem; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerAccount; import net.ktnx.mobileledger.model.LedgerTransaction; @@ -55,17 +56,17 @@ import static net.ktnx.mobileledger.utils.Logger.debug; public class MainModel extends ViewModel { public final MutableLiveData foundTransactionItemIndex = new MutableLiveData<>(null); - public final MutableLiveData lastUpdateDate = new MutableLiveData<>(null); private final MutableLiveData updatingFlag = new MutableLiveData<>(false); private final MutableLiveData accountFilter = new MutableLiveData<>(); private final MutableLiveData> displayedTransactions = new MutableLiveData<>(new ArrayList<>()); - private final MutableLiveData> displayedAccounts = new MutableLiveData<>(); + private final MutableLiveData> displayedAccounts = + new MutableLiveData<>(); private final Locker accountsLocker = new Locker(); private final MutableLiveData updateError = new MutableLiveData<>(); + private final Map accountMap = new HashMap<>(); private MobileLedgerProfile profile; private List allAccounts = new ArrayList<>(); - private Map accountMap = new HashMap<>(); private SimpleDate firstTransactionDate; private SimpleDate lastTransactionDate; transient private RetrieveTransactionsTask retrieveTransactionsTask; @@ -123,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()); - lastUpdateDate.postValue(now); + Data.lastUpdateDate.postValue(now); } public void scheduleTransactionListReload() { UpdateTransactionsTask task = new UpdateTransactionsTask(); @@ -146,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; @@ -209,7 +211,7 @@ public class MainModel extends ViewModel { updateAccountsMap(allAccounts); } } - public LiveData> getDisplayedAccounts() { + public LiveData> getDisplayedAccounts() { return displayedAccounts; } synchronized public void scheduleAccountListReload() { @@ -226,7 +228,7 @@ public class MainModel extends ViewModel { List accounts, List transactions) { profile.storeAccountAndTransactionListAsync(accounts, transactions); - setLastUpdateStamp(); + setLastUpdateStamp(transactions.size()); mergeAccountListFromWeb(accounts); updateDisplayedAccounts(); @@ -269,13 +271,14 @@ public class MainModel extends ViewModel { public void clearUpdateError() { updateError.postValue(null); } + public void clearAccounts() { displayedAccounts.postValue(new ArrayList<>()); } public void clearTransactions() { displayedTransactions.setValue(new ArrayList<>()); } static class AccountListLoader extends Thread { - private MobileLedgerProfile profile; - private MainModel model; + private final MobileLedgerProfile profile; + private final MainModel model; AccountListLoader(MobileLedgerProfile profile, MainModel model) { this.profile = profile; this.model = model; @@ -283,26 +286,27 @@ public class MainModel extends ViewModel { @Override public void run() { Logger.debug("async-acc", "AccountListLoader::run() entered"); - String profileUUID = profile.getUuid(); + long profileId = profile.getId(); ArrayList list = new ArrayList<>(); HashMap map = new HashMap<>(); - String sql = "SELECT a.name, a.expanded, a.amounts_expanded"; - sql += " from accounts a WHERE a.profile = ?"; + String sql = "SELECT a.name, a.expanded, a.amounts_expanded, a.id"; + sql += " from accounts a WHERE a.profile_id = ?"; sql += " ORDER BY a.name"; SQLiteDatabase db = App.getDatabase(); Logger.debug("async-acc", "AccountListLoader::run() connected to DB"); - try (Cursor cursor = db.rawQuery(sql, new String[]{profileUUID})) { + try (Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(profileId)})) { Logger.debug("async-acc", "AccountListLoader::run() executed query"); while (cursor.moveToNext()) { if (isInterrupted()) return; + final long accId = cursor.getLong(3); final String accName = cursor.getString(0); // debug("accounts", // String.format("Read account '%s' from DB [%s]", accName, -// profileUUID)); +// profileId)); String parentName = LedgerAccount.extractParentName(accName); LedgerAccount parent; if (parentName != null) { @@ -322,8 +326,8 @@ public class MainModel extends ViewModel { acc.setHasSubAccounts(false); try (Cursor c2 = db.rawQuery( - "SELECT value, currency FROM account_values WHERE profile = ?" + " " + - "AND account = ?", new String[]{profileUUID, accName})) + "SELECT value, currency FROM account_values WHERE account_id = ?", + new String[]{String.valueOf(accId)})) { while (c2.moveToNext()) { acc.addAmount(c2.getFloat(0), c2.getString(1)); @@ -355,21 +359,25 @@ public class MainModel extends ViewModel { } @Override public void run() { - List newDisplayed = new ArrayList<>(); + List newDisplayed = new ArrayList<>(); Logger.debug("dFilter", "waiting for synchronized block"); Logger.debug("dFilter", String.format(Locale.US, "entered synchronized block (about to examine %d accounts)", list.size())); + newDisplayed.add(new AccountListItem.Header()); // header + + int count = 0; for (LedgerAccount a : list) { - if (isInterrupted()) { + if (isInterrupted()) return; - } if (a.isVisible()) { - newDisplayed.add(a); + newDisplayed.add(new AccountListItem.Account(a)); + count++; } } if (!isInterrupted()) { model.displayedAccounts.postValue(newDisplayed); + Data.lastUpdateAccountCount.postValue(count); } Logger.debug("dFilter", "left synchronized block"); }