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.activity;
20 import android.text.Editable;
21 import android.text.TextWatcher;
22 import android.view.View;
23 import android.view.inputmethod.EditorInfo;
24 import android.widget.AutoCompleteTextView;
25 import android.widget.FrameLayout;
26 import android.widget.LinearLayout;
27 import android.widget.TextView;
29 import androidx.annotation.NonNull;
30 import androidx.constraintlayout.widget.ConstraintLayout;
31 import androidx.lifecycle.Observer;
32 import androidx.recyclerview.widget.RecyclerView;
34 import net.ktnx.mobileledger.R;
35 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
36 import net.ktnx.mobileledger.model.Data;
37 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
38 import net.ktnx.mobileledger.model.MobileLedgerProfile;
39 import net.ktnx.mobileledger.ui.DatePickerFragment;
40 import net.ktnx.mobileledger.utils.Logger;
41 import net.ktnx.mobileledger.utils.MLDB;
42 import net.ktnx.mobileledger.utils.Misc;
44 import java.util.Calendar;
45 import java.util.Date;
46 import java.util.GregorianCalendar;
47 import java.util.Locale;
49 class NewTransactionItemHolder extends RecyclerView.ViewHolder
50 implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
51 private NewTransactionModel.Item item;
52 private TextView tvDate;
53 private AutoCompleteTextView tvDescription;
54 private AutoCompleteTextView tvAccount;
55 private TextView tvAmount;
56 private ConstraintLayout lHead;
57 private LinearLayout lAccount;
58 private FrameLayout lPadding;
59 private MobileLedgerProfile mProfile;
61 private Observer<Date> dateObserver;
62 private Observer<String> descriptionObserver;
63 private Observer<String> hintObserver;
64 private Observer<Integer> focusedAccountObserver;
65 private Observer<Integer> accountCountObserver;
66 private boolean inUpdate = false;
67 private boolean syncingData = false;
68 NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
70 tvAccount = itemView.findViewById(R.id.account_row_acc_name);
71 tvAmount = itemView.findViewById(R.id.account_row_acc_amounts);
72 tvDate = itemView.findViewById(R.id.new_transaction_date);
73 tvDescription = itemView.findViewById(R.id.new_transaction_description);
74 lHead = itemView.findViewById(R.id.ntr_data);
75 lAccount = itemView.findViewById(R.id.ntr_account);
76 lPadding = itemView.findViewById(R.id.ntr_padding);
78 tvDescription.setNextFocusForwardId(View.NO_ID);
79 tvAccount.setNextFocusForwardId(View.NO_ID);
80 tvAmount.setNextFocusForwardId(View.NO_ID); // magic!
82 tvDate.setOnFocusChangeListener((v, hasFocus) -> {
83 if (hasFocus) pickTransactionDate();
85 tvDate.setOnClickListener(v -> pickTransactionDate());
87 mProfile = Data.profile.getValue();
88 if (mProfile == null) throw new AssertionError();
90 MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription,
91 MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
92 MLDB.hookAutocompletionAdapter(tvAccount.getContext(), tvAccount, MLDB.ACCOUNTS_TABLE,
93 "name", true, this, mProfile);
95 final TextWatcher tw = new TextWatcher() {
97 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
101 public void onTextChanged(CharSequence s, int start, int before, int count) {
105 public void afterTextChanged(Editable s) {
106 // debug("input", "text changed");
107 if (inUpdate) return;
109 Logger.debug("textWatcher", "calling syncData()");
111 Logger.debug("textWatcher",
112 "syncData() returned, checking if transaction is submittable");
113 adapter.model.checkTransactionSubmittable(adapter);
114 Logger.debug("textWatcher", "done");
117 tvDescription.addTextChangedListener(tw);
118 tvAccount.addTextChangedListener(tw);
119 tvAmount.addTextChangedListener(tw);
121 dateObserver = date -> {
122 if (syncingData) return;
125 tvDate.setText(item.getFormattedDate());
131 descriptionObserver = description -> {
132 if (syncingData) return;
135 tvDescription.setText(description);
141 hintObserver = hint -> {
142 if (syncingData) return;
145 tvAmount.setHint(hint);
151 focusedAccountObserver = index -> {
152 if ((index != null) && index.equals(getAdapterPosition())) {
153 switch (item.getType()) {
155 // bad idea - double pop-up, and not really necessary.
156 // the user can tap the input to get the calendar
157 //if (!tvDate.hasFocus()) tvDate.requestFocus();
158 boolean focused = tvDescription.requestFocus();
159 tvDescription.dismissDropDown();
160 if (focused) Misc.showSoftKeyboard(
161 (NewTransactionActivity) tvDescription.getContext());
164 focused = tvAccount.requestFocus();
165 tvAccount.dismissDropDown();
167 Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
173 accountCountObserver = count -> {
174 if (getAdapterPosition() == count) tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE);
175 else tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
178 private void beginUpdates() {
179 if (inUpdate) throw new RuntimeException("Already in update mode");
182 private void endUpdates() {
183 if (!inUpdate) throw new RuntimeException("Not in update mode");
189 * Stores the data from the UI elements into the model item
191 private void syncData() {
192 if (item == null) return;
195 Logger.debug("new-trans", "skipping syncData() loop");
202 switch (item.getType()) {
204 item.setDate(String.valueOf(tvDate.getText()));
205 item.setDescription(String.valueOf(tvDescription.getText()));
209 .setAccountName(String.valueOf(tvAccount.getText()));
211 // TODO: handle multiple amounts
212 String amount = String.valueOf(tvAmount.getText());
213 amount = amount.trim();
215 if (!amount.isEmpty()) item.getAccount()
216 .setAmount(Float.parseFloat(amount));
217 else item.getAccount()
222 throw new RuntimeException("Should not happen");
229 private void pickTransactionDate() {
230 DatePickerFragment picker = new DatePickerFragment();
231 picker.setOnDatePickedListener(this);
232 picker.show(((NewTransactionActivity) tvDate.getContext()).getSupportFragmentManager(),
238 * @param item updates the UI elements with the data from the model item
240 public void setData(NewTransactionModel.Item item) {
243 if (this.item != null && !this.item.equals(item)) {
244 this.item.stopObservingDate(dateObserver);
245 this.item.stopObservingDescription(descriptionObserver);
246 this.item.stopObservingAmountHint(hintObserver);
248 .stopObservingFocusedItem(focusedAccountObserver);
250 .stopObservingAccountCount(accountCountObserver);
255 switch (item.getType()) {
257 tvDate.setText(item.getFormattedDate());
258 tvDescription.setText(item.getDescription());
259 lHead.setVisibility(View.VISIBLE);
260 lAccount.setVisibility(View.GONE);
261 lPadding.setVisibility(View.GONE);
264 LedgerTransactionAccount acc = item.getAccount();
265 tvAccount.setText(acc.getAccountName());
267 acc.isAmountSet() ? String.format(Locale.US, "%1.2f", acc.getAmount())
269 lHead.setVisibility(View.GONE);
270 lAccount.setVisibility(View.VISIBLE);
271 lPadding.setVisibility(View.GONE);
274 lHead.setVisibility(View.GONE);
275 lAccount.setVisibility(View.GONE);
276 lPadding.setVisibility(View.VISIBLE);
280 if (this.item == null) { // was null or has changed
282 final NewTransactionActivity activity =
283 (NewTransactionActivity) tvDescription.getContext();
284 item.observeDate(activity, dateObserver);
285 item.observeDescription(activity, descriptionObserver);
286 item.observeAmountHint(activity, hintObserver);
288 .observeFocusedItem(activity, focusedAccountObserver);
290 .observeAccountCount(activity, accountCountObserver);
298 public void onDatePicked(int year, int month, int day) {
299 final Calendar c = GregorianCalendar.getInstance();
300 c.set(year, month, day);
301 item.setDate(c.getTime());
302 boolean focused = tvDescription.requestFocus();
303 if (focused) Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
307 public void descriptionSelected(String description) {
308 tvAccount.setText(description);
309 tvAmount.requestFocus(View.FOCUS_FORWARD);