]> git.ktnx.net Git - mobile-ledger-staging.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
proper transaction parsing progress
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveTransactionsTask.java
index 371013005173c211a32644999aed851ff0a3af8a..ac9ec5093e54ece20790e6107f8fe2d593c5d9af 100644 (file)
@@ -36,14 +36,12 @@ import net.ktnx.mobileledger.model.LedgerAccount;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
-import net.ktnx.mobileledger.ui.activity.MainActivity;
 import net.ktnx.mobileledger.utils.NetworkUtil;
 
 import java.io.BufferedReader;
 import java.io.IOException;
 import java.io.InputStream;
 import java.io.InputStreamReader;
-import java.lang.ref.WeakReference;
 import java.net.HttpURLConnection;
 import java.net.MalformedURLException;
 import java.net.URLDecoder;
@@ -72,15 +70,13 @@ public class RetrieveTransactionsTask
     private static final Pattern reEnd = Pattern.compile("\\bid=\"addmodal\"");
     private static final Pattern reDecimalPoint = Pattern.compile("\\.\\d\\d?$");
     private static final Pattern reDecimalComma = Pattern.compile(",\\d\\d?$");
-    private WeakReference<MainActivity> contextRef;
     // %3A is '='
     private Pattern reAccountName = Pattern.compile("/register\\?q=inacct%3A([a-zA-Z0-9%]+)\"");
     private Pattern reAccountValue = Pattern.compile(
             "<span class=\"[^\"]*\\bamount\\b[^\"]*\">\\s*([-+]?[\\d.,]+)(?:\\s+(\\S+))?</span>");
     private MobileLedgerProfile profile;
-    public RetrieveTransactionsTask(WeakReference<MainActivity> contextRef,
-                                    @NonNull MobileLedgerProfile profile) {
-        this.contextRef = contextRef;
+    private int expectedPostingsCount = -1;
+    public RetrieveTransactionsTask(@NonNull MobileLedgerProfile profile) {
         this.profile = profile;
     }
     private static void L(String msg) {
@@ -119,14 +115,6 @@ public class RetrieveTransactionsTask
         Data.backgroundTaskProgress.postValue(values[0]);
     }
     @Override
-    protected void onPreExecute() {
-        super.onPreExecute();
-        MainActivity context = getContext();
-        if (context == null)
-            return;
-        context.onRetrieveStart();
-    }
-    @Override
     protected void onPostExecute(String error) {
         super.onPostExecute(error);
         Progress progress = new Progress();
@@ -144,6 +132,7 @@ public class RetrieveTransactionsTask
     private String retrieveTransactionListLegacy() throws IOException, HTTPException {
         Progress progress = Progress.indeterminate();
         progress.setState(ProgressState.RUNNING);
+        progress.setTotal(expectedPostingsCount);
         int maxTransactionId = -1;
         ArrayList<LedgerAccount> list = new ArrayList<>();
         HashMap<String, LedgerAccount> map = new HashMap<>();
@@ -276,7 +265,8 @@ public class RetrieveTransactionsTask
                             progress.setProgress(++processedTransactionCount);
                             if (maxTransactionId < transactionId)
                                 maxTransactionId = transactionId;
-                            if ((progress.isIndeterminate()) || (progress.getTotal() < transactionId))
+                            if ((progress.isIndeterminate()) ||
+                                (progress.getTotal() < transactionId))
                                 progress.setTotal(transactionId);
                             publishProgress(progress);
                         }
@@ -405,6 +395,7 @@ public class RetrieveTransactionsTask
                 throw new IOException(String.format("HTTP error %d", http.getResponseCode()));
 
             AccountListParser parser = new AccountListParser(resp);
+            expectedPostingsCount = 0;
 
             while (true) {
                 throwIfCancelled();
@@ -412,7 +403,7 @@ public class RetrieveTransactionsTask
                 if (parsedAccount == null) {
                     break;
                 }
-
+                expectedPostingsCount += parsedAccount.getAnumpostings();
                 final String accName = parsedAccount.getAname();
                 LedgerAccount acc = map.get(accName);
                 if (acc != null)
@@ -474,6 +465,7 @@ public class RetrieveTransactionsTask
     private boolean retrieveTransactionList() throws IOException, ParseException, HTTPException {
         Progress progress = new Progress();
         int maxTransactionId = Data.transactions.size();
+        progress.setTotal(expectedPostingsCount);
 
         HttpURLConnection http = NetworkUtil.prepareConnection(profile, "transactions");
         http.setAllowUserInteraction(false);
@@ -492,7 +484,7 @@ public class RetrieveTransactionsTask
 
             TransactionListParser parser = new TransactionListParser(resp);
 
-            int processedTransactionCount = 0;
+            int processedPostings = 0;
 
             while (true) {
                 throwIfCancelled();
@@ -504,7 +496,8 @@ public class RetrieveTransactionsTask
                 LedgerTransaction transaction = parsedTransaction.asLedgerTransaction();
                 trList.add(transaction);
 
-                progress.setProgress(++processedTransactionCount);
+                progress.setProgress(processedPostings += transaction.getAccounts()
+                                                                     .size());
                 publishProgress(progress);
             }
 
@@ -548,9 +541,6 @@ public class RetrieveTransactionsTask
             Data.backgroundTaskFinished();
         }
     }
-    private MainActivity getContext() {
-        return contextRef.get();
-    }
     private void throwIfCancelled() {
         if (isCancelled())
             throw new OperationCanceledException(null);
@@ -600,6 +590,7 @@ public class RetrieveTransactionsTask
         protected void setTotal(int total) {
             this.total = total;
             state = ProgressState.RUNNING;
+            indeterminate = total == -1;
         }
         private void ensureState(ProgressState wanted) {
             if (state != wanted)