X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fasync%2FRetrieveTransactionsTask.java;h=dc73944332b728ccae78e798089fa71c11d9a35c;hp=e81a71c2c87b098cb6bcaa072790f191a3493e0f;hb=abd5a19252bf81af903c3406132030e3ad63704f;hpb=c040f83305fb25dfe9a7c088683926b7d101c373 diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java index e81a71c2..dc739443 100644 --- a/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java +++ b/app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java @@ -112,7 +112,7 @@ public class RetrieveTransactionsTask int maxTransactionId = Progress.INDETERMINATE; ArrayList accountList = new ArrayList<>(); HashMap accountNames = new HashMap<>(); - LedgerAccount lastAccount = null; + LedgerAccount lastAccount = null, prevAccount = null; boolean onlyStarred = Data.optShowOnlyStarred.get(); HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal"); @@ -124,7 +124,7 @@ public class RetrieveTransactionsTask default: throw new HTTPException(http.getResponseCode(), http.getResponseMessage()); } - try (SQLiteDatabase db = MLDB.getWritableDatabase()) { + try (SQLiteDatabase db = MLDB.getDatabase()) { try (InputStream resp = http.getInputStream()) { if (http.getResponseCode() != 200) throw new IOException(String.format("HTTP error %d", http.getResponseCode())); @@ -169,12 +169,15 @@ public class RetrieveTransactionsTask acct_name = acct_name.replace("\"", ""); L(String.format("found account: %s", acct_name)); + prevAccount = lastAccount; lastAccount = profile.tryLoadAccount(db, acct_name); if (lastAccount == null) lastAccount = new LedgerAccount(acct_name); else lastAccount.removeAmounts(); profile.storeAccount(db, lastAccount); + if (prevAccount != null) prevAccount + .setHasSubAccounts(prevAccount.isParentOf(lastAccount)); // make sure the parent account(s) are present, // synthesising them if necessary String parentName = lastAccount.getParentName(); @@ -190,6 +193,7 @@ public class RetrieveTransactionsTask String aName = toAppend.pop(); LedgerAccount acc = new LedgerAccount(aName); acc.setHiddenByStar(lastAccount.isHiddenByStar()); + acc.setHasSubAccounts(true); if ((!onlyStarred || !acc.isHiddenByStar()) && acc.isVisible(accountList)) accountList.add(acc); L(String.format("gap-filling with %s", aName)); @@ -379,45 +383,53 @@ public class RetrieveTransactionsTask throw new HTTPException(http.getResponseCode(), http.getResponseMessage()); } publishProgress(progress); - try (SQLiteDatabase db = MLDB.getWritableDatabase()) { - try (InputStream resp = http.getInputStream()) { - if (http.getResponseCode() != 200) - throw new IOException(String.format("HTTP error %d", http.getResponseCode())); + SQLiteDatabase db = MLDB.getDatabase(); + try (InputStream resp = http.getInputStream()) { + if (http.getResponseCode() != 200) + throw new IOException(String.format("HTTP error %d", http.getResponseCode())); - db.beginTransaction(); - try { - profile.markAccountsAsNotPresent(db); + db.beginTransaction(); + try { + profile.markAccountsAsNotPresent(db); - AccountListParser parser = new AccountListParser(resp); - ArrayList accountList = new ArrayList<>(); + AccountListParser parser = new AccountListParser(resp); + ArrayList accountList = new ArrayList<>(); - while (true) { - throwIfCancelled(); - ParsedLedgerAccount parsedAccount = parser.nextAccount(); - if (parsedAccount == null) break; + LedgerAccount prevAccount = null; - LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname()); - if (acc == null) acc = new LedgerAccount(parsedAccount.getAname()); - else acc.removeAmounts(); + while (true) { + throwIfCancelled(); + ParsedLedgerAccount parsedAccount = parser.nextAccount(); + if (parsedAccount == null) break; - profile.storeAccount(db, acc); - for (ParsedBalance b : parsedAccount.getAebalance()) { - profile.storeAccountValue(db, acc.getName(), b.getAcommodity(), - b.getAquantity().asFloat()); - } + LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname()); + if (acc == null) acc = new LedgerAccount(parsedAccount.getAname()); + else acc.removeAmounts(); - if (acc.isVisible(accountList)) accountList.add(acc); + profile.storeAccount(db, acc); + for (ParsedBalance b : parsedAccount.getAebalance()) { + profile.storeAccountValue(db, acc.getName(), b.getAcommodity(), + b.getAquantity().asFloat()); } - throwIfCancelled(); - profile.deleteNotPresentAccounts(db); - throwIfCancelled(); - db.setTransactionSuccessful(); - Data.accounts.set(accountList); - } - finally { - db.endTransaction(); + if (acc.isVisible(accountList)) accountList.add(acc); + + if (prevAccount != null) { + prevAccount.setHasSubAccounts( + acc.getName().startsWith(prevAccount.getName() + ":")); + } + + prevAccount = acc; } + throwIfCancelled(); + + profile.deleteNotPresentAccounts(db); + throwIfCancelled(); + db.setTransactionSuccessful(); + Data.accounts.set(accountList); + } + finally { + db.endTransaction(); } } @@ -439,7 +451,7 @@ public class RetrieveTransactionsTask default: throw new HTTPException(http.getResponseCode(), http.getResponseMessage()); } - try (SQLiteDatabase db = MLDB.getWritableDatabase()) { + try (SQLiteDatabase db = MLDB.getDatabase()) { try (InputStream resp = http.getInputStream()) { if (http.getResponseCode() != 200) throw new IOException(String.format("HTTP error %d", http.getResponseCode()));