2 * Copyright © 2019 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.Switch;
27 import android.widget.TextView;
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 import androidx.appcompat.app.AppCompatDialogFragment;
32 import androidx.lifecycle.ViewModelProvider;
33 import androidx.recyclerview.widget.GridLayoutManager;
34 import androidx.recyclerview.widget.LinearLayoutManager;
35 import androidx.recyclerview.widget.RecyclerView;
37 import net.ktnx.mobileledger.App;
38 import net.ktnx.mobileledger.R;
39 import net.ktnx.mobileledger.model.Currency;
40 import net.ktnx.mobileledger.model.Data;
41 import net.ktnx.mobileledger.model.MobileLedgerProfile;
43 import java.util.ArrayList;
44 import java.util.List;
45 import java.util.Objects;
46 import java.util.concurrent.CopyOnWriteArrayList;
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 private static final String ARG_COLUMN_COUNT = "column-count";
59 private int mColumnCount = DEFAULT_COLUMN_COUNT;
60 private OnCurrencySelectedListener mListener;
61 private CurrencySelectorModel model;
64 * Mandatory empty constructor for the fragment manager to instantiate the
65 * fragment (e.g. upon screen orientation changes).
67 public CurrencySelectorFragment() {
69 @SuppressWarnings("unused")
70 public static CurrencySelectorFragment newInstance() {
71 return newInstance(DEFAULT_COLUMN_COUNT);
73 public static CurrencySelectorFragment newInstance(int columnCount) {
74 CurrencySelectorFragment fragment = new CurrencySelectorFragment();
75 Bundle args = new Bundle();
76 args.putInt(ARG_COLUMN_COUNT, columnCount);
77 fragment.setArguments(args);
81 public void onCreate(Bundle savedInstanceState) {
82 super.onCreate(savedInstanceState);
84 if (getArguments() != null) {
85 mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT, DEFAULT_COLUMN_COUNT);
90 public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
91 Context context = Objects.requireNonNull(getContext());
92 Dialog csd = new Dialog(context);
93 csd.setContentView(R.layout.fragment_currency_selector_list);
94 csd.setTitle(R.string.choose_currency_label);
96 RecyclerView recyclerView = csd.findViewById(R.id.list);
98 if (mColumnCount <= 1) {
99 recyclerView.setLayoutManager(new LinearLayoutManager(context));
102 recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
104 model = new ViewModelProvider(this).get(CurrencySelectorModel.class);
105 MobileLedgerProfile profile = Objects.requireNonNull(Data.profile.getValue());
107 model.currencies.setValue(new CopyOnWriteArrayList<>(profile.getCurrencies()));
108 CurrencySelectorRecyclerViewAdapter adapter = new CurrencySelectorRecyclerViewAdapter();
109 model.currencies.observe(this, list -> adapter.submitList(list));
111 recyclerView.setAdapter(adapter);
112 adapter.setCurrencySelectedListener(this);
113 adapter.setCurrencyLongClickListener(this);
115 final TextView tvNewCurrName = csd.findViewById(R.id.new_currency_name);
116 final TextView tvNoCurrBtn = csd.findViewById(R.id.btn_no_currency);
117 final TextView tvAddCurrOkBtn = csd.findViewById(R.id.btn_add_currency);
118 final TextView tvAddCurrBtn = csd.findViewById(R.id.btn_add_new);
120 tvNewCurrName.setVisibility(View.GONE);
121 tvAddCurrOkBtn.setVisibility(View.GONE);
122 tvNoCurrBtn.setVisibility(View.VISIBLE);
123 tvAddCurrBtn.setVisibility(View.VISIBLE);
125 tvAddCurrBtn.setOnClickListener(v -> {
126 tvNewCurrName.setVisibility(View.VISIBLE);
127 tvAddCurrOkBtn.setVisibility(View.VISIBLE);
129 tvNoCurrBtn.setVisibility(View.GONE);
130 tvAddCurrBtn.setVisibility(View.GONE);
132 tvNewCurrName.setText(null);
133 tvNewCurrName.requestFocus();
134 net.ktnx.mobileledger.utils.Misc.showSoftKeyboard(this);
137 tvAddCurrOkBtn.setOnClickListener(v -> {
140 String currName = String.valueOf(tvNewCurrName.getText());
141 if (!currName.isEmpty()) {
142 List<Currency> list = new ArrayList<>( model.currencies.getValue());
143 // FIXME hardcoded position and gap setting
144 list.add(new Currency(profile, String.valueOf(tvNewCurrName.getText()),
145 Currency.Position.after, false));
146 model.currencies.setValue(list);
149 tvNewCurrName.setVisibility(View.GONE);
150 tvAddCurrOkBtn.setVisibility(View.GONE);
152 tvNoCurrBtn.setVisibility(View.VISIBLE);
153 tvAddCurrBtn.setVisibility(View.VISIBLE);
156 tvNoCurrBtn.setOnClickListener(v -> {
157 adapter.notifyCurrencySelected(null);
161 RadioButton rbPositionLeft = csd.findViewById(R.id.currency_position_left);
162 RadioButton rbPositionRight = csd.findViewById(R.id.currency_position_right);
164 if (Data.currencySymbolPosition.getValue() == Currency.Position.before)
165 rbPositionLeft.toggle();
167 rbPositionRight.toggle();
169 RadioGroup rgPosition = csd.findViewById(R.id.position_radio_group);
170 rgPosition.setOnCheckedChangeListener((group, checkedId) -> {
171 if (checkedId == R.id.currency_position_left)
172 Data.currencySymbolPosition.setValue(Currency.Position.before);
174 Data.currencySymbolPosition.setValue(Currency.Position.after);
177 Switch gap = csd.findViewById(R.id.currency_gap);
179 gap.setChecked(Data.currencyGap.getValue());
181 gap.setOnCheckedChangeListener((v, checked) -> {
182 Data.currencyGap.setValue(checked);
187 public void setOnCurrencySelectedListener(OnCurrencySelectedListener listener) {
188 mListener = listener;
190 public void resetOnCurrencySelectedListener() {
194 public void onCurrencySelected(Currency item) {
195 if (mListener != null)
196 mListener.onCurrencySelected(item);
202 public void onCurrencyLongClick(Currency item) {
203 ArrayList<Currency> list = new ArrayList<>(model.currencies.getValue());
204 App.getDatabase().execSQL("delete from currencies where id=?", new Object[]{item.getId()});
206 model.currencies.setValue(list);