X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2FCurrencySelectorFragment.java;h=76a640ce287998432b17ac64516ebb263a5429aa;hb=48c4a9232442016aa8d80c2ac1bc8e0214bd6de1;hp=76a7d348dd742aec3024ec92afe64f5273fb01fb;hpb=5545ddea3574103c2a7eea552fff0d43a0587fac;p=mobile-ledger.git 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 76a7d348..76a640ce 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java @@ -55,10 +55,13 @@ public class CurrencySelectorFragment extends AppCompatDialogFragment implements OnCurrencySelectedListener, OnCurrencyLongClickListener { public static final int DEFAULT_COLUMN_COUNT = 2; - private static final String ARG_COLUMN_COUNT = "column-count"; + public static final String ARG_COLUMN_COUNT = "column-count"; + 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 @@ -68,12 +71,13 @@ public class CurrencySelectorFragment extends AppCompatDialogFragment } @SuppressWarnings("unused") public static CurrencySelectorFragment newInstance() { - return newInstance(DEFAULT_COLUMN_COUNT); + return newInstance(DEFAULT_COLUMN_COUNT, DEFAULT_SHOW_PARAMS); } - public static CurrencySelectorFragment newInstance(int columnCount) { + public static CurrencySelectorFragment newInstance(int columnCount, boolean showParams) { CurrencySelectorFragment fragment = new CurrencySelectorFragment(); Bundle args = new Bundle(); args.putInt(ARG_COLUMN_COUNT, columnCount); + args.putBoolean(ARG_SHOW_PARAMS, showParams); fragment.setArguments(args); return fragment; } @@ -88,7 +92,7 @@ public class CurrencySelectorFragment extends AppCompatDialogFragment @NonNull @Override public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { - Context context = Objects.requireNonNull(getContext()); + Context context = requireContext(); Dialog csd = new Dialog(context); csd.setContentView(R.layout.fragment_currency_selector_list); csd.setTitle(R.string.choose_currency_label); @@ -102,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())); @@ -139,7 +145,7 @@ public class CurrencySelectorFragment extends AppCompatDialogFragment String currName = String.valueOf(tvNewCurrName.getText()); if (!currName.isEmpty()) { - List list = new ArrayList<>( model.currencies.getValue()); + List list = new ArrayList<>(model.currencies.getValue()); // FIXME hardcoded position and gap setting list.add(new Currency(profile, String.valueOf(tvNewCurrName.getText()), Currency.Position.after, false)); @@ -182,18 +188,31 @@ public class CurrencySelectorFragment extends AppCompatDialogFragment Data.currencyGap.setValue(checked); }); + model.observePositionAndPaddingVisible(this, visible -> { + csd.findViewById(R.id.params_panel) + .setVisibility(visible ? View.VISIBLE : View.GONE); + }); + + if ((savedInstanceState != null) ? savedInstanceState.getBoolean(ARG_SHOW_PARAMS, + DEFAULT_SHOW_PARAMS) : DEFAULT_SHOW_PARAMS) + model.showPositionAndPadding(); + else + model.hidePositionAndPadding(); + 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(); } @@ -201,8 +220,15 @@ public class CurrencySelectorFragment extends AppCompatDialogFragment @Override public void onCurrencyLongClick(Currency item) { ArrayList list = new ArrayList<>(model.currencies.getValue()); - App.getDatabase().execSQL("delete from currencies where id=?", new Object[]{item.getId()}); + App.getDatabase() + .execSQL("delete from currencies where id=?", new Object[]{item.getId()}); list.remove(item); model.currencies.setValue(list); } + public void showPositionAndPadding() { + deferredShowPositionAndPadding = true; + } + public void hidePositionAndPadding() { + deferredShowPositionAndPadding = false; + } }