]> git.ktnx.net Git - mobile-ledger.git/commitdiff
on-demand loading of transaction details
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 16 Dec 2018 05:56:45 +0000 (05:56 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 16 Dec 2018 05:56:45 +0000 (05:56 +0000)
speeds up the initial list display quite a bit

app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java
app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java

index 4b8a87084fbd7eddfa6035ff57e092f235ee9b44..b82e84ca59188cb16989e0c80c1979dc36ac3d85 100644 (file)
@@ -19,6 +19,7 @@ package net.ktnx.mobileledger;
 
 import android.content.Context;
 import android.content.res.Resources;
+import android.database.sqlite.SQLiteDatabase;
 import android.os.Build;
 import android.support.annotation.NonNull;
 import android.support.v7.widget.RecyclerView;
@@ -27,10 +28,14 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.widget.LinearLayout;
 import android.widget.TableLayout;
+import android.widget.TableRow;
 import android.widget.TextView;
 
 import net.ktnx.mobileledger.model.LedgerTransaction;
+import net.ktnx.mobileledger.model.LedgerTransactionItem;
+import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
 
+import java.util.Iterator;
 import java.util.List;
 
 class TransactionListAdapter
@@ -45,34 +50,42 @@ class TransactionListAdapter
         LedgerTransaction tr = transactions.get(position);
         Context ctx = holder.row.getContext();
         Resources rm = ctx.getResources();
+        try (MobileLedgerDatabase dbh = new MobileLedgerDatabase(ctx)) {
+            try (SQLiteDatabase db = dbh.getReadableDatabase()) {
+                tr.loadData(db);
 
-        holder.tvDescription.setText(String.format("%s\n%s", tr.getDescription(), tr.getDate()));
-        TableLayout tbl = holder.row.findViewById(R.id.transaction_row_acc_amounts);
-        tbl.removeAllViews();
-        for (Iterator<LedgerTransactionItem> it = tr.getItemsIterator(); it.hasNext(); ) {
-            LedgerTransactionItem acc = it.next();
-            TableRow row = new TableRow(holder.row.getContext());
-            TextView child = new TextView(ctx);
-            child.setText(acc.getShortAccountName());
-            row.addView(child);
-            child = new TextView(ctx);
-            child.setText(acc.toString());
-            row.addView(child);
-            tbl.addView(row);
-        }
+                holder.tvDescription
+                        .setText(String.format("%s\n%s", tr.getDescription(), tr.getDate()));
+                TableLayout tbl = holder.row.findViewById(R.id.transaction_row_acc_amounts);
+                tbl.removeAllViews();
+                for (Iterator<LedgerTransactionItem> it = tr.getItemsIterator(); it.hasNext(); ) {
+                    LedgerTransactionItem acc = it.next();
+                    TableRow row = new TableRow(holder.row.getContext());
+                    TextView child = new TextView(ctx);
+                    child.setText(acc.getShortAccountName());
+                    row.addView(child);
+                    child = new TextView(ctx);
+                    child.setText(acc.toString());
+                    row.addView(child);
+                    tbl.addView(row);
+                }
 
-        if (position % 2 == 0) {
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
-                    .setBackgroundColor(rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
-            else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
-        }
-        else {
-            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
-                    .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
-            else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
-        }
+                if (position % 2 == 0) {
+                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
+                            .setBackgroundColor(
+                                    rm.getColor(R.color.table_row_even_bg, ctx.getTheme()));
+                    else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_even_bg));
+                }
+                else {
+                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
+                            .setBackgroundColor(
+                                    rm.getColor(R.color.drawer_background, ctx.getTheme()));
+                    else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
+                }
 
-        holder.row.setTag(R.id.POS, position);
+                holder.row.setTag(R.id.POS, position);
+            }
+        }
     }
 
     @NonNull
