]> git.ktnx.net Git - mobile-ledger.git/commitdiff
speculatively add new transactions to the database and UI list
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 24 Apr 2021 21:04:59 +0000 (00:04 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 25 Apr 2021 17:07:58 +0000 (17:07 +0000)
this saves a refresh that would be need to view the new transactions and
 ccount balances

app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java
app/src/main/java/net/ktnx/mobileledger/async/TaskCallback.java
app/src/main/java/net/ktnx/mobileledger/dao/TransactionDAO.java
app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java

index f7370adb506528e6336a55e94e08b3e26529013c..a79a9a63e9430183712ff674cf792c6992494f19 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
@@ -307,7 +307,7 @@ public class SendTransactionTask extends AsyncTask<LedgerTransaction, Void, Void
     @Override
     protected void onPostExecute(Void aVoid) {
         super.onPostExecute(aVoid);
-        taskCallback.done(error);
+        taskCallback.done(error, transaction);
     }
 
 }
\ No newline at end of file
index c1db43dadbe666c5cf6e5040cf7b3caf6a030bde..28f5e0eb08499585f7028143c64fb41217717c34 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019 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
@@ -18,5 +18,5 @@
 package net.ktnx.mobileledger.async;
 
 public interface TaskCallback {
-    void done(String error);
+    void done(String error, Object args);
 }
index e347207cda9ed053b36cb1481d9c62ca2527884c..8e0156b77cf1993e29abbedabe2c9f79e14a1c94 100644 (file)
@@ -17,6 +17,8 @@
 
 package net.ktnx.mobileledger.dao;
 
+import android.os.AsyncTask;
+
 import androidx.annotation.NonNull;
 import androidx.lifecycle.LiveData;
 import androidx.room.ColumnInfo;
@@ -27,10 +29,13 @@ import androidx.room.OnConflictStrategy;
 import androidx.room.Query;
 import androidx.room.Update;
 
+import net.ktnx.mobileledger.db.Account;
+import net.ktnx.mobileledger.db.AccountValue;
 import net.ktnx.mobileledger.db.DB;
 import net.ktnx.mobileledger.db.Transaction;
 import net.ktnx.mobileledger.db.TransactionAccount;
 import net.ktnx.mobileledger.db.TransactionWithAccounts;
+import net.ktnx.mobileledger.model.LedgerAccount;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
 
@@ -151,6 +156,9 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
     @Query("UPDATE transaction_accounts SET generation = :newGeneration WHERE transaction_id = " +
            ":transactionId")
     public abstract int updateAccountsGeneration(long transactionId, long newGeneration);
+
+    @Query("SELECT max(ledger_id) as ledger_id FROM transactions WHERE profile_id = :profileId")
+    public abstract LedgerIdContainer getMaxLedgerIdPOJOSync(long profileId);
     @androidx.room.Transaction
     public void updateGenerationWithAccounts(long transactionId, long newGeneration) {
         updateGeneration(transactionId, newGeneration);
@@ -163,6 +171,13 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
             return 0;
         return result.generation;
     }
+    public long getMaxLedgerIdSync(long profileId) {
+        LedgerIdContainer result = getMaxLedgerIdPOJOSync(profileId);
+
+        if (result == null)
+            return 0;
+        return result.ledgerId;
+    }
     @androidx.room.Transaction
     public void storeTransactionsSync(List<TransactionWithAccounts> list, long profileId) {
         long generation = getGenerationSync(profileId) + 1;
@@ -216,6 +231,58 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
                 trAcc.setId(trAccDao.insertSync(trAcc));
         }
     }
+    public void storeLast(TransactionWithAccounts rec) {
+        AsyncTask.execute(() -> appendSync(rec));
+    }
+    @androidx.room.Transaction
+    public void appendSync(TransactionWithAccounts rec) {
+        TransactionAccountDAO trAccDao = DB.get()
+                                           .getTransactionAccountDAO();
+        AccountDAO accDao = DB.get()
+                              .getAccountDAO();
+        AccountValueDAO accValDao = DB.get()
+                                      .getAccountValueDAO();
+
+        Transaction transaction = rec.transaction;
+        final long profileId = transaction.getProfileId();
+        transaction.setGeneration(getGenerationSync(profileId));
+        transaction.setLedgerId(getMaxLedgerIdSync(profileId) + 1);
+        transaction.setId(insertSync(transaction));
+
+        for (TransactionAccount trAcc : rec.accounts) {
+            trAcc.setTransactionId(transaction.getId());
+            trAcc.setGeneration(transaction.getGeneration());
+            trAcc.setId(trAccDao.insertSync(trAcc));
+
+            Account acc = accDao.getByNameSync(profileId, trAcc.getAccountName());
+            if (acc == null) {
+                acc = new Account();
+                acc.setProfileId(profileId);
+                acc.setName(trAcc.getAccountName());
+                acc.setNameUpper(acc.getName()
+                                    .toUpperCase());
+                acc.setParentName(LedgerAccount.extractParentName(acc.getName()));
+                acc.setLevel(LedgerAccount.determineLevel(acc.getName()));
+                acc.setGeneration(trAcc.getGeneration());
+
+                acc.setId(accDao.insertSync(acc));
+            }
+
+            AccountValue accVal = accValDao.getByCurrencySync(acc.getId(), trAcc.getCurrency());
+            if (accVal == null) {
+                accVal = new AccountValue();
+                accVal.setAccountId(acc.getId());
+                accVal.setGeneration(trAcc.getGeneration());
+                accVal.setCurrency(trAcc.getCurrency());
+                accVal.setValue(trAcc.getAmount());
+                accVal.setId(accValDao.insertSync(accVal));
+            }
+            else {
+                accVal.setValue(accVal.getValue() + trAcc.getAmount());
+                accValDao.updateSync(accVal);
+            }
+        }
+    }
     static class TransactionGenerationContainer {
         @ColumnInfo
         long generation;
@@ -224,6 +291,14 @@ public abstract class TransactionDAO extends BaseDAO<Transaction> {
         }
     }
 
