]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionHeaderItemHolder.java
make use of getBindingAdapter and save a field
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionHeaderItemHolder.java
index 81afa05dc69fbe938c22ee4229f0fe8e592d43bd..95a8526a3494ecaee52a7d2c68588b4c55a63b05 100644 (file)
@@ -29,11 +29,13 @@ import android.widget.TextView;
 
 import androidx.annotation.ColorInt;
 import androidx.annotation.NonNull;
+import androidx.recyclerview.widget.RecyclerView;
 
 import net.ktnx.mobileledger.R;
 import net.ktnx.mobileledger.databinding.NewTransactionHeaderRowBinding;
 import net.ktnx.mobileledger.db.TransactionDescriptionAutocompleteAdapter;
 import net.ktnx.mobileledger.model.Data;
+import net.ktnx.mobileledger.model.FutureDates;
 import net.ktnx.mobileledger.ui.DatePickerFragment;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
@@ -52,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);
@@ -70,7 +72,7 @@ class NewTransactionHeaderItemHolder extends NewTransactionItemViewHolder
                 boolean wasSyncing = syncingData;
                 syncingData = true;
                 try {
-                    final int pos = getAdapterPosition();
+                    final int pos = getBindingAdapterPosition();
                     if (id == R.id.transaction_comment) {
                         adapter.noteFocusIsOnTransactionComment(pos);
                     }
@@ -152,10 +154,14 @@ class NewTransactionHeaderItemHolder extends NewTransactionItemViewHolder
         ignoreFocusChanges = true;
         try {
             if (((focusInfo == null) || (focusInfo.element == null) ||
-                 focusInfo.position != getAdapterPosition()))
+                 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();
@@ -248,7 +254,7 @@ class NewTransactionHeaderItemHolder extends NewTransactionItemViewHolder
             return false;
         }
 
-        if (getAdapterPosition() < 0) {
+        if (getBindingAdapterPosition() == RecyclerView.NO_POSITION) {
             // probably the row was swiped out
             Logger.debug("new-trans", "Ignoring request to suncData(): adapter position negative");
             return false;
@@ -259,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()));
 
@@ -282,7 +292,7 @@ class NewTransactionHeaderItemHolder extends NewTransactionItemViewHolder
     }
     private void pickTransactionDate() {
         DatePickerFragment picker = new DatePickerFragment();
-        picker.setFutureDates(mProfile.getFutureDates());
+        picker.setFutureDates(FutureDates.valueOf(mProfile.getFutureDates()));
         picker.setOnDatePickedListener(this);
         picker.setCurrentDateFromText(b.newTransactionDate.getText());
         picker.show(((NewTransactionActivity) b.getRoot()
@@ -318,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;
@@ -338,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());