/*
- * 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
import android.util.Log;
import net.ktnx.mobileledger.R;
-import net.ktnx.mobileledger.TransactionListActivity;
import net.ktnx.mobileledger.model.LedgerTransaction;
import net.ktnx.mobileledger.model.LedgerTransactionAccount;
+import net.ktnx.mobileledger.ui.transaction_list.TransactionListFragment;
import net.ktnx.mobileledger.utils.MLDB;
import net.ktnx.mobileledger.utils.NetworkUtil;
private static final Pattern transactionDetailsPattern =
Pattern.compile("^\\s+" + "(\\S[\\S\\s]+\\S)\\s\\s+([-+]?\\d[\\d,.]*)(?:\\s+(\\S+)$)?");
private static final Pattern endPattern = Pattern.compile("\\bid=\"addmodal\"");
- protected WeakReference<TransactionListActivity> contextRef;
+ protected WeakReference<TransactionListFragment> contextRef;
protected int error;
private boolean success;
- public RetrieveTransactionsTask(WeakReference<TransactionListActivity> contextRef) {
+ public RetrieveTransactionsTask(WeakReference<TransactionListFragment> contextRef) {
this.contextRef = contextRef;
}
private static final void L(String msg) {
@Override
protected void onProgressUpdate(Progress... values) {
super.onProgressUpdate(values);
- TransactionListActivity context = getContext();
+ TransactionListFragment context = getContext();
if (context == null) return;
context.onRetrieveProgress(values[0]);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
- TransactionListActivity context = getContext();
+ TransactionListFragment context = getContext();
if (context == null) return;
context.onRetrieveStart();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
- TransactionListActivity context = getContext();
+ TransactionListFragment context = getContext();
if (context == null) return;
context.onRetrieveDone(success);
}
+ @Override
+ protected void onCancelled() {
+ super.onCancelled();
+ TransactionListFragment context = getContext();
+ if (context == null) return;
+ context.onRetrieveDone(false);
+ }
@SuppressLint("DefaultLocale")
@Override
protected Void doInBackground(Params... params) {
NetworkUtil.prepare_connection(params[0].getBackendPref(), "journal");
http.setAllowUserInteraction(false);
publishProgress(progress);
- TransactionListActivity ctx = contextRef.get();
+ TransactionListFragment ctx = getContext();
if (ctx == null) return null;
- try (SQLiteDatabase db = MLDB.getWritableDatabase(ctx)) {
+ try (SQLiteDatabase db = MLDB.getWritableDatabase()) {
try (InputStream resp = http.getInputStream()) {
if (http.getResponseCode() != 200) throw new IOException(
String.format("HTTP error %d", http.getResponseCode()));
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"));
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()) {
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()) {
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)) {
break;
default:
throw new RuntimeException(
- String.format("Unknown parser state %d", state));
+ String.format("Unknown parser state %s", state.name()));
}
}
if (!isCancelled()) {
if (success && !isCancelled()) {
Log.d("db", "Updating transaction list stamp");
- MLDB.set_option_value(ctx, MLDB.OPT_TRANSACTION_LIST_STAMP, new Date().getTime());
+ MLDB.set_option_value(MLDB.OPT_TRANSACTION_LIST_STAMP,
+ new Date().getTime());
ctx.model.reloadTransactions(ctx);
}
}
}
return null;
}
- TransactionListActivity getContext() {
+ TransactionListFragment getContext() {
return contextRef.get();
}
+ private enum ParserState {
+ EXPECTING_JOURNAL, EXPECTING_TRANSACTION, EXPECTING_TRANSACTION_DESCRIPTION,
+ EXPECTING_TRANSACTION_DETAILS
+ }
+
public static class Params {
private SharedPreferences backendPref;
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;
- }
}