]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
migrate a bunch of globals to LiveData
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveTransactionsTask.java
index 3dd291ec29db9cbebeb9401f2416297d0d6a5045..9db9c9dfdce8426b0df7275c7246c6515026f08f 100644 (file)
@@ -21,7 +21,6 @@ import android.annotation.SuppressLint;
 import android.database.sqlite.SQLiteDatabase;
 import android.os.AsyncTask;
 import android.os.OperationCanceledException;
-import android.util.Log;
 
 import net.ktnx.mobileledger.err.HTTPException;
 import net.ktnx.mobileledger.json.AccountListParser;
@@ -50,10 +49,13 @@ import java.nio.charset.StandardCharsets;
 import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.Locale;
 import java.util.Stack;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import static net.ktnx.mobileledger.utils.Logger.debug;
+
 
 public class RetrieveTransactionsTask
         extends AsyncTask<Void, RetrieveTransactionsTask.Progress, String> {
@@ -67,16 +69,18 @@ public class RetrieveTransactionsTask
             Pattern.compile("^\\s+(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)(?:\\s+(\\S+)$)?");
     private static final Pattern reEnd = Pattern.compile("\\bid=\"addmodal\"");
     private WeakReference<MainActivity> contextRef;
-    private int error;
     // %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>");
-    public RetrieveTransactionsTask(WeakReference<MainActivity> contextRef) {
+    private MobileLedgerProfile profile;
+    public RetrieveTransactionsTask(WeakReference<MainActivity> contextRef,
+                                    MobileLedgerProfile profile) {
         this.contextRef = contextRef;
+        this.profile = profile;
     }
     private static void L(String msg) {
-        //Log.d("transaction-parser", msg);
+        //debug("transaction-parser", msg);
     }
     @Override
     protected void onProgressUpdate(Progress... values) {
@@ -106,7 +110,7 @@ public class RetrieveTransactionsTask
         if (context == null) return;
         context.onRetrieveDone(null);
     }
-    private String retrieveTransactionListLegacy(MobileLedgerProfile profile)
+    private String retrieveTransactionListLegacy()
             throws IOException, ParseException, HTTPException {
         Progress progress = new Progress();
         int maxTransactionId = Progress.INDETERMINATE;
@@ -151,7 +155,7 @@ public class RetrieveTransactionsTask
                         m = reComment.matcher(line);
                         if (m.find()) {
                             // TODO: comments are ignored for now
-                            Log.v("transaction-parser", "Ignoring comment");
+//                            Log.v("transaction-parser", "Ignoring comment");
                             continue;
                         }
                         //L(String.format("State is %d", updating));
@@ -265,7 +269,8 @@ public class RetrieveTransactionsTask
                                 if (m.find()) {
                                     transactionId = Integer.valueOf(m.group(1));
                                     state = ParserState.EXPECTING_TRANSACTION_DESCRIPTION;
-                                    L(String.format("found transaction %d → expecting description",
+                                    L(String.format(Locale.ENGLISH,
+                                            "found transaction %d → expecting description",
                                             transactionId));
                                     progress.setProgress(++processedTransactionCount);
                                     if (maxTransactionId < transactionId)
@@ -302,9 +307,9 @@ public class RetrieveTransactionsTask
                                         return String.format("Error parsing date '%s'", date);
                                     }
                                     state = ParserState.EXPECTING_TRANSACTION_DETAILS;
-                                    L(String.format("transaction %d created for %s (%s) →" +
-                                                    " expecting details", transactionId, date,
-                                            m.group(2)));
+                                    L(String.format(Locale.ENGLISH,
+                                            "transaction %d created for %s (%s) →" +
+                                            " expecting details", transactionId, date, m.group(2)));
                                 }
                                 break;
 
@@ -355,8 +360,8 @@ public class RetrieveTransactionsTask
                                         transaction.addAccount(
                                                 new LedgerTransactionAccount(acc_name,
                                                         Float.valueOf(amount), currency));
-                                        L(String.format("%d: %s = %s", transaction.getId(),
-                                                acc_name, amount));
+                                        L(String.format(Locale.ENGLISH, "%d: %s = %s",
+                                                transaction.getId(), acc_name, amount));
                                     }
                                     else throw new IllegalStateException(String.format(
                                             "Can't parse transaction %d " + "details: %s",
@@ -391,7 +396,7 @@ public class RetrieveTransactionsTask
                 new String[]{profile.getUuid()});
         db.execSQL("update accounts set keep=0 where profile=?;", new String[]{profile.getUuid()});
     }
-    private boolean retrieveAccountList(MobileLedgerProfile profile)
+    private boolean retrieveAccountList()
             throws IOException, HTTPException {
         Progress progress = new Progress();
 
@@ -479,7 +484,7 @@ public class RetrieveTransactionsTask
 
         return true;
     }
-    private boolean retrieveTransactionList(MobileLedgerProfile profile)
+    private boolean retrieveTransactionList()
             throws IOException, ParseException, HTTPException {
         Progress progress = new Progress();
         int maxTransactionId = Progress.INDETERMINATE;
@@ -526,14 +531,14 @@ public class RetrieveTransactionsTask
                     if (transactionOrder == DetectedTransactionOrder.UNKNOWN) {
                         if (orderAccumulator > 30) {
                             transactionOrder = DetectedTransactionOrder.FILE;
-                            Log.d("rtt", String.format(
+                            debug("rtt", String.format(Locale.ENGLISH,
                                     "Detected native file order after %d transactions (factor %d)",
                                     processedTransactionCount, orderAccumulator));
                             progress.setTotal(Data.transactions.size());
                         }
                         else if (orderAccumulator < -30) {
                             transactionOrder = DetectedTransactionOrder.REVERSE_CHRONOLOGICAL;
-                            Log.d("rtt", String.format(
+                            debug("rtt", String.format(Locale.ENGLISH,
                                     "Detected reverse chronological order after %d transactions (factor %d)",
                                     processedTransactionCount, orderAccumulator));
                         }
@@ -587,11 +592,10 @@ public class RetrieveTransactionsTask
     @SuppressLint("DefaultLocale")
     @Override
     protected String doInBackground(Void... params) {
-        MobileLedgerProfile profile = Data.profile.get();
         Data.backgroundTaskStarted();
         try {
-            if (!retrieveAccountList(profile) || !retrieveTransactionList(profile))
-                return retrieveTransactionListLegacy(profile);
+            if (!retrieveAccountList() || !retrieveTransactionList())
+                return retrieveTransactionListLegacy();
             return null;
         }
         catch (MalformedURLException e) {