From: Damyan Ivanov Date: Wed, 9 Jan 2019 05:25:10 +0000 (+0000) Subject: update description list after transaction retrieval X-Git-Tag: v0.3~95 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=45a7596af05e4a9469081a21c054774fcea93f8a;p=mobile-ledger.git update description list after transaction retrieval --- diff --git a/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java b/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java new file mode 100644 index 00000000..cb15980f --- /dev/null +++ b/app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java @@ -0,0 +1,73 @@ +/* + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your opinion), any later version. + * + * Mobile-Ledger is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License terms for details. + * + * You should have received a copy of the GNU General Public License + * along with Mobile-Ledger. If not, see . + */ + +package net.ktnx.mobileledger.async; + +import android.database.Cursor; +import android.database.sqlite.SQLiteDatabase; +import android.os.AsyncTask; +import android.util.Log; + +import net.ktnx.mobileledger.model.Data; +import net.ktnx.mobileledger.utils.MLDB; + +import java.util.HashMap; +import java.util.Map; + +public class RefreshDescriptionsTask extends AsyncTask { + @Override + protected Void doInBackground(Void... voids) { + Map unique = new HashMap<>(); + + Log.d("descriptions", "Starting refresh"); + SQLiteDatabase db = MLDB.getWritableDatabase(); + + Data.backgroundTaskCount.incrementAndGet(); + try { + db.beginTransaction(); + try { + db.execSQL("UPDATE description_history set keep=0"); + try (Cursor c = db + .rawQuery("SELECT distinct description from transactions", null)) + { + while (c.moveToNext()) { + String description = c.getString(0); + String descriptionUpper = description.toUpperCase(); + if (unique.containsKey(descriptionUpper)) continue; + + db.execSQL( + "replace into description_history(description, description_upper, " + + "keep) values(?, ?, 1)", new String[]{description, descriptionUpper}); + unique.put(descriptionUpper, true); + } + } + db.execSQL("DELETE from description_history where keep=0"); + db.setTransactionSuccessful(); + Log.d("descriptions", "Refresh successful"); + } + finally { + db.endTransaction(); + } + } + finally { + Data.backgroundTaskCount.decrementAndGet(); + Log.d("descriptions", "Refresh done"); + } + + return null; + } +} diff --git a/app/src/main/java/net/ktnx/mobileledger/model/Data.java b/app/src/main/java/net/ktnx/mobileledger/model/Data.java index 13998c72..0deda2f4 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/Data.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/Data.java @@ -23,13 +23,11 @@ import net.ktnx.mobileledger.utils.ObservableValue; import java.util.ArrayList; import java.util.Date; -import java.util.List; public final class Data { public static TransactionList transactions = new TransactionList(); public static ObservableValue> accounts = - new ObservableValue<>(new ArrayList()); - public static ObservableValue> descriptions = new ObservableValue<>(); + new ObservableValue<>(new ArrayList<>()); public static ObservableAtomicInteger backgroundTaskCount = new ObservableAtomicInteger(0); public static ObservableValue lastUpdateDate = new ObservableValue<>(); public static ObservableValue profile = new ObservableValue<>(); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java index 931f7835..2c9724a9 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java @@ -41,6 +41,7 @@ import android.widget.ProgressBar; import android.widget.TextView; import net.ktnx.mobileledger.R; +import net.ktnx.mobileledger.async.RefreshDescriptionsTask; import net.ktnx.mobileledger.async.RetrieveTransactionsTask; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerAccount; @@ -383,6 +384,8 @@ public class MainActivity extends AppCompatActivity { public void onRetrieveDone(boolean success) { progressLayout.setVisibility(View.GONE); updateLastUpdateTextFromDB(); + + new RefreshDescriptionsTask().execute(); } public void onRetrieveStart() { progressBar.setIndeterminate(true);