]> git.ktnx.net Git - mobile-ledger.git/commitdiff
make use of getBindingAdapter and save a field
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 18 Apr 2021 10:51:54 +0000 (13:51 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 18 Apr 2021 10:52:14 +0000 (10:52 +0000)
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionAccountRowItemHolder.java
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionHeaderItemHolder.java
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemViewHolder.java

index 5e930569e1afac281593d7a7d0d5cd51151b87e2..4b0c1134c911f450099ed425f412e19679ad7751 100644 (file)
@@ -56,7 +56,7 @@ class NewTransactionAccountRowItemHolder extends NewTransactionItemViewHolder {
     //TODO multiple amounts with different currencies per posting?
     NewTransactionAccountRowItemHolder(@NonNull NewTransactionAccountRowBinding b,
                                        NewTransactionItemsAdapter adapter) {
-        super(b.getRoot(), adapter);
+        super(b.getRoot());
         this.b = b;
         new TextViewClearHelper().attachToTextView(b.comment);
 
@@ -225,7 +225,11 @@ class NewTransactionAccountRowItemHolder extends NewTransactionItemViewHolder {
                  focusInfo.position != getBindingAdapterPosition()))
                 return;
 
-            NewTransactionModel.TransactionAccount acc = getItem().toTransactionAccount();
+            final NewTransactionModel.Item item = getItem();
+            if (item == null)
+                return;
+
+            NewTransactionModel.TransactionAccount acc = item.toTransactionAccount();
             switch (focusInfo.element) {
                 case Amount:
                     b.accountRowAccAmounts.requestFocus();
@@ -404,7 +408,9 @@ class NewTransactionAccountRowItemHolder extends NewTransactionItemViewHolder {
             return false;
         }
 
-        NewTransactionModel.Item item = getItem();
+        final NewTransactionModel.Item item = getItem();
+        if (item == null)
+            return false;
 
         syncingData = true;
 
@@ -524,8 +530,11 @@ class NewTransactionAccountRowItemHolder extends NewTransactionItemViewHolder {
 
                 setEditable(true);
 
-                applyFocus(mAdapter.model.getFocusInfo()
-                                         .getValue());
+                NewTransactionItemsAdapter adapter =
+                        (NewTransactionItemsAdapter) getBindingAdapter();
+                if (adapter != null)
+                    applyFocus(adapter.model.getFocusInfo()
+                                            .getValue());
             }
             finally {
                 syncingData = false;
index 37c55ce8aa1e4b19ec98f4b287d003683d8773c8..95a8526a3494ecaee52a7d2c68588b4c55a63b05 100644 (file)
@@ -54,7 +54,7 @@ class NewTransactionHeaderItemHolder extends NewTransactionItemViewHolder
     //TODO multiple amounts with different currencies per posting?
     NewTransactionHeaderItemHolder(@NonNull NewTransactionHeaderRowBinding b,
                                    NewTransactionItemsAdapter adapter) {
-        super(b.getRoot(), adapter);
+        super(b.getRoot());
         this.b = b;
 
         b.newTransactionDescription.setNextFocusForwardId(View.NO_ID);
@@ -157,7 +157,11 @@ class NewTransactionHeaderItemHolder extends NewTransactionItemViewHolder
                  focusInfo.position != getBindingAdapterPosition()))
                 return;
 
-            NewTransactionModel.Item head = getItem().toTransactionHead();
+            final NewTransactionModel.Item item = getItem();
+            if (item == null)
+                return;
+
+            NewTransactionModel.Item head = item.toTransactionHead();
             // bad idea - double pop-up, and not really necessary.
             // the user can tap the input to get the calendar
             //if (!tvDate.hasFocus()) tvDate.requestFocus();
@@ -261,7 +265,11 @@ class NewTransactionHeaderItemHolder extends NewTransactionItemViewHolder
 
         syncingData = true;
         try {
-            NewTransactionModel.TransactionHead head = getItem().toTransactionHead();
+            final NewTransactionModel.Item item = getItem();
+            if (item == null)
+                return false;
+
+            NewTransactionModel.TransactionHead head = item.toTransactionHead();
 
             head.setDate(String.valueOf(b.newTransactionDate.getText()));
 
@@ -320,8 +328,11 @@ class NewTransactionHeaderItemHolder extends NewTransactionItemViewHolder
 
                 setEditable(true);
 
-                applyFocus(mAdapter.model.getFocusInfo()
-                                         .getValue());
+                NewTransactionItemsAdapter adapter =
+                        (NewTransactionItemsAdapter) getBindingAdapter();
+                if (adapter != null)
+                    applyFocus(adapter.model.getFocusInfo()
+                                            .getValue());
             }
             finally {
                 syncingData = false;
@@ -340,7 +351,11 @@ class NewTransactionHeaderItemHolder extends NewTransactionItemViewHolder
     }
     @Override
     public void onDatePicked(int year, int month, int day) {
-        final NewTransactionModel.TransactionHead head = getItem().toTransactionHead();
+        final NewTransactionModel.Item item = getItem();
+        if (item == null)
+            return;
+
+        final NewTransactionModel.TransactionHead head = item.toTransactionHead();
         head.setDate(new SimpleDate(year, month + 1, day));
         b.newTransactionDate.setText(head.getFormattedDate());
 
index 56daf91f80adaf3a04532151b0b40b066687994f..25e0bb6d2d0c36246aee3b9506300a5940f8579f 100644 (file)
@@ -20,25 +20,24 @@ package net.ktnx.mobileledger.ui.new_transaction;
 import android.view.View;
 
 import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
 import androidx.recyclerview.widget.RecyclerView;
 
 import net.ktnx.mobileledger.db.Profile;
 import net.ktnx.mobileledger.model.Data;
 
 abstract class NewTransactionItemViewHolder extends RecyclerView.ViewHolder {
-    final NewTransactionItemsAdapter mAdapter;
     final Profile mProfile;
-    public NewTransactionItemViewHolder(@NonNull View itemView,
-                                        NewTransactionItemsAdapter adapter) {
+    public NewTransactionItemViewHolder(@NonNull View itemView) {
         super(itemView);
-        mAdapter = adapter;
         mProfile = Data.getProfile();
     }
+    @Nullable
     NewTransactionModel.Item getItem() {
-        return mAdapter.getItem(getBindingAdapterPosition());
-//        return Objects.requireNonNull(mAdapter.model.getItems()
-//                                                    .getValue())
-//                      .get(getBindingAdapterPosition());
+        NewTransactionItemsAdapter adapter = (NewTransactionItemsAdapter) getBindingAdapter();
+        if (adapter == null)
+            return null;
+        return adapter.getItem(getBindingAdapterPosition());
     }
     abstract public void bind(NewTransactionModel.Item item);
 }