]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/CurrencySelectorFragment.java
Room-based profile management
[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.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;
44
45 import java.util.ArrayList;
46 import java.util.List;
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 CurrencySelectorModel model;
63     private boolean deferredShowPositionAndPadding;
64     private OnCurrencySelectedListener onCurrencySelectedListener;
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 = requireContext();
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         if (onCurrencySelectedListener != null)
110             model.setOnCurrencySelectedListener(onCurrencySelectedListener);
111         Profile profile = Data.getProfile();
112
113         CurrencySelectorRecyclerViewAdapter adapter = new CurrencySelectorRecyclerViewAdapter();
114         DB.get()
115           .getCurrencyDAO()
116           .getAll()
117           .observe(this, list -> {
118               List<String> strings = new ArrayList<>();
119               for (net.ktnx.mobileledger.db.Currency c : list) {
120                   strings.add(c.getName());
121               }
122               adapter.submitList(strings);
123           });
124
125         recyclerView.setAdapter(adapter);
126         adapter.setCurrencySelectedListener(this);
127         adapter.setCurrencyLongClickListener(this);
128
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
134         tvNewCurrName.setVisibility(View.GONE);
135         tvAddCurrOkBtn.setVisibility(View.GONE);
136         tvNoCurrBtn.setVisibility(View.VISIBLE);
137         tvAddCurrBtn.setVisibility(View.VISIBLE);
138
139         tvAddCurrBtn.setOnClickListener(v -> {
140             tvNewCurrName.setVisibility(View.VISIBLE);
141             tvAddCurrOkBtn.setVisibility(View.VISIBLE);
142
143             tvNoCurrBtn.setVisibility(View.GONE);
144             tvAddCurrBtn.setVisibility(View.GONE);
145
146             tvNewCurrName.setText(null);
147             tvNewCurrName.requestFocus();
148             net.ktnx.mobileledger.utils.Misc.showSoftKeyboard(this);
149         });
150
151         tvAddCurrOkBtn.setOnClickListener(v -> {
152
153
154             String currName = String.valueOf(tvNewCurrName.getText());
155             if (!currName.isEmpty()) {
156                 DB.get()
157                   .getCurrencyDAO()
158                   .insert(new net.ktnx.mobileledger.db.Currency(null,
159                           String.valueOf(tvNewCurrName.getText()), "after", false), null);
160                 // FIXME hardcoded position and gap setting
161             }
162
163             tvNewCurrName.setVisibility(View.GONE);
164             tvAddCurrOkBtn.setVisibility(View.GONE);
165
166             tvNoCurrBtn.setVisibility(View.VISIBLE);
167             tvAddCurrBtn.setVisibility(View.VISIBLE);
168         });
169
170         tvNoCurrBtn.setOnClickListener(v -> {
171             adapter.notifyCurrencySelected(null);
172             dismiss();
173         });
174
175         RadioButton rbPositionLeft = csd.findViewById(R.id.currency_position_left);
176         RadioButton rbPositionRight = csd.findViewById(R.id.currency_position_right);
177
178         if (Data.currencySymbolPosition.getValue() == Currency.Position.before)
179             rbPositionLeft.toggle();
180         else
181             rbPositionRight.toggle();
182
183         RadioGroup rgPosition = csd.findViewById(R.id.position_radio_group);
184         rgPosition.setOnCheckedChangeListener((group, checkedId) -> {
185             if (checkedId == R.id.currency_position_left)
186                 Data.currencySymbolPosition.setValue(Currency.Position.before);
187             else
188                 Data.currencySymbolPosition.setValue(Currency.Position.after);
189         });
190
191         SwitchMaterial gap = csd.findViewById(R.id.currency_gap);
192
193         gap.setChecked(Data.currencyGap.getValue());
194
195         gap.setOnCheckedChangeListener((v, checked) -> Data.currencyGap.setValue(checked));
196
197         model.observePositionAndPaddingVisible(this, visible -> csd.findViewById(R.id.params_panel)
198                                                                    .setVisibility(
199                                                                            visible ? View.VISIBLE
200                                                                                    : View.GONE));
201
202         if ((savedInstanceState != null) ? savedInstanceState.getBoolean(ARG_SHOW_PARAMS,
203                 DEFAULT_SHOW_PARAMS) : DEFAULT_SHOW_PARAMS)
204             model.showPositionAndPadding();
205         else
206             model.hidePositionAndPadding();
207
208         return csd;
209     }
210     public void setOnCurrencySelectedListener(OnCurrencySelectedListener listener) {
211         onCurrencySelectedListener = listener;
212
213         if (model != null)
214             model.setOnCurrencySelectedListener(listener);
215     }
216     public void resetOnCurrencySelectedListener() {
217         model.resetOnCurrencySelectedListener();
218     }
219     @Override
220     public void onCurrencySelected(String item) {
221         model.triggerOnCurrencySelectedListener(item);
222
223         dismiss();
224     }
225
226     @Override
227     public void onCurrencyLongClick(String item) {
228         CurrencyDAO dao = DB.get()
229                             .getCurrencyDAO();
230         dao.getByName(item)
231            .observe(this, dao::deleteSync);
232     }
233     public void showPositionAndPadding() {
234         deferredShowPositionAndPadding = true;
235     }
236     public void hidePositionAndPadding() {
237         deferredShowPositionAndPadding = false;
238     }
239 }