index 8aa169dc05a6893a8d3d2f54be37eef5daa30d89..e51d40038a499b304c938a150d8a3d9b212aa233 100644 (file)
@@ -17,6 +17,7 @@
 
 package net.ktnx.mobileledger.model;
 
+import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 
 import net.ktnx.mobileledger.utils.Digest;
@@ -45,12 +46,14 @@ public class LedgerTransaction {
     private String description;
     private ArrayList<LedgerTransactionItem> items;
     private String dataHash;
+    private boolean dataLoaded;
     public LedgerTransaction(String id, String date, String description) {
         this.id = id;
         this.date = date;
         this.description = description;
         this.items = new ArrayList<>();
         this.dataHash = null;
+        dataLoaded = false;
     }
     public LedgerTransaction(int id, String date, String description) {
         this(String.valueOf(id), date, description);
@@ -58,6 +61,9 @@ public class LedgerTransaction {
     public LedgerTransaction(String date, String description) {
         this(null, date, description);
     }
+    public LedgerTransaction(int id) {
+        this(id, null, null);
+    }
     public void add_item(LedgerTransactionItem item) {
         items.add(item);
         dataHash = null;
@@ -128,4 +134,27 @@ public class LedgerTransaction {
                     String.format("Unable to get instance of %s digest", DIGEST_TYPE), e);
         }
     }
+    public void loadData(SQLiteDatabase db) {
+        if (dataLoaded) return;
+
+        try (Cursor cTr = db.rawQuery("SELECT date, description from transactions WHERE " +
+                                         "id=?",new String[]{id})) {
+            if (cTr.moveToFirst()) {
+                date = cTr.getString(0);
+                description = cTr.getString(1);
+
+                try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
+                                               "transaction_accounts WHERE transaction_id = ?",
+                        new String[]{id}))
+                {
+                    while (cAcc.moveToNext()) {
+                        add_item(
+                                new LedgerTransactionItem(cAcc.getString(0), cAcc.getFloat(1),
+                                        cAcc.getString(2)));
+                    }
+                }
+            }
+        }
+
+    }
 }
index d0c979e6df9b06ea6325fa80c3e906f5f3a78e62..1482c1d59d991b7f3bc2c459db9d72d7da315038 100644 (file)
@@ -22,7 +22,6 @@ import android.database.Cursor;
 import android.database.sqlite.SQLiteDatabase;
 
 import net.ktnx.mobileledger.model.LedgerTransaction;
-import net.ktnx.mobileledger.model.LedgerTransactionItem;
 import net.ktnx.mobileledger.utils.MobileLedgerDatabase;
 
 import java.util.ArrayList;
@@ -42,26 +41,12 @@ public class TransactionListViewModel extends ViewModel {
     }
     private void reloadTransactions(MobileLedgerDatabase dbh) {
         transactions.clear();
-        String sql = "SELECT id, date, description FROM transactions";
-        sql += " ORDER BY date desc, id desc";
+        String sql = "SELECT id FROM transactions ORDER BY date desc, id desc";
 
         try (SQLiteDatabase db = dbh.getReadableDatabase()) {
             try (Cursor cursor = db.rawQuery(sql, null)) {
                 while (cursor.moveToNext()) {
-                    LedgerTransaction tr =
-                            new LedgerTransaction(cursor.getString(0), cursor.getString(1),
-                                    cursor.getString(2));
-                    try (Cursor cAcc = db.rawQuery("SELECT account_name, amount, currency FROM " +
-                                                   "transaction_accounts WHERE transaction_id = ?",
-                            new String[]{tr.getId()}))
-                    {
-                        while (cAcc.moveToNext()) {
-                            tr.add_item(
-                                    new LedgerTransactionItem(cAcc.getString(0), cAcc.getFloat(1),
-                                            cAcc.getString(2)));
-                        }
-                    }
-                    transactions.add(tr);
+                    transactions.add(new LedgerTransaction(cursor.getInt(0)));
                 }
             }
         }