]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
fix duplication af account amounts on refresh
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveTransactionsTask.java
index e17b40c7c62fe69d00dc8d842f72d80a701bf880..8f1a764b386aa4db91a901fcde5688ec346dd6d6 100644 (file)
@@ -107,7 +107,7 @@ 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<>();
@@ -118,6 +118,12 @@ public class RetrieveTransactionsTask
         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,11 +169,12 @@ public class RetrieveTransactionsTask
                                     acct_name = acct_name.replace("\"", "");
                                     L(String.format("found account: %s", acct_name));
 
-                                    lastAccount = profile.loadAccount(acct_name);
-                                    if (lastAccount == null) {
+                                    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);
 
                                     // make sure the parent account(s) are present,
                                     // synthesising them if necessary
@@ -183,8 +190,8 @@ public class RetrieveTransactionsTask
                                         while (!toAppend.isEmpty()) {
                                             String aName = toAppend.pop();
                                             LedgerAccount acc = new LedgerAccount(aName);
-                                            acc.setHidden(lastAccount.isHidden());
-                                            if (!onlyStarred || !acc.isHidden())
+                                            acc.setHiddenByStar(lastAccount.isHiddenByStar());
+                                            if (!onlyStarred || !acc.isHiddenByStar())
                                                 accountList.add(acc);
                                             L(String.format("gap-filling with %s", aName));
                                             accountNames.put(aName, null);
@@ -192,7 +199,7 @@ public class RetrieveTransactionsTask
                                         }
                                     }
 
-                                    if (!onlyStarred || !lastAccount.isHidden())
+                                    if (!onlyStarred || !lastAccount.isHiddenByStar())
                                         accountList.add(lastAccount);
                                     accountNames.put(acct_name, null);
 
@@ -422,9 +429,12 @@ public class RetrieveTransactionsTask
         http.setAllowUserInteraction(false);
         publishProgress(progress);
         switch (http.getResponseCode()) {
-            case 200: break;
-            case 404: return false;
-            default:  throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
+            case 200:
+                break;
+            case 404:
+                return false;
+            default:
+                throw new HTTPException(http.getResponseCode(), http.getResponseMessage());
         }
         try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
             try (InputStream resp = http.getInputStream()) {
@@ -465,6 +475,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);
                     }
@@ -503,7 +517,7 @@ public class RetrieveTransactionsTask
         }
         catch (IOException e) {
             e.printStackTrace();
-            return "Parse error";
+            return e.getLocalizedMessage();
         }
         catch (ParseException e) {
             e.printStackTrace();