]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
fill hasSubAccounts when populating account list on refresh
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveTransactionsTask.java
index c1e51442c781e12bbf8f801edcae873947a7307c..de459ece7a4e6ef9bf615ba064a4bfb4c22fba4d 100644 (file)
@@ -107,17 +107,23 @@ public class RetrieveTransactionsTask
         context.onRetrieveDone(null);
     }
     private String retrieveTransactionListLegacy(MobileLedgerProfile profile)
-            throws IOException, ParseException {
+            throws IOException, ParseException, HTTPException {
         Progress progress = new Progress();
         int maxTransactionId = Progress.INDETERMINATE;
         ArrayList<LedgerAccount> accountList = new ArrayList<>();
         HashMap<String, Void> accountNames = new HashMap<>();
-        LedgerAccount lastAccount = null;
+        LedgerAccount lastAccount = null, prevAccount = null;
         boolean onlyStarred = Data.optShowOnlyStarred.get();
 
         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "journal");
         http.setAllowUserInteraction(false);
         publishProgress(progress);
+        switch (http.getResponseCode()) {
+            case 200:
+                break;
+            default:
+                throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
+        }
         try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
             try (InputStream resp = http.getInputStream()) {
                 if (http.getResponseCode() != 200)
@@ -163,12 +169,15 @@ public class RetrieveTransactionsTask
                                     acct_name = acct_name.replace("\"", "");
                                     L(String.format("found account: %s", acct_name));
 
-                                    lastAccount = profile.loadAccount(acct_name);
-                                    if (lastAccount == null) {
+                                    prevAccount = lastAccount;
+                                    lastAccount = profile.tryLoadAccount(db, acct_name);
+                                    if (lastAccount == null)
                                         lastAccount = new LedgerAccount(acct_name);
-                                        profile.storeAccount(db, lastAccount);
-                                    }
+                                    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();
@@ -183,16 +192,18 @@ public class RetrieveTransactionsTask
                                         while (!toAppend.isEmpty()) {
                                             String aName = toAppend.pop();
                                             LedgerAccount acc = new LedgerAccount(aName);
-                                            acc.setHidden(lastAccount.isHidden());
-                                            if (!onlyStarred || !acc.isHidden())
-                                                accountList.add(acc);
+                                            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));
                                             accountNames.put(aName, null);
                                             profile.storeAccount(db, acc);
                                         }
                                     }
 
-                                    if (!onlyStarred || !lastAccount.isHidden())
+                                    if ((!onlyStarred || !lastAccount.isHiddenByStar()) &&
+                                        lastAccount.isVisible(accountList))
                                         accountList.add(lastAccount);
                                     accountNames.put(acct_name, null);
 
@@ -384,19 +395,31 @@ public class RetrieveTransactionsTask
                     AccountListParser parser = new AccountListParser(resp);
                     ArrayList<LedgerAccount> accountList = new ArrayList<>();
 
+                    LedgerAccount prevAccount = null;
+
                     while (true) {
                         throwIfCancelled();
                         ParsedLedgerAccount parsedAccount = parser.nextAccount();
                         if (parsedAccount == null) break;
 
-                        LedgerAccount acc = new LedgerAccount(parsedAccount.getAname());
+                        LedgerAccount acc = profile.tryLoadAccount(db, parsedAccount.getAname());
+                        if (acc == null) acc = new LedgerAccount(parsedAccount.getAname());
+                        else acc.removeAmounts();
+
                         profile.storeAccount(db, acc);
                         for (ParsedBalance b : parsedAccount.getAebalance()) {
                             profile.storeAccountValue(db, acc.getName(), b.getAcommodity(),
                                     b.getAquantity().asFloat());
                         }
 
-                        accountList.add(acc);
+                        if (acc.isVisible(accountList)) accountList.add(acc);
+
+                        if (prevAccount != null) {
+                            prevAccount.setHasSubAccounts(
+                                    acc.getName().startsWith(prevAccount.getName() + ":"));
+                        }
+
+                        prevAccount = acc;
                     }
                     throwIfCancelled();
 
@@ -468,6 +491,10 @@ public class RetrieveTransactionsTask
                             progress.setTotal(maxTransactionId);
                         }
 
+                        if ((progress.getTotal() == Progress.INDETERMINATE) ||
+                            (progress.getTotal() < transaction.getId()))
+                            progress.setTotal(transaction.getId());
+
                         progress.setProgress(++processedTransactionCount);
                         publishProgress(progress);
                     }
@@ -506,7 +533,7 @@ public class RetrieveTransactionsTask
         }
         catch (IOException e) {
             e.printStackTrace();
-            return "Parse error";
+            return e.getLocalizedMessage();
         }
         catch (ParseException e) {
             e.printStackTrace();