]> git.ktnx.net Git - mobile-ledger.git/blob - ui/CurrencySelectorFragment.java
bump androidx.constraintlayout library version
[mobile-ledger.git] / 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     private static final String ARG_COLUMN_COUNT = "column-count";
59     private int mColumnCount = DEFAULT_COLUMN_COUNT;
60     private OnCurrencySelectedListener mListener;
61     private CurrencySelectorModel model;
62
63     /**
64      * Mandatory empty constructor for the fragment manager to instantiate the
65      * fragment (e.g. upon screen orientation changes).
66      */
67     public CurrencySelectorFragment() {
68     }
69     @SuppressWarnings("unused")
70     public static CurrencySelectorFragment newInstance() {
71         return newInstance(DEFAULT_COLUMN_COUNT);
72     }
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);
78         return fragment;
79     }
80     @Override
81     public void onCreate(Bundle savedInstanceState) {
82         super.onCreate(savedInstanceState);
83
84         if (getArguments() != null) {
85             mColumnCount = getArguments().getInt(ARG_COLUMN_COUNT, DEFAULT_COLUMN_COUNT);
86         }
87     }
88     @NonNull
89     @Override
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);
95
96         RecyclerView recyclerView = csd.findViewById(R.id.list);
97
98         if (mColumnCount <= 1) {
99             recyclerView.setLayoutManager(new LinearLayoutManager(context));
100         }
101         else {
102             recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
103         }
104         model = new ViewModelProvider(this).get(CurrencySelectorModel.class);
105         MobileLedgerProfile profile = Objects.requireNonNull(Data.profile.getValue());
106
107         model.currencies.setValue(new CopyOnWriteArrayList<>(profile.getCurrencies()));
108         CurrencySelectorRecyclerViewAdapter adapter = new CurrencySelectorRecyclerViewAdapter();
109         model.currencies.observe(this, list -> adapter.submitList(list));
110
111         recyclerView.setAdapter(adapter);
112         adapter.setCurrencySelectedListener(this);
113         adapter.setCurrencyLongClickListener(this);
114
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);
119
120         tvNewCurrName.setVisibility(View.GONE);
121         tvAddCurrOkBtn.setVisibility(View.GONE);
122         tvNoCurrBtn.setVisibility(View.VISIBLE);
123         tvAddCurrBtn.setVisibility(View.VISIBLE);
124
125         tvAddCurrBtn.setOnClickListener(v -> {
126             tvNewCurrName.setVisibility(View.VISIBLE);
127             tvAddCurrOkBtn.setVisibility(View.VISIBLE);
128
129             tvNoCurrBtn.setVisibility(View.GONE);
130             tvAddCurrBtn.setVisibility(View.GONE);
131
132             tvNewCurrName.setText(null);
133             tvNewCurrName.requestFocus();
134             net.ktnx.mobileledger.utils.Misc.showSoftKeyboard(this);
135         });
136
137         tvAddCurrOkBtn.setOnClickListener(v -> {
138
139
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);
147             }
148
149             tvNewCurrName.setVisibility(View.GONE);
150             tvAddCurrOkBtn.setVisibility(View.GONE);
151
152             tvNoCurrBtn.setVisibility(View.VISIBLE);
153             tvAddCurrBtn.setVisibility(View.VISIBLE);
154         });
155
156         tvNoCurrBtn.setOnClickListener(v -> {
157             adapter.notifyCurrencySelected(null);
158             dismiss();
159         });
160
161         RadioButton rbPositionLeft = csd.findViewById(R.id.currency_position_left);
162         RadioButton rbPositionRight = csd.findViewById(R.id.currency_position_right);
163
164         if (Data.currencySymbolPosition.getValue() == Currency.Position.before)
165             rbPositionLeft.toggle();
166         else
167             rbPositionRight.toggle();
168
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);
173             else
174                 Data.currencySymbolPosition.setValue(Currency.Position.after);
175         });
176
177         Switch gap = csd.findViewById(R.id.currency_gap);
178
179         gap.setChecked(Data.currencyGap.getValue());
180
181         gap.setOnCheckedChangeListener((v, checked) -> {
182             Data.currencyGap.setValue(checked);
183         });
184
185         return csd;
186     }
187     public void setOnCurrencySelectedListener(OnCurrencySelectedListener listener) {
188         mListener = listener;
189     }
190     public void resetOnCurrencySelectedListener() {
191         mListener = null;
192     }
193     @Override
194     public void onCurrencySelected(Currency item) {
195         if (mListener != null)
196             mListener.onCurrencySelected(item);
197
198         dismiss();
199     }
200
201     @Override
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()});
205         list.remove(item);
206         model.currencies.setValue(list);
207     }
208 }