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.annotation.SuppressLint;
21 import android.os.Build;
22 import android.text.Editable;
23 import android.text.TextWatcher;
24 import android.text.method.DigitsKeyListener;
25 import android.view.View;
26 import android.view.inputmethod.EditorInfo;
27 import android.widget.AutoCompleteTextView;
28 import android.widget.FrameLayout;
29 import android.widget.LinearLayout;
30 import android.widget.TextView;
32 import androidx.annotation.NonNull;
33 import androidx.lifecycle.Observer;
34 import androidx.recyclerview.widget.RecyclerView;
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
38 import net.ktnx.mobileledger.model.Data;
39 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
40 import net.ktnx.mobileledger.model.MobileLedgerProfile;
41 import net.ktnx.mobileledger.ui.DatePickerFragment;
42 import net.ktnx.mobileledger.utils.Logger;
43 import net.ktnx.mobileledger.utils.MLDB;
44 import net.ktnx.mobileledger.utils.Misc;
46 import java.text.DecimalFormatSymbols;
47 import java.util.Calendar;
48 import java.util.Date;
49 import java.util.GregorianCalendar;
50 import java.util.Locale;
52 class NewTransactionItemHolder extends RecyclerView.ViewHolder
53 implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
54 private final String decimalSeparator;
55 private final String decimalDot;
56 private NewTransactionModel.Item item;
57 private TextView tvDate;
58 private AutoCompleteTextView tvDescription;
59 private AutoCompleteTextView tvAccount;
60 private TextView tvAmount;
61 private LinearLayout lHead;
62 private LinearLayout lAccount;
63 private FrameLayout lPadding;
64 private MobileLedgerProfile mProfile;
66 private Observer<Date> dateObserver;
67 private Observer<String> descriptionObserver;
68 private Observer<String> hintObserver;
69 private Observer<Integer> focusedAccountObserver;
70 private Observer<Integer> accountCountObserver;
71 private Observer<Boolean> editableObserver;
72 private boolean inUpdate = false;
73 private boolean syncingData = false;
74 NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
76 tvAccount = itemView.findViewById(R.id.account_row_acc_name);
77 tvAmount = itemView.findViewById(R.id.account_row_acc_amounts);
78 tvDate = itemView.findViewById(R.id.new_transaction_date);
79 tvDescription = itemView.findViewById(R.id.new_transaction_description);
80 lHead = itemView.findViewById(R.id.ntr_data);
81 lAccount = itemView.findViewById(R.id.ntr_account);
82 lPadding = itemView.findViewById(R.id.ntr_padding);
84 tvDescription.setNextFocusForwardId(View.NO_ID);
85 tvAccount.setNextFocusForwardId(View.NO_ID);
86 tvAmount.setNextFocusForwardId(View.NO_ID); // magic!
88 tvDate.setOnClickListener(v -> pickTransactionDate());
90 mProfile = Data.profile.getValue();
92 throw new AssertionError();
94 View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
96 boolean wasSyncing = syncingData;
99 adapter.updateFocusedItem(getAdapterPosition());
102 syncingData = wasSyncing;
107 tvDescription.setOnFocusChangeListener(focusMonitor);
108 tvAccount.setOnFocusChangeListener(focusMonitor);
109 tvAmount.setOnFocusChangeListener(focusMonitor);
111 MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription,
112 MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
113 MLDB.hookAutocompletionAdapter(tvAccount.getContext(), tvAccount, MLDB.ACCOUNTS_TABLE,
114 "name", true, this, mProfile);
116 // FIXME: react on configuration (locale) changes
117 decimalSeparator = String.valueOf(DecimalFormatSymbols.getInstance()
118 .getMonetaryDecimalSeparator());
121 final TextWatcher tw = new TextWatcher() {
123 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
127 public void onTextChanged(CharSequence s, int start, int before, int count) {
131 public void afterTextChanged(Editable s) {
132 // debug("input", "text changed");
136 Logger.debug("textWatcher", "calling syncData()");
138 Logger.debug("textWatcher",
139 "syncData() returned, checking if transaction is submittable");
140 adapter.model.checkTransactionSubmittable(adapter);
141 Logger.debug("textWatcher", "done");
144 final TextWatcher amountWatcher = new TextWatcher() {
146 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
149 public void onTextChanged(CharSequence s, int start, int before, int count) {
153 public void afterTextChanged(Editable s) {
154 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
155 // only one decimal separator is allowed
156 // plus and minus are allowed only at the beginning
157 String val = s.toString();
159 tvAmount.setKeyListener(DigitsKeyListener.getInstance(
160 "0123456789+-" + decimalSeparator + decimalDot));
161 else if (val.contains(decimalSeparator) || val.contains(decimalDot))
162 tvAmount.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
164 tvAmount.setKeyListener(DigitsKeyListener.getInstance(
165 "0123456789" + decimalSeparator + decimalDot));
168 adapter.model.checkTransactionSubmittable(adapter);
172 tvDescription.addTextChangedListener(tw);
173 tvAccount.addTextChangedListener(tw);
174 tvAmount.addTextChangedListener(amountWatcher);
176 // FIXME: react on locale changes
177 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
178 tvAmount.setKeyListener(DigitsKeyListener.getInstance(Locale.getDefault(), true, true));
180 tvAmount.setKeyListener(
181 DigitsKeyListener.getInstance("0123456789+-" + decimalSeparator + decimalDot));
183 dateObserver = date -> {
188 tvDate.setText(item.getFormattedDate());
194 descriptionObserver = description -> {
199 tvDescription.setText(description);
205 hintObserver = hint -> {
211 tvAmount.setHint(R.string.zero_amount);
213 tvAmount.setHint(hint);
219 editableObserver = this::setEditable;
220 focusedAccountObserver = index -> {
221 if ((index != null) && index.equals(getAdapterPosition())) {
222 switch (item.getType()) {
224 // bad idea - double pop-up, and not really necessary.
225 // the user can tap the input to get the calendar
226 //if (!tvDate.hasFocus()) tvDate.requestFocus();
227 boolean focused = tvDescription.requestFocus();
228 tvDescription.dismissDropDown();
230 Misc.showSoftKeyboard(
231 (NewTransactionActivity) tvDescription.getContext());
234 // do nothing if a row element already has the focus
235 if (!itemView.hasFocus()) {
236 focused = tvAccount.requestFocus();
237 tvAccount.dismissDropDown();
239 Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
246 accountCountObserver = count -> {
247 final int adapterPosition = getAdapterPosition();
248 final int layoutPosition = getLayoutPosition();
249 Logger.debug("holder",
250 String.format(Locale.US, "count=%d; pos=%d, layoutPos=%d [%s]", count,
251 adapterPosition, layoutPosition, item.getType()
253 .concat(item.getType() ==
254 NewTransactionModel.ItemType.transactionRow
255 ? String.format(Locale.US,
261 ? String.format(Locale.US,
266 if (adapterPosition == count)
267 tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE);
269 tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
272 private void setEditable(Boolean editable) {
273 tvDate.setEnabled(editable);
274 tvDescription.setEnabled(editable);
275 tvAccount.setEnabled(editable);
276 tvAmount.setEnabled(editable);
278 private void beginUpdates() {
280 throw new RuntimeException("Already in update mode");
283 private void endUpdates() {
285 throw new RuntimeException("Not in update mode");
291 * Stores the data from the UI elements into the model item
293 private void syncData() {
298 Logger.debug("new-trans", "skipping syncData() loop");
305 switch (item.getType()) {
307 item.setDate(String.valueOf(tvDate.getText()));
308 item.setDescription(String.valueOf(tvDescription.getText()));
312 .setAccountName(String.valueOf(tvAccount.getText()));
314 // TODO: handle multiple amounts
315 String amount = String.valueOf(tvAmount.getText());
316 amount = amount.trim();
318 if (amount.isEmpty()) {
324 amount = amount.replace(decimalSeparator, decimalDot);
326 .setAmount(Float.parseFloat(amount));
328 catch (NumberFormatException e) {
329 Logger.debug("new-trans", String.format(
330 "assuming amount is not set due to number format exception. " +
331 "input was '%s'", amount));
339 throw new RuntimeException("Should not happen");
346 private void pickTransactionDate() {
347 DatePickerFragment picker = new DatePickerFragment();
348 picker.setOnDatePickedListener(this);
349 picker.show(((NewTransactionActivity) tvDate.getContext()).getSupportFragmentManager(),
355 * @param item updates the UI elements with the data from the model item
357 @SuppressLint("DefaultLocale")
358 public void setData(NewTransactionModel.Item item) {
361 if (this.item != null && !this.item.equals(item)) {
362 this.item.stopObservingDate(dateObserver);
363 this.item.stopObservingDescription(descriptionObserver);
364 this.item.stopObservingAmountHint(hintObserver);
365 this.item.stopObservingEditableFlag(editableObserver);
367 .stopObservingFocusedItem(focusedAccountObserver);
369 .stopObservingAccountCount(accountCountObserver);
374 switch (item.getType()) {
376 tvDate.setText(item.getFormattedDate());
377 tvDescription.setText(item.getDescription());
378 lHead.setVisibility(View.VISIBLE);
379 lAccount.setVisibility(View.GONE);
380 lPadding.setVisibility(View.GONE);
384 LedgerTransactionAccount acc = item.getAccount();
385 tvAccount.setText(acc.getAccountName());
386 if (acc.isAmountSet()) {
387 tvAmount.setText(String.format("%1.2f", acc.getAmount()));
390 tvAmount.setText("");
391 // tvAmount.setHint(R.string.zero_amount);
393 tvAmount.setHint(item.getAmountHint());
394 lHead.setVisibility(View.GONE);
395 lAccount.setVisibility(View.VISIBLE);
396 lPadding.setVisibility(View.GONE);
400 lHead.setVisibility(View.GONE);
401 lAccount.setVisibility(View.GONE);
402 lPadding.setVisibility(View.VISIBLE);
407 if (this.item == null) { // was null or has changed
409 final NewTransactionActivity activity =
410 (NewTransactionActivity) tvDescription.getContext();
411 item.observeDate(activity, dateObserver);
412 item.observeDescription(activity, descriptionObserver);
413 item.observeAmountHint(activity, hintObserver);
414 item.observeEditableFlag(activity, editableObserver);
416 .observeFocusedItem(activity, focusedAccountObserver);
418 .observeAccountCount(activity, accountCountObserver);
426 public void onDatePicked(int year, int month, int day) {
427 final Calendar c = GregorianCalendar.getInstance();
428 c.set(year, month, day);
429 item.setDate(c.getTime());
430 boolean focused = tvDescription.requestFocus();
432 Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
436 public void descriptionSelected(String description) {
437 tvAccount.setText(description);
438 tvAmount.requestFocus(View.FOCUS_FORWARD);