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