]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java
debug++
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / CurrencySelectorFragment.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui;
19
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;
27
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;
35
36 import com.google.android.material.switchmaterial.SwitchMaterial;
37
38 import net.ktnx.mobileledger.App;
39 import net.ktnx.mobileledger.R;
40 import net.ktnx.mobileledger.model.Currency;
41 import net.ktnx.mobileledger.model.Data;
42 import net.ktnx.mobileledger.model.MobileLedgerProfile;
43
44 import java.util.ArrayList;
45 import java.util.List;
46 import java.util.Objects;
47 import java.util.concurrent.CopyOnWriteArrayList;
48
49 /**
50  * A fragment representing a list of Items.
51  * <p/>
52  * Activities containing this fragment MUST implement the {@link OnCurrencySelectedListener}
53  * interface.
54  */
55 public class CurrencySelectorFragment extends AppCompatDialogFragment
56         implements OnCurrencySelectedListener, OnCurrencyLongClickListener {
57
58     public static final int DEFAULT_COLUMN_COUNT = 2;
59     public static final String ARG_COLUMN_COUNT = "column-count";
60     public static final String ARG_SHOW_PARAMS = "show-params";
61     public static final boolean DEFAULT_SHOW_PARAMS = true;
62     private int mColumnCount = DEFAULT_COLUMN_COUNT;
63     private CurrencySelectorModel model;
64     private boolean deferredShowPositionAndPadding;
65     private OnCurrencySelectedListener onCurrencySelectedListener;
66
67     /**
68      * Mandatory empty constructor for the fragment manager to instantiate the
69      * fragment (e.g. upon screen orientation changes).
70      */
71     public CurrencySelectorFragment() {
72     }
73     @SuppressWarnings("unused")
74     public static CurrencySelectorFragment newInstance() {
75         return newInstance(DEFAULT_COLUMN_COUNT, DEFAULT_SHOW_PARAMS);
76     }
77     public static CurrencySelectorFragment newInstance(int columnCount, boolean showParams) {
78         CurrencySelectorFragment fragment = new CurrencySelectorFragment();
79         Bundle args = new Bundle();
80         args.putInt(ARG_COLUMN_COUNT, columnCount);
81         args.putBoolean(ARG_SHOW_PARAMS, showParams);
82         fragment.setArguments(args);
83         return fragment;
84     }
85     @Override
86     public void onCreate(Bundle savedInstanceState) {
87         super.onCreate(savedInstanceState);
88
89         if (getArguments() != null) {
90             mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT, DEFAULT_COLUMN_COUNT);
91         }
92     }
93     @NonNull
94     @Override
95     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
96         Context context = requireContext();
97         Dialog csd = new Dialog(context);
98         csd.setContentView(R.layout.fragment_currency_selector_list);
99         csd.setTitle(R.string.choose_currency_label);
100
101         RecyclerView recyclerView = csd.findViewById(R.id.list);
102
103         if (mColumnCount <= 1) {
104             recyclerView.setLayoutManager(new LinearLayoutManager(context));
105         }
106         else {
107             recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
108         }
109         model = new ViewModelProvider(this).get(CurrencySelectorModel.class);
110         if (onCurrencySelectedListener != null)
111             model.setOnCurrencySelectedListener(onCurrencySelectedListener);
112         MobileLedgerProfile profile = Objects.requireNonNull(Data.getProfile());
113
114         model.currencies.setValue(new CopyOnWriteArrayList<>(profile.getCurrencies()));
115         CurrencySelectorRecyclerViewAdapter adapter = new CurrencySelectorRecyclerViewAdapter();
116         model.currencies.observe(this, adapter::submitList);
117
118         recyclerView.setAdapter(adapter);
119         adapter.setCurrencySelectedListener(this);
120         adapter.setCurrencyLongClickListener(this);
121
122         final TextView tvNewCurrName = csd.findViewById(R.id.new_currency_name);
123         final TextView tvNoCurrBtn = csd.findViewById(R.id.btn_no_currency);
124         final TextView tvAddCurrOkBtn = csd.findViewById(R.id.btn_add_currency);
125         final TextView tvAddCurrBtn = csd.findViewById(R.id.btn_add_new);
126
127         tvNewCurrName.setVisibility(View.GONE);
128         tvAddCurrOkBtn.setVisibility(View.GONE);
129         tvNoCurrBtn.setVisibility(View.VISIBLE);
130         tvAddCurrBtn.setVisibility(View.VISIBLE);
131
132         tvAddCurrBtn.setOnClickListener(v -> {
133             tvNewCurrName.setVisibility(View.VISIBLE);
134             tvAddCurrOkBtn.setVisibility(View.VISIBLE);
135
136             tvNoCurrBtn.setVisibility(View.GONE);
137             tvAddCurrBtn.setVisibility(View.GONE);
138
139             tvNewCurrName.setText(null);
140             tvNewCurrName.requestFocus();
141             net.ktnx.mobileledger.utils.Misc.showSoftKeyboard(this);
142         });
143
144         tvAddCurrOkBtn.setOnClickListener(v -> {
145
146
147             String currName = String.valueOf(tvNewCurrName.getText());
148             if (!currName.isEmpty()) {
149                 List<Currency> list = new ArrayList<>(model.currencies.getValue());
150                 // FIXME hardcoded position and gap setting
151                 list.add(new Currency(profile, String.valueOf(tvNewCurrName.getText()),
152                         Currency.Position.after, false));
153                 model.currencies.setValue(list);
154             }
155
156             tvNewCurrName.setVisibility(View.GONE);
157             tvAddCurrOkBtn.setVisibility(View.GONE);
158
159             tvNoCurrBtn.setVisibility(View.VISIBLE);
160             tvAddCurrBtn.setVisibility(View.VISIBLE);
161         });
162
163         tvNoCurrBtn.setOnClickListener(v -> {
164             adapter.notifyCurrencySelected(null);
165             dismiss();
166         });
167
168         RadioButton rbPositionLeft = csd.findViewById(R.id.currency_position_left);
169         RadioButton rbPositionRight = csd.findViewById(R.id.currency_position_right);
170
171         if (Data.currencySymbolPosition.getValue() == Currency.Position.before)
172             rbPositionLeft.toggle();
173         else
174             rbPositionRight.toggle();
175
176         RadioGroup rgPosition = csd.findViewById(R.id.position_radio_group);
177         rgPosition.setOnCheckedChangeListener((group, checkedId) -> {
178             if (checkedId == R.id.currency_position_left)
179                 Data.currencySymbolPosition.setValue(Currency.Position.before);
180             else
181                 Data.currencySymbolPosition.setValue(Currency.Position.after);
182         });
183
184         SwitchMaterial gap = csd.findViewById(R.id.currency_gap);
185
186         gap.setChecked(Data.currencyGap.getValue());
187
188         gap.setOnCheckedChangeListener((v, checked) -> Data.currencyGap.setValue(checked));
189
190         model.observePositionAndPaddingVisible(this, visible -> csd.findViewById(R.id.params_panel)
191                                                                    .setVisibility(
192                                                                            visible ? View.VISIBLE
193                                                                                    : View.GONE));
194
195         if ((savedInstanceState != null) ? savedInstanceState.getBoolean(ARG_SHOW_PARAMS,
196                 DEFAULT_SHOW_PARAMS) : DEFAULT_SHOW_PARAMS)
197             model.showPositionAndPadding();
198         else
199             model.hidePositionAndPadding();
200
201         return csd;
202     }
203     public void setOnCurrencySelectedListener(OnCurrencySelectedListener listener) {
204         onCurrencySelectedListener = listener;
205
206         if (model != null)
207             model.setOnCurrencySelectedListener(listener);
208     }
209     public void resetOnCurrencySelectedListener() {
210         model.resetOnCurrencySelectedListener();
211     }
212     @Override
213     public void onCurrencySelected(Currency item) {
214         model.triggerOnCurrencySelectedListener(item);
215
216         dismiss();
217     }
218
219     @Override
220     public void onCurrencyLongClick(Currency item) {
221         ArrayList<Currency> list = new ArrayList<>(model.currencies.getValue());
222         App.getDatabase()
223            .execSQL("delete from currencies where id=?", new Object[]{item.getId()});
224         list.remove(item);
225         model.currencies.setValue(list);
226     }
227     public void showPositionAndPadding() {
228         deferredShowPositionAndPadding = true;
229     }
230     public void hidePositionAndPadding() {
231         deferredShowPositionAndPadding = false;
232     }
233 }