]> git.ktnx.net Git - mobile-ledger.git/commitdiff
update description list after transaction retrieval
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 9 Jan 2019 05:25:10 +0000 (05:25 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 9 Jan 2019 05:25:10 +0000 (05:25 +0000)
app/src/main/java/net/ktnx/mobileledger/async/RefreshDescriptionsTask.java [new file with mode: 0644]
app/src/main/java/net/ktnx/mobileledger/model/Data.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/MainActivity.java

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 (file)
index 0000000..cb15980
--- /dev/null
@@ -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 <https://www.gnu.org/licenses/>.
+ */
+
+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<Void, Void, Void> {
+    @Override
+    protected Void doInBackground(Void... voids) {
+        Map<String, Boolean> 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;
+    }
+}
index 13998c7205210368854e41aaa5ca6f32abf0e598..0deda2f4592020ddbad2b3435149068f0ea1e00b 100644 (file)
@@ -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<ArrayList<LedgerAccount>> accounts =
-            new ObservableValue<>(new ArrayList<LedgerAccount>());
-    public static ObservableValue<List<String>> descriptions = new ObservableValue<>();
+            new ObservableValue<>(new ArrayList<>());
     public static ObservableAtomicInteger backgroundTaskCount = new ObservableAtomicInteger(0);
     public static ObservableValue<Date> lastUpdateDate = new ObservableValue<>();
     public static ObservableValue<MobileLedgerProfile> profile = new ObservableValue<>();
index 931f783582bd6b95ba1ab62965ab0640033b5dff..2c9724a9672a38e1f460f0f1cf74c8245a16d28d 100644 (file)
@@ -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);