Data.transactions -> observable list
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 29 Mar 2019 17:32:32 +0000 (19:32 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 29 Mar 2019 17:32:32 +0000 (19:32 +0200)
app/src/main/java/net/ktnx/mobileledger/async/UpdateTransactionsTask.java
app/src/main/java/net/ktnx/mobileledger/model/Data.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/transaction_list/TransactionListViewModel.java

index fb1bd72..f60ba7e 100644 (file)
@@ -87,7 +87,7 @@ public class UpdateTransactionsTask extends AsyncTask<String, Void, String> {
                     lastDateString = dateString;
                     odd = !odd;
                 }
-                Data.transactions.set(newList);
+                Data.transactions.setList(newList);
                 Log.d("UTT", "transaction list value updated");
             }
 
index d92ddef..1fc4aa0 100644 (file)
@@ -28,7 +28,7 @@ import java.util.Date;
 import java.util.List;
 
 public final class Data {
-    public static TransactionList transactions = new TransactionList();
+    public static ObservableList<TransactionListItem> transactions = new ObservableList<>(new ArrayList<>());
     public static ObservableList<LedgerAccount> accounts =
             new ObservableList<>(new ArrayList<>());
     public static ObservableAtomicInteger backgroundTaskCount = new ObservableAtomicInteger(0);
index 1e9d2d4..bad849e 100644 (file)
@@ -32,6 +32,7 @@ import android.widget.LinearLayout;
 import android.widget.TextView;
 
 import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerTransaction;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.TransactionListItem;
@@ -126,7 +127,7 @@ public class TransactionListAdapter extends RecyclerView.Adapter<TransactionRowH
 
     @Override
     public int getItemCount() {
-        return TransactionListViewModel.getTransactionCount() + 1;
+        return Data.transactions.size() + 1;
     }
     public void setBoldAccountName(String boldAccountName) {
         this.boldAccountName = boldAccountName;
index 513bf9f..7e0214c 100644 (file)
@@ -40,11 +40,12 @@ public class TransactionListViewModel extends ViewModel {
         task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, filter);
     }
     public static TransactionListItem getTransactionListItem(int position) {
-        List<TransactionListItem> transactions = Data.transactions.get();
-        if (transactions == null) return null;
-        if (position >= transactions.size() + 1) return null;
-        if (position == transactions.size()) return new TransactionListItem();
-        return transactions.get(position);
+        try(LockHolder lh = Data.transactions.lockForReading()) {
+            if (Data.transactions == null) return null;
+            if (position >= Data.transactions.size() + 1) return null;
+            if (position == Data.transactions.size()) return new TransactionListItem();
+            return Data.transactions.get(position);
+        }
     }
     public static int getTransactionCount() {
         List<TransactionListItem> transactions = Data.transactions.get();