From: Damyan Ivanov Date: Sat, 22 Dec 2018 07:29:18 +0000 (+0000) Subject: let the model hold the (single) transaction list X-Git-Tag: v0.3~171 X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=commitdiff_plain;h=023539ae4b017c80b2b28978d701e05354d265df let the model hold the (single) transaction list all access to the list goes via the model transaction list is loaded upon activity launch if it was never downloaded before there were two copies of the list that were out of sync --- diff --git a/app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java b/app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java index 2ccb286c..1a74eaad 100644 --- a/app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java +++ b/app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java @@ -33,7 +33,6 @@ import android.widget.ProgressBar; import android.widget.TextView; import net.ktnx.mobileledger.async.RetrieveTransactionsTask; -import net.ktnx.mobileledger.model.LedgerTransaction; import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel; import net.ktnx.mobileledger.utils.MLDB; @@ -41,7 +40,6 @@ import java.lang.ref.WeakReference; import java.time.ZoneId; import java.time.format.DateTimeFormatter; import java.util.Date; -import java.util.List; public class TransactionListActivity extends AppCompatActivity { public TransactionListViewModel model; @@ -70,8 +68,7 @@ public class TransactionListActivity extends AppCompatActivity { tvLastUpdate = findViewById(R.id.transactions_last_update); updateLastUpdateText(); model = ViewModelProviders.of(this).get(TransactionListViewModel.class); - List transactions = model.getTransactions(this); - modelAdapter = new TransactionListAdapter(transactions); + modelAdapter = new TransactionListAdapter(model); RecyclerView root = findViewById(R.id.transaction_root); root.setAdapter(modelAdapter); @@ -88,7 +85,12 @@ public class TransactionListActivity extends AppCompatActivity { swiper.setColorSchemeResources(R.color.colorPrimary, R.color.colorAccent); -// update_transactions(); + updateLastUpdateText(); + long last_update = MLDB.get_option_value(this, MLDB.OPT_TRANSACTION_LIST_STAMP, 0L); + Log.d("transactions", String.format("Last update = %d", last_update)); + if (last_update == 0) { + update_transactions(); + } } private void setupActionBar() { ActionBar actionBar = getSupportActionBar(); @@ -142,6 +144,7 @@ public class TransactionListActivity extends AppCompatActivity { swiper.setRefreshing(false); updateLastUpdateText(); if (success) { + Log.d("transactions", "calling notifyDataSetChanged()"); modelAdapter.notifyDataSetChanged(); } } @@ -149,7 +152,9 @@ public class TransactionListActivity extends AppCompatActivity { { long last_update = MLDB.get_option_value(this, MLDB.OPT_TRANSACTION_LIST_STAMP, 0L); Log.d("transactions", String.format("Last update = %d", last_update)); - if (last_update == 0) tvLastUpdate.setText(getString(R.string.transaction_last_update_never)); + if (last_update == 0) { + tvLastUpdate.setText(getString(R.string.transaction_last_update_never)); + } else { Date date = new Date(last_update); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { diff --git a/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java b/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java index de091f3a..7fa85d3a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java @@ -33,29 +33,26 @@ import android.widget.TextView; import net.ktnx.mobileledger.model.LedgerTransaction; import net.ktnx.mobileledger.model.LedgerTransactionAccount; +import net.ktnx.mobileledger.ui.transaction_list.TransactionListViewModel; import net.ktnx.mobileledger.utils.Globals; import net.ktnx.mobileledger.utils.MLDB; -import java.util.List; - import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px; class TransactionListAdapter extends RecyclerView.Adapter { - private List transactions; - - TransactionListAdapter(List transactions) { - this.transactions = transactions; + TransactionListViewModel model; + public TransactionListAdapter(TransactionListViewModel model) { + this.model = model; } - public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) { + LedgerTransaction tr = model.getTransaction(position); // in a race when transaction list is reduced, but the model hasn't been notified yet // the view will disappear when the notifications reaches the model, so by simply omitting // the out-of-range get() call nothing bad happens - just a to-be-deleted view remains // a bit longer - if (position >= transactions.size()) return; + if (tr == null) return; - LedgerTransaction tr = transactions.get(position); Context ctx = holder.row.getContext(); try (SQLiteDatabase db = MLDB.getReadableDatabase(ctx)) { @@ -109,6 +106,8 @@ class TransactionListAdapter else { holder.row.setBackgroundColor(Globals.table_row_odd_bg); } + + Log.d("transactions", String.format("Filled position %d", position)); } } @@ -122,8 +121,9 @@ class TransactionListAdapter } @Override - public int getItemCount() { - return transactions.size(); + public int getItemCount() + { + return model.getTransactionCount(); } class TransactionRowHolder extends RecyclerView.ViewHolder { TextView tvDescription, tvDate;