From a9ac1a87e456ef147fb6a97b3b5dcb1e57494ee8 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Sun, 18 Apr 2021 13:51:54 +0300 Subject: [PATCH] make use of getBindingAdapter and save a field --- .../NewTransactionAccountRowItemHolder.java | 19 +++++++++---- .../NewTransactionHeaderItemHolder.java | 27 ++++++++++++++----- .../NewTransactionItemViewHolder.java | 15 +++++------ 3 files changed, 42 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionAccountRowItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionAccountRowItemHolder.java index 5e930569..4b0c1134 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionAccountRowItemHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionAccountRowItemHolder.java @@ -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; diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionHeaderItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionHeaderItemHolder.java index 37c55ce8..95a8526a 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionHeaderItemHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionHeaderItemHolder.java @@ -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()); diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemViewHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemViewHolder.java index 56daf91f..25e0bb6d 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemViewHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemViewHolder.java @@ -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); } -- 2.39.5