]> git.ktnx.net Git - mobile-ledger.git/commitdiff
let the model hold the (single) transaction list
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 22 Dec 2018 07:29:18 +0000 (07:29 +0000)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sat, 22 Dec 2018 07:29:18 +0000 (07:29 +0000)
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

app/src/main/java/net/ktnx/mobileledger/TransactionListActivity.java
app/src/main/java/net/ktnx/mobileledger/TransactionListAdapter.java

index 2ccb286c1e78a50285c87fc139f16f027292e4fe..1a74eaad5031da20664c7aa72fb0a7b980a44532 100644 (file)
@@ -33,7 +33,6 @@ import android.widget.ProgressBar;
 import android.widget.TextView;
 
 import net.ktnx.mobileledger.async.RetrieveTransactionsTask;
 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;
 
 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.time.ZoneId;
 import java.time.format.DateTimeFormatter;
 import java.util.Date;
-import java.util.List;
 
 public class TransactionListActivity extends AppCompatActivity {
     public TransactionListViewModel model;
 
 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);
         tvLastUpdate = findViewById(R.id.transactions_last_update);
         updateLastUpdateText();
         model = ViewModelProviders.of(this).get(TransactionListViewModel.class);
-        List<LedgerTransaction> transactions = model.getTransactions(this);
-        modelAdapter = new TransactionListAdapter(transactions);
+        modelAdapter = new TransactionListAdapter(model);
 
         RecyclerView root = findViewById(R.id.transaction_root);
         root.setAdapter(modelAdapter);
 
         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);
 
 
         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();
     }
     private void setupActionBar() {
         ActionBar actionBar = getSupportActionBar();
@@ -142,6 +144,7 @@ public class TransactionListActivity extends AppCompatActivity {
         swiper.setRefreshing(false);
         updateLastUpdateText();
         if (success) {
         swiper.setRefreshing(false);
         updateLastUpdateText();
         if (success) {
+            Log.d("transactions", "calling notifyDataSetChanged()");
             modelAdapter.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));
         {
             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) {
             else {
                 Date date = new Date(last_update);
                 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
index de091f3a4b8112d3d00a48d5b6a66525920e14f2..7fa85d3a6c98b277350126403798588e985833bd 100644 (file)
@@ -33,29 +33,26 @@ import android.widget.TextView;
 
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 
 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 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<TransactionListAdapter.TransactionRowHolder> {
 import static net.ktnx.mobileledger.utils.DimensionUtils.dp2px;
 
 class TransactionListAdapter
         extends RecyclerView.Adapter<TransactionListAdapter.TransactionRowHolder> {
-    private List<LedgerTransaction> transactions;
-
-    TransactionListAdapter(List<LedgerTransaction> transactions) {
-        this.transactions = transactions;
+    TransactionListViewModel model;
+    public TransactionListAdapter(TransactionListViewModel model) {
+        this.model = model;
     }
     }
-
     public void onBindViewHolder(@NonNull TransactionRowHolder holder, int position) {
     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
         // 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)) {
         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);
             }
             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
     }
 
     @Override
-    public int getItemCount() {
-        return transactions.size();
+    public int getItemCount()
+    {
+        return model.getTransactionCount();
     }
     class TransactionRowHolder extends RecyclerView.ViewHolder {
         TextView tvDescription, tvDate;
     }
     class TransactionRowHolder extends RecyclerView.ViewHolder {
         TextView tvDescription, tvDate;