]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/MainModel.java
migrate to surrogate IDs for all database objects
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / MainModel.java
index 6d640d622d5bca660f578cd14b0b8e212f67fd34..813e3a00b3ab54a49b480ccad5f9b5584d077cd5 100644 (file)
@@ -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
@@ -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<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;
@@ -227,7 +228,7 @@ public class MainModel extends ViewModel {
             List<LedgerAccount> accounts, List<LedgerTransaction> transactions) {
         profile.storeAccountAndTransactionListAsync(accounts, transactions);
 
-        setLastUpdateStamp();
+        setLastUpdateStamp(transactions.size());
 
         mergeAccountListFromWeb(accounts);
         updateDisplayedAccounts();
@@ -270,6 +271,7 @@ 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<>());
     }
@@ -284,7 +286,7 @@ 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<LedgerAccount> list = new ArrayList<>();
             HashMap<String, LedgerAccount> map = new HashMap<>();
 
@@ -294,7 +296,7 @@ public class MainModel extends ViewModel {
 
             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())
@@ -303,7 +305,7 @@ public class MainModel extends ViewModel {
                     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) {
@@ -324,7 +326,7 @@ public class MainModel extends ViewModel {
 
                     try (Cursor c2 = db.rawQuery(
                             "SELECT value, currency FROM account_values WHERE profile = ?" + " " +
-                            "AND account = ?", new String[]{profileUUID, accName}))
+                            "AND account = ?", new String[]{String.valueOf(profileId), accName}))
                     {
                         while (c2.moveToNext()) {
                             acc.addAmount(c2.getFloat(0), c2.getString(1));
@@ -361,15 +363,20 @@ public class MainModel extends ViewModel {
             Logger.debug("dFilter", String.format(Locale.US,
                     "entered synchronized block (about to examine %d accounts)", list.size()));
             newDisplayed.add(new AccountListItem());    // header
+
+            int count = 0;
             for (LedgerAccount a : list) {
                 if (isInterrupted())
                     return;
 
-                if (a.isVisible())
+                if (a.isVisible()) {
                     newDisplayed.add(new AccountListItem(a));
+                    count++;
+                }
             }
             if (!isInterrupted()) {
                 model.displayedAccounts.postValue(newDisplayed);
+                Data.lastUpdateAccountCount.postValue(count);
             }
             Logger.debug("dFilter", "left synchronized block");
         }