2 * Copyright © 2021 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui;
20 import android.app.Dialog;
21 import android.content.Context;
22 import android.os.Bundle;
23 import android.view.View;
24 import android.widget.RadioButton;
25 import android.widget.RadioGroup;
26 import android.widget.TextView;
28 import androidx.annotation.NonNull;
29 import androidx.annotation.Nullable;
30 import androidx.appcompat.app.AppCompatDialogFragment;
31 import androidx.lifecycle.ViewModelProvider;
32 import androidx.recyclerview.widget.GridLayoutManager;
33 import androidx.recyclerview.widget.LinearLayoutManager;
34 import androidx.recyclerview.widget.RecyclerView;
36 import com.google.android.material.switchmaterial.SwitchMaterial;
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.dao.CurrencyDAO;
40 import net.ktnx.mobileledger.db.DB;
41 import net.ktnx.mobileledger.db.Profile;
42 import net.ktnx.mobileledger.model.Currency;
43 import net.ktnx.mobileledger.model.Data;
45 import java.util.ArrayList;
46 import java.util.List;
49 * A fragment representing a list of Items.
51 * Activities containing this fragment MUST implement the {@link OnCurrencySelectedListener}
54 public class CurrencySelectorFragment extends AppCompatDialogFragment
55 implements OnCurrencySelectedListener, OnCurrencyLongClickListener {
57 public static final int DEFAULT_COLUMN_COUNT = 2;
58 public static final String ARG_COLUMN_COUNT = "column-count";
59 public static final String ARG_SHOW_PARAMS = "show-params";
60 public static final boolean DEFAULT_SHOW_PARAMS = true;
61 private int mColumnCount = DEFAULT_COLUMN_COUNT;
62 private CurrencySelectorModel model;
63 private boolean deferredShowPositionAndPadding;
64 private OnCurrencySelectedListener onCurrencySelectedListener;
67 * Mandatory empty constructor for the fragment manager to instantiate the
68 * fragment (e.g. upon screen orientation changes).
70 public CurrencySelectorFragment() {
72 @SuppressWarnings("unused")
73 public static CurrencySelectorFragment newInstance() {
74 return newInstance(DEFAULT_COLUMN_COUNT, DEFAULT_SHOW_PARAMS);
76 public static CurrencySelectorFragment newInstance(int columnCount, boolean showParams) {
77 CurrencySelectorFragment fragment = new CurrencySelectorFragment();
78 Bundle args = new Bundle();
79 args.putInt(ARG_COLUMN_COUNT, columnCount);
80 args.putBoolean(ARG_SHOW_PARAMS, showParams);
81 fragment.setArguments(args);
85 public void onCreate(Bundle savedInstanceState) {
86 super.onCreate(savedInstanceState);
88 if (getArguments() != null) {
89 mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT, DEFAULT_COLUMN_COUNT);
94 public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
95 Context context = requireContext();
96 Dialog csd = new Dialog(context);
97 csd.setContentView(R.layout.fragment_currency_selector_list);
98 csd.setTitle(R.string.choose_currency_label);
100 RecyclerView recyclerView = csd.findViewById(R.id.list);
102 if (mColumnCount <= 1) {
103 recyclerView.setLayoutManager(new LinearLayoutManager(context));
106 recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
108 model = new ViewModelProvider(this).get(CurrencySelectorModel.class);
109 if (onCurrencySelectedListener != null)
110 model.setOnCurrencySelectedListener(onCurrencySelectedListener);
111 Profile profile = Data.getProfile();
113 CurrencySelectorRecyclerViewAdapter adapter = new CurrencySelectorRecyclerViewAdapter();
117 .observe(this, list -> {
118 List<String> strings = new ArrayList<>();
119 for (net.ktnx.mobileledger.db.Currency c : list) {
120 strings.add(c.getName());
122 adapter.submitList(strings);
125 recyclerView.setAdapter(adapter);
126 adapter.setCurrencySelectedListener(this);
127 adapter.setCurrencyLongClickListener(this);
129 final TextView tvNewCurrName = csd.findViewById(R.id.new_currency_name);
130 final TextView tvNoCurrBtn = csd.findViewById(R.id.btn_no_currency);
131 final TextView tvAddCurrOkBtn = csd.findViewById(R.id.btn_add_currency);
132 final TextView tvAddCurrBtn = csd.findViewById(R.id.btn_add_new);
133 final SwitchMaterial gap = csd.findViewById(R.id.currency_gap);
134 final RadioGroup rgPosition = csd.findViewById(R.id.position_radio_group);
136 tvNewCurrName.setVisibility(View.GONE);
137 tvAddCurrOkBtn.setVisibility(View.GONE);
138 tvNoCurrBtn.setVisibility(View.VISIBLE);
139 tvAddCurrBtn.setVisibility(View.VISIBLE);
141 tvAddCurrBtn.setOnClickListener(v -> {
142 tvNewCurrName.setVisibility(View.VISIBLE);
143 tvAddCurrOkBtn.setVisibility(View.VISIBLE);
145 tvNoCurrBtn.setVisibility(View.GONE);
146 tvAddCurrBtn.setVisibility(View.GONE);
148 tvNewCurrName.setText(null);
149 tvNewCurrName.requestFocus();
150 net.ktnx.mobileledger.utils.Misc.showSoftKeyboard(this);
153 tvAddCurrOkBtn.setOnClickListener(v -> {
154 String currName = String.valueOf(tvNewCurrName.getText());
155 if (!currName.isEmpty()) {
158 .insert(new net.ktnx.mobileledger.db.Currency(0,
159 String.valueOf(tvNewCurrName.getText()),
160 (rgPosition.getCheckedRadioButtonId() == R.id.currency_position_left)
161 ? "before" : "after", gap.isChecked()));
164 tvNewCurrName.setVisibility(View.GONE);
165 tvAddCurrOkBtn.setVisibility(View.GONE);
167 tvNoCurrBtn.setVisibility(View.VISIBLE);
168 tvAddCurrBtn.setVisibility(View.VISIBLE);
171 tvNoCurrBtn.setOnClickListener(v -> {
172 adapter.notifyCurrencySelected(null);
176 RadioButton rbPositionLeft = csd.findViewById(R.id.currency_position_left);
177 RadioButton rbPositionRight = csd.findViewById(R.id.currency_position_right);
179 if (Data.currencySymbolPosition.getValue() == Currency.Position.before)
180 rbPositionLeft.toggle();
182 rbPositionRight.toggle();
184 rgPosition.setOnCheckedChangeListener((group, checkedId) -> {
185 if (checkedId == R.id.currency_position_left)
186 Data.currencySymbolPosition.setValue(Currency.Position.before);
188 Data.currencySymbolPosition.setValue(Currency.Position.after);
191 gap.setChecked(Data.currencyGap.getValue());
193 gap.setOnCheckedChangeListener((v, checked) -> Data.currencyGap.setValue(checked));
195 model.observePositionAndPaddingVisible(this, visible -> csd.findViewById(R.id.params_panel)
197 visible ? View.VISIBLE
200 final boolean showParams;
201 if (getArguments() == null)
202 showParams = DEFAULT_SHOW_PARAMS;
204 showParams = getArguments().getBoolean(ARG_SHOW_PARAMS, DEFAULT_SHOW_PARAMS);
207 model.showPositionAndPadding();
209 model.hidePositionAndPadding();
213 public void setOnCurrencySelectedListener(OnCurrencySelectedListener listener) {
214 onCurrencySelectedListener = listener;
217 model.setOnCurrencySelectedListener(listener);
219 public void resetOnCurrencySelectedListener() {
220 model.resetOnCurrencySelectedListener();
223 public void onCurrencySelected(String item) {
224 model.triggerOnCurrencySelectedListener(item);
230 public void onCurrencyLongClick(String item) {
231 CurrencyDAO dao = DB.get()
234 .observe(this, dao::deleteSync);
236 public void showPositionAndPadding() {
237 deferredShowPositionAndPadding = true;
239 public void hidePositionAndPadding() {
240 deferredShowPositionAndPadding = false;