]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
use enum for the parser state
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveTransactionsTask.java
index b8a341344fb478ab5d5202173c02a1b257e26923..0768817e5673bc1242c3ffc44040aa76492b667b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2018 Damyan Ivanov.
+ * Copyright © 2019 Damyan Ivanov.
  * This file is part of Mobile-Ledger.
  * Mobile-Ledger is free software: you can distribute it and/or modify it
  * under the term of the GNU General Public License as published by
@@ -102,7 +102,7 @@ public class RetrieveTransactionsTask extends
             publishProgress(progress);
             TransactionListFragment ctx = getContext();
             if (ctx == null) return null;
-            try (SQLiteDatabase db = MLDB.getWritableDatabase(ctx.getActivity())) {
+            try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
                 try (InputStream resp = http.getInputStream()) {
                     if (http.getResponseCode() != 200) throw new IOException(
                             String.format("HTTP error %d", http.getResponseCode()));
@@ -110,7 +110,7 @@ public class RetrieveTransactionsTask extends
                     try {
                         db.execSQL("UPDATE transactions set keep=0");
 
-                        int state = ParserState.EXPECTING_JOURNAL;
+                        ParserState state = ParserState.EXPECTING_JOURNAL;
                         String line;
                         BufferedReader buf =
                                 new BufferedReader(new InputStreamReader(resp, "UTF-8"));
@@ -125,14 +125,14 @@ public class RetrieveTransactionsTask extends
                             Matcher m;
                             //L(String.format("State is %d", state));
                             switch (state) {
-                                case ParserState.EXPECTING_JOURNAL:
+                                case EXPECTING_JOURNAL:
                                     if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
                                     if (line.equals("<h2>General Journal</h2>")) {
                                         state = ParserState.EXPECTING_TRANSACTION;
                                         L("→ expecting transaction");
                                     }
                                     break;
-                                case ParserState.EXPECTING_TRANSACTION:
+                                case EXPECTING_TRANSACTION:
                                     if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
                                     m = transactionStartPattern.matcher(line);
                                     if (m.find()) {
@@ -156,7 +156,7 @@ public class RetrieveTransactionsTask extends
                                         break LINES;
                                     }
                                     break;
-                                case ParserState.EXPECTING_TRANSACTION_DESCRIPTION:
+                                case EXPECTING_TRANSACTION_DESCRIPTION:
                                     if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
                                     m = transactionDescriptionPattern.matcher(line);
                                     if (m.find()) {
@@ -174,7 +174,7 @@ public class RetrieveTransactionsTask extends
                                                 m.group(1), m.group(2)));
                                     }
                                     break;
-                                case ParserState.EXPECTING_TRANSACTION_DETAILS:
+                                case EXPECTING_TRANSACTION_DETAILS:
                                     if (line.isEmpty()) {
                                         // transaction data collected
                                         if (transaction.existsInDb(db)) {
@@ -235,7 +235,7 @@ public class RetrieveTransactionsTask extends
                                     break;
                                 default:
                                     throw new RuntimeException(
-                                            String.format("Unknown parser state %d", state));
+                                            String.format("Unknown parser state %s", state.name()));
                             }
                         }
                         if (!isCancelled()) {
@@ -251,7 +251,7 @@ public class RetrieveTransactionsTask extends
 
             if (success && !isCancelled()) {
                 Log.d("db", "Updating transaction list stamp");
-                MLDB.set_option_value(ctx.getActivity(), MLDB.OPT_TRANSACTION_LIST_STAMP,
+                MLDB.set_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP,
                         new Date().getTime());
                 ctx.model.reloadTransactions(ctx);
             }
@@ -274,6 +274,11 @@ public class RetrieveTransactionsTask extends
         return contextRef.get();
     }
 
+    private enum ParserState {
+        EXPECTING_JOURNAL, EXPECTING_TRANSACTION, EXPECTING_TRANSACTION_DESCRIPTION,
+        EXPECTING_TRANSACTION_DETAILS
+    }
+
     public static class Params {
         private SharedPreferences backendPref;
 
@@ -315,11 +320,4 @@ public class RetrieveTransactionsTask extends
             super(message);
         }
     }
-
-    private class ParserState {
-        static final int EXPECTING_JOURNAL = 0;
-        static final int EXPECTING_TRANSACTION = 1;
-        static final int EXPECTING_TRANSACTION_DESCRIPTION = 2;
-        static final int EXPECTING_TRANSACTION_DETAILS = 3;
-    }
 }