]> git.ktnx.net Git - mobile-ledger.git/commitdiff
CurrencySelector: move selection listener to the model so that it survives reconficur...
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 20 Apr 2020 18:43:47 +0000 (21:43 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Mon, 20 Apr 2020 18:43:47 +0000 (21:43 +0300)
app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorModel.java

index d25db493f7b05b659bd6cb6c21f0989570737564..3bbfe1554219aec3b3dd96b40f5a96feb3207966 100644 (file)
@@ -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();
     }
index a2bc9ca4924526bfd20527cd8b8591b4680acb16..58950244b70ce29d9ee8876bef118b4ca80872b0 100644 (file)
@@ -29,6 +29,7 @@ import java.util.List;
 public class CurrencySelectorModel extends ViewModel {
     public final MutableLiveData<List<Currency>> currencies;
     private final MutableLiveData<Boolean> 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<Boolean> 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);
+    }
 }