From 916547239190f7daf921f2066593637cfca877fc Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 25 Apr 2021 00:04:59 +0300 Subject: [PATCH] speculatively add new transactions to the database and UI list this saves a refresh that would be need to view the new transactions and ccount balances --- .../async/SendTransactionTask.java | 4 +- .../ktnx/mobileledger/async/TaskCallback.java | 4 +- .../ktnx/mobileledger/dao/TransactionDAO.java | 75 +++++++++++++++++++ .../mobileledger/model/LedgerAccount.java | 12 ++- .../NewTransactionActivity.java | 13 +++- .../new_transaction/NewTransactionModel.java | 4 +- 6 files changed, 103 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java index f7370adb..a79a9a63 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/SendTransactionTask.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 @@ -307,7 +307,7 @@ public class SendTransactionTask extends AsyncTask { @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 { 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 list, long profileId) { long generation = getGenerationSync(profileId) + 1; @@ -216,6 +231,58 @@ public abstract class TransactionDAO extends BaseDAO { 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 { } } + static class LedgerIdContainer { + @ColumnInfo(name = "ledger_id") + long ledgerId; + public LedgerIdContainer(long ledgerId) { + this.ledgerId = ledgerId; + } + } + static public class DescriptionContainer { @ColumnInfo public String description; diff --git a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java index 1e2a1a4f..06ac4da1 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java @@ -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(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java index 791bbcef..b9e41be9 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionActivity.java @@ -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(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java index ad9c75cd..2bcf82fb 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionModel.java @@ -426,7 +426,6 @@ public class NewTransactionModel extends ViewModel { List 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) -- 2.39.5