package net.ktnx.mobileledger.async;
import android.annotation.SuppressLint;
-import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.os.AsyncTask;
-import android.util.Log;
import net.ktnx.mobileledger.R;
import net.ktnx.mobileledger.TransactionListActivity;
this.contextRef = contextRef;
}
private static final void L(String msg) {
- Log.d("transaction-parser", msg);
+// Log.d("transaction-parser", msg);
}
@Override
protected void onProgressUpdate(Progress... values) {
@Override
protected Void doInBackground(Params... params) {
Progress progress = new Progress();
+ int maxTransactionId = Progress.INDETERMINATE;
success = false;
try {
HttpURLConnection http =
NetworkUtil.prepare_connection(params[0].getBackendPref(), "journal");
http.setAllowUserInteraction(false);
publishProgress(progress);
- Context ctx = contextRef.get();
+ TransactionListActivity ctx = contextRef.get();
if (ctx == null) return null;
try (SQLiteDatabase db = MLDB.getWritableDatabase(ctx)) {
try (InputStream resp = http.getInputStream()) {
String.format("HTTP error %d", http.getResponseCode()));
db.beginTransaction();
try {
- db.execSQL("DELETE FROM transactions;");
- db.execSQL("DELETE FROM transaction_accounts");
+ db.execSQL("UPDATE transactions set keep=0");
int state = ParserState.EXPECTING_JOURNAL;
String line;
BufferedReader buf =
new BufferedReader(new InputStreamReader(resp, "UTF-8"));
- int transactionCount = 0;
+ int processedTransactionCount = 0;
int transactionId = 0;
+ int matchedTransactionsCount = 0;
LedgerTransaction transaction = null;
LINES:
while ((line = buf.readLine()) != null) {
L(String.format(
"found transaction %d → expecting " + "description",
transactionId));
- progress.setProgress(++transactionCount);
- if (progress.getTotal() == Progress.INDETERMINATE)
+ progress.setProgress(++processedTransactionCount);
+ if (maxTransactionId < transactionId)
+ maxTransactionId = transactionId;
+ if ((progress.getTotal() == Progress.INDETERMINATE) ||
+ (progress.getTotal() < transactionId))
progress.setTotal(transactionId);
publishProgress(progress);
}
case ParserState.EXPECTING_TRANSACTION_DETAILS:
if (line.isEmpty()) {
// transaction data collected
- transaction.insertInto(db);
+ if (transaction.existsInDb(db)) {
+ db.execSQL("UPDATE transactions SET keep = 1 WHERE id" +
+ "=?", new Integer[]{transaction.getId()});
+ matchedTransactionsCount++;
+
+ if (matchedTransactionsCount == 100) {
+ db.execSQL("UPDATE transactions SET keep=1 WHERE " +
+ "id < ?",
+ new Integer[]{transaction.getId()});
+ progress.setTotal(progress.getProgress());
+ publishProgress(progress);
+ break LINES;
+ }
+ }
+ else {
+ db.execSQL("DELETE from transactions WHERE id=?",
+ new Integer[]{transaction.getId()});
+ db.execSQL("DELETE from transaction_accounts WHERE " +
+ "transaction_id=?",
+ new Integer[]{transaction.getId()});
+ transaction.insertInto(db);
+ matchedTransactionsCount = 0;
+ progress.setTotal(maxTransactionId);
+ }
state = ParserState.EXPECTING_TRANSACTION;
L(String.format(
String.format("Unknown " + "parser state %d", state));
}
}
- if (!isCancelled()) db.setTransactionSuccessful();
+ if (!isCancelled()) {
+ db.execSQL("DELETE FROM transactions WHERE keep = 0");
+ db.setTransactionSuccessful();
+ }
}
finally {
db.endTransaction();
}
}
}
+
+ if (success && !isCancelled()) ctx.model.reloadTransactions(ctx);
}
catch (MalformedURLException e) {
error = R.string.err_bad_backend_url;