]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/async/RetrieveTransactionsTask.java
move DB stuff into a static class
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / async / RetrieveTransactionsTask.java
index 60827d2130e0e6484feee8d32f529a5fca849e35..8a9a8f62e8e98c4917aab9e383828c48f496771d 100644 (file)
@@ -28,7 +28,7 @@ import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.TransactionListActivity;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionItem;
-import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
+import net.ktnx.mobileledger.utils.MLDB;
 import net.ktnx.mobileledger.utils.NetworkUtil;
 
 import java.io.BufferedReader;
@@ -94,8 +94,7 @@ public class RetrieveTransactionsTask extends
             publishProgress(progress);
             Context ctx = contextRef.get();
             if (ctx == null) return null;
-            try (MobileLedgerDatabase dbh = new MobileLedgerDatabase(ctx)) {
-                try (SQLiteDatabase db = dbh.getWritableDatabase()) {
+            try (SQLiteDatabase db = MLDB.getWritableDatabase(ctx)) {
                     try (InputStream resp = http.getInputStream()) {
                         if (http.getResponseCode() != 200) throw new IOException(
                                 String.format("HTTP error %d", http.getResponseCode()));
@@ -114,8 +113,10 @@ public class RetrieveTransactionsTask extends
                             LedgerTransaction transaction = null;
                             LINES:
                             while ((line = buf.readLine()) != null) {
+                                if (isCancelled()) break;
+                                if (!line.isEmpty() && (line.charAt(0) == ' ')) continue;
                                 Matcher m;
-                                L(String.format("State is %d", state));
+                                //L(String.format("State is %d", state));
                                 switch (state) {
                                     case ParserState.EXPECTING_JOURNAL:
                                         if (line.equals("<h2>General Journal</h2>")) {
@@ -167,6 +168,14 @@ public class RetrieveTransactionsTask extends
                                             state = ParserState.EXPECTING_TRANSACTION;
                                             L(String.format("transaction %s saved → expecting " +
                                                             "transaction", transaction.getId()));
+
+// sounds like a good idea, but transaction-1 may not be the first one chronologically
+// for example, when you add the initial seeding transaction after entering some others
+//                                            if (transactionId == 1) {
+//                                                L("This was the initial transaction. Terminating " +
+//                                                  "parser");
+//                                                break LINES;
+//                                            }
                                         }
                                         else {
                                             m = transactionDetailsPattern.matcher(line);
@@ -189,7 +198,7 @@ public class RetrieveTransactionsTask extends
                                                         state));
                                 }
                             }
-                            db.setTransactionSuccessful();
+                            if (!isCancelled()) db.setTransactionSuccessful();
                         }
                         finally {
                             db.endTransaction();