From 04774f5e1c5953df1630962ae4f09d548e8d233f Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Mon, 20 Apr 2020 21:43:47 +0300 Subject: [PATCH] CurrencySelector: move selection listener to the model so that it survives reconficuration --- .../mobileledger/ui/CurrencySelectorFragment.java | 14 +++++++++----- .../mobileledger/ui/CurrencySelectorModel.java | 11 +++++++++++ 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java b/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java index d25db493..3bbfe155 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java @@ -59,9 +59,9 @@ public class CurrencySelectorFragment extends AppCompatDialogFragment public static final String ARG_SHOW_PARAMS = "show-params"; public static final boolean DEFAULT_SHOW_PARAMS = true; private int mColumnCount = DEFAULT_COLUMN_COUNT; - private OnCurrencySelectedListener mListener; private CurrencySelectorModel model; private boolean deferredShowPositionAndPadding; + private OnCurrencySelectedListener onCurrencySelectedListener; /** * Mandatory empty constructor for the fragment manager to instantiate the @@ -106,6 +106,8 @@ public class CurrencySelectorFragment extends AppCompatDialogFragment recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount)); } model = new ViewModelProvider(this).get(CurrencySelectorModel.class); + if (onCurrencySelectedListener != null) + model.setOnCurrencySelectedListener(onCurrencySelectedListener); MobileLedgerProfile profile = Objects.requireNonNull(Data.profile.getValue()); model.currencies.setValue(new CopyOnWriteArrayList<>(profile.getCurrencies())); @@ -200,15 +202,17 @@ public class CurrencySelectorFragment extends AppCompatDialogFragment return csd; } public void setOnCurrencySelectedListener(OnCurrencySelectedListener listener) { - mListener = listener; + onCurrencySelectedListener = listener; + + if (model != null) + model.setOnCurrencySelectedListener(listener); } public void resetOnCurrencySelectedListener() { - mListener = null; + model.resetOnCurrencySelectedListener(); } @Override public void onCurrencySelected(Currency item) { - if (mListener != null) - mListener.onCurrencySelected(item); + model.triggerOnCurrencySelectedListener(item); dismiss(); } diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorModel.java index a2bc9ca4..58950244 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorModel.java @@ -29,6 +29,7 @@ import java.util.List; public class CurrencySelectorModel extends ViewModel { public final MutableLiveData> currencies; private final MutableLiveData positionAndPaddingVisible = new MutableLiveData<>(true); + private OnCurrencySelectedListener selectionListener; public CurrencySelectorModel() { this.currencies = new MutableLiveData<>(); } @@ -41,4 +42,14 @@ public class CurrencySelectorModel extends ViewModel { public void observePositionAndPaddingVisible(LifecycleOwner activity, Observer observer) { positionAndPaddingVisible.observe(activity, observer); } + void setOnCurrencySelectedListener(OnCurrencySelectedListener listener) { + selectionListener = listener; + } + void resetOnCurrencySelectedListener() { + selectionListener = null; + } + void triggerOnCurrencySelectedListener(Currency c) { + if (selectionListener != null) + selectionListener.onCurrencySelected(c); + } } -- 2.39.5