/*
- * 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
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;
public class MainModel extends ViewModel {
public final MutableLiveData<Integer> foundTransactionItemIndex = new MutableLiveData<>(null);
- public final MutableLiveData<Date> lastUpdateDate = new MutableLiveData<>(null);
private final MutableLiveData<Boolean> updatingFlag = new MutableLiveData<>(false);
private final MutableLiveData<String> accountFilter = new MutableLiveData<>();
private final MutableLiveData<List<TransactionListItem>> displayedTransactions =
new MutableLiveData<>(new ArrayList<>());
- private final MutableLiveData<List<LedgerAccount>> displayedAccounts = new MutableLiveData<>();
+ private final MutableLiveData<List<AccountListItem>> displayedAccounts =
+ new MutableLiveData<>();
private final Locker accountsLocker = new Locker();
private final MutableLiveData<String> updateError = new MutableLiveData<>();
+ private final Map<String, LedgerAccount> accountMap = new HashMap<>();
private MobileLedgerProfile profile;
private List<LedgerAccount> allAccounts = new ArrayList<>();
- private final Map<String, LedgerAccount> accountMap = new HashMap<>();
private SimpleDate firstTransactionDate;
private SimpleDate lastTransactionDate;
transient private RetrieveTransactionsTask retrieveTransactionsTask;
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();
public LiveData<List<TransactionListItem>> getDisplayedTransactions() {
return displayedTransactions;
}
- public void setDisplayedTransactions(List<TransactionListItem> list) {
+ public void setDisplayedTransactions(List<TransactionListItem> list, int transactionCount) {
displayedTransactions.postValue(list);
+ Data.lastUpdateTransactionCount.postValue(transactionCount);
}
public SimpleDate getFirstTransactionDate() {
return firstTransactionDate;
updateAccountsMap(allAccounts);
}
}
- public LiveData<List<LedgerAccount>> getDisplayedAccounts() {
+ public LiveData<List<AccountListItem>> getDisplayedAccounts() {
return displayedAccounts;
}
synchronized public void scheduleAccountListReload() {
List<LedgerAccount> accounts, List<LedgerTransaction> transactions) {
profile.storeAccountAndTransactionListAsync(accounts, transactions);
- setLastUpdateStamp();
+ setLastUpdateStamp(transactions.size());
mergeAccountListFromWeb(accounts);
updateDisplayedAccounts();
public void clearUpdateError() {
updateError.postValue(null);
}
+ public void clearAccounts() { displayedAccounts.postValue(new ArrayList<>()); }
public void clearTransactions() {
displayedTransactions.setValue(new ArrayList<>());
}
@Override
public void run() {
Logger.debug("async-acc", "AccountListLoader::run() entered");
- String profileUUID = profile.getUuid();
+ long profileId = profile.getId();
ArrayList<LedgerAccount> list = new ArrayList<>();
HashMap<String, LedgerAccount> 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) {
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));
}
@Override
public void run() {
- List<LedgerAccount> newDisplayed = new ArrayList<>();
+ List<AccountListItem> 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");
}