From: Damyan Ivanov Date: Sun, 16 Dec 2018 05:56:45 +0000 (+0000) Subject: on-demand loading of transaction details X-Git-Tag: v0.3~220 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=0250439d60e198f47f86ab4091027ec0d772d4c9;p=mobile-ledger.git on-demand loading of transaction details speeds up the initial list display quite a bit --- diff --git a/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java b/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java index 4b8a8708..b82e84ca 100644 --- a/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java @@ -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 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 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 diff --git a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java index 8aa169dc..e51d4003 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerTransaction.java @@ -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 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))); + } + } + } + } + + } } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java index d0c979e6..1482c1d5 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java @@ -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))); } } }