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
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()));
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();
}
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<>();
}
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);
+ }
}