+    static class LedgerIdContainer {
+        @ColumnInfo(name = "ledger_id")
+        long ledgerId;
+        public LedgerIdContainer(long ledgerId) {
+            this.ledgerId = ledgerId;
+        }
+    }
+
     static public class DescriptionContainer {
         @ColumnInfo
         public String description;
index 1e2a1a4f6834ed306d1ffb948715f26379c5df7f..06ac4da110f61acb3a73c1309f3da1332a88f671 100644 (file)
@@ -29,6 +29,7 @@ import java.util.List;
 import java.util.regex.Pattern;
 
 public class LedgerAccount {
+    private static final char ACCOUNT_DELIMITER = ':';
     static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
     private final LedgerAccount parent;
     private long dbId;
@@ -51,7 +52,7 @@ public class LedgerAccount {
     }
     @Nullable
     public static String extractParentName(@NonNull String accName) {
-        int colonPos = accName.lastIndexOf(':');
+        int colonPos = accName.lastIndexOf(ACCOUNT_DELIMITER);
         if (colonPos < 0)
             return null;    // no parent account -- this is a top-level account
         else
@@ -73,6 +74,15 @@ public class LedgerAccount {
 
         return res;
     }
+    public static int determineLevel(String accName) {
+        int level = 0;
+        int delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER);
+        while (delimiterPosition >= 0) {
+            level++;
+            delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER, delimiterPosition + 1);
+        }
+        return level;
+    }
     @Override
     public int hashCode() {
         return name.hashCode();
index 791bbcefa5a5cc44300caac28c04b133af08ece0..b9e41be9b4aaf875ac57419f7050f3afc212f18b 100644 (file)
@@ -198,14 +198,23 @@ public class NewTransactionActivity extends ProfileThemedActivity
                 getResources().getDisplayMetrics()));
     }
     @Override
-    public void done(String error) {
+    public void done(String error, Object arg) {
         Bundle b = new Bundle();
         if (error != null) {
             b.putString("error", error);
             navController.navigate(R.id.action_newTransactionSavingFragment_Failure, b);
         }
-        else
+        else {
             navController.navigate(R.id.action_newTransactionSavingFragment_Success, b);
+
+            AsyncTask.execute(() -> commitToDb((LedgerTransaction) arg));
+        }
+    }
+    public void commitToDb(LedgerTransaction tr) {
+        TransactionWithAccounts dbTransaction = tr.toDBO();
+        DB.get()
+          .getTransactionDAO()
+          .appendSync(dbTransaction);
     }
     public boolean onToggleSimulateSaveMenuItemClicked(MenuItem item) {
         model.toggleSimulateSave();
index ad9c75cd87282307581f5679d5825095734e4110..2bcf82fb8d46ce3d54d193436087eaf4bba83ba4 100644 (file)
@@ -426,7 +426,6 @@ public class NewTransactionModel extends ViewModel {
         List<Item> list = Objects.requireNonNull(items.getValue());
         TransactionHead head = list.get(0)
                                    .toTransactionHead();
-        SimpleDate date = head.getDate();
         LedgerTransaction tr = head.asLedgerTransaction();
 
         tr.setComment(head.getComment());
@@ -1027,7 +1026,8 @@ public class NewTransactionModel extends ViewModel {
             return ItemType.generalData;
         }
         public LedgerTransaction asLedgerTransaction() {
-            return new LedgerTransaction(0, date, description, Data.getProfile());
+            return new LedgerTransaction(0, (date == null) ? SimpleDate.today() : date, description,
+                    Data.getProfile());
         }
         public boolean equalContents(TransactionHead other) {
             if (other == null)