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.graphics.Typeface;
22 import android.os.Build;
23 import android.text.Editable;
24 import android.text.TextWatcher;
25 import android.text.method.DigitsKeyListener;
26 import android.view.Gravity;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.view.inputmethod.EditorInfo;
30 import android.widget.AutoCompleteTextView;
31 import android.widget.EditText;
32 import android.widget.FrameLayout;
33 import android.widget.ProgressBar;
34 import android.widget.TextView;
36 import androidx.annotation.NonNull;
37 import androidx.appcompat.app.AppCompatActivity;
38 import androidx.constraintlayout.widget.ConstraintLayout;
39 import androidx.lifecycle.Observer;
40 import androidx.recyclerview.widget.RecyclerView;
42 import net.ktnx.mobileledger.R;
43 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
44 import net.ktnx.mobileledger.model.Currency;
45 import net.ktnx.mobileledger.model.Data;
46 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
47 import net.ktnx.mobileledger.model.MobileLedgerProfile;
48 import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
49 import net.ktnx.mobileledger.ui.DatePickerFragment;
50 import net.ktnx.mobileledger.ui.TextViewClearHelper;
51 import net.ktnx.mobileledger.utils.Colors;
52 import net.ktnx.mobileledger.utils.DimensionUtils;
53 import net.ktnx.mobileledger.utils.Logger;
54 import net.ktnx.mobileledger.utils.MLDB;
55 import net.ktnx.mobileledger.utils.Misc;
57 import org.jetbrains.annotations.NotNull;
59 import java.text.DecimalFormatSymbols;
60 import java.util.Calendar;
61 import java.util.Date;
62 import java.util.GregorianCalendar;
63 import java.util.Locale;
65 import static net.ktnx.mobileledger.ui.activity.NewTransactionModel.ItemType;
67 class NewTransactionItemHolder extends RecyclerView.ViewHolder
68 implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
69 private final String decimalSeparator;
70 private final String decimalDot;
71 private final TextView tvCurrency;
72 private NewTransactionModel.Item item;
73 private TextView tvDate;
74 private AutoCompleteTextView tvDescription;
75 private AutoCompleteTextView tvAccount;
76 private TextView tvComment;
77 private EditText tvAmount;
78 private ViewGroup lHead;
79 private ViewGroup lAccount;
80 private FrameLayout lPadding;
81 private MobileLedgerProfile mProfile;
83 private Observer<Date> dateObserver;
84 private Observer<String> descriptionObserver;
85 private Observer<String> hintObserver;
86 private Observer<Integer> focusedAccountObserver;
87 private Observer<Integer> accountCountObserver;
88 private Observer<Boolean> editableObserver;
89 private Observer<Currency.Position> currencyPositionObserver;
90 private Observer<Boolean> currencyGapObserver;
91 private Observer<Locale> localeObserver;
92 private Observer<Currency> currencyObserver;
93 private Observer<Boolean> showCurrencyObserver;
94 private Observer<String> commentObserver;
95 private Observer<Boolean> busyFlagObserver;
96 private boolean inUpdate = false;
97 private boolean syncingData = false;
98 private View commentButton;
99 //TODO multiple amounts with different currencies per posting
100 NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
102 tvAccount = itemView.findViewById(R.id.account_row_acc_name);
103 tvComment = itemView.findViewById(R.id.comment);
104 new TextViewClearHelper().attachToTextView((EditText) tvComment);
105 commentButton = itemView.findViewById(R.id.comment_button);
106 tvAmount = itemView.findViewById(R.id.account_row_acc_amounts);
107 tvCurrency = itemView.findViewById(R.id.currency);
108 tvDate = itemView.findViewById(R.id.new_transaction_date);
109 tvDescription = itemView.findViewById(R.id.new_transaction_description);
110 lHead = itemView.findViewById(R.id.ntr_data);
111 lAccount = itemView.findViewById(R.id.ntr_account);
112 lPadding = itemView.findViewById(R.id.ntr_padding);
113 View commentLayout = itemView.findViewById(R.id.comment_layout);
114 ProgressBar p = itemView.findViewById(R.id.progressBar);
116 tvDescription.setNextFocusForwardId(View.NO_ID);
117 tvAccount.setNextFocusForwardId(View.NO_ID);
118 tvAmount.setNextFocusForwardId(View.NO_ID); // magic!
120 tvDate.setOnClickListener(v -> pickTransactionDate());
122 itemView.findViewById(R.id.comment_button)
123 .setOnClickListener(v -> {
124 tvComment.setVisibility(View.VISIBLE);
125 tvComment.requestFocus();
128 mProfile = Data.profile.getValue();
129 if (mProfile == null)
130 throw new AssertionError();
132 View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
133 final int id = v.getId();
135 boolean wasSyncing = syncingData;
138 final int pos = getAdapterPosition();
139 adapter.updateFocusedItem(pos);
141 case R.id.account_row_acc_name:
142 adapter.noteFocusIsOnAccount(pos);
144 case R.id.account_row_acc_amounts:
145 adapter.noteFocusIsOnAmount(pos);
148 adapter.noteFocusIsOnComment(pos);
153 syncingData = wasSyncing;
157 if (id == R.id.comment) {
158 commentLayout.setAlpha(hasFocus ? 1f : 0.5f);
159 tvComment.setTypeface(null, hasFocus ? Typeface.NORMAL : Typeface.ITALIC);
161 tvComment.setHint(R.string.transaction_account_comment_hint);
163 tvComment.setHint("");
165 if (!hasFocus && Misc.isEmptyOrNull(tvComment.getText()))
166 tvComment.setVisibility(View.INVISIBLE);
170 tvDescription.setOnFocusChangeListener(focusMonitor);
171 tvAccount.setOnFocusChangeListener(focusMonitor);
172 tvAmount.setOnFocusChangeListener(focusMonitor);
173 tvComment.setOnFocusChangeListener(focusMonitor);
175 MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription,
176 MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
177 MLDB.hookAutocompletionAdapter(tvAccount.getContext(), tvAccount, MLDB.ACCOUNTS_TABLE,
178 "name", true, this, mProfile);
180 // FIXME: react on configuration (locale) changes
181 decimalSeparator = String.valueOf(DecimalFormatSymbols.getInstance()
182 .getMonetaryDecimalSeparator());
185 final TextWatcher tw = new TextWatcher() {
187 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
191 public void onTextChanged(CharSequence s, int start, int before, int count) {
195 public void afterTextChanged(Editable s) {
196 // debug("input", "text changed");
200 Logger.debug("textWatcher", "calling syncData()");
202 Logger.debug("textWatcher",
203 "syncData() returned, checking if transaction is submittable");
204 adapter.checkTransactionSubmittable();
205 Logger.debug("textWatcher", "done");
208 final TextWatcher amountWatcher = new TextWatcher() {
210 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
212 String.format(Locale.US, "beforeTextChanged: start=%d, count=%d, after=%d",
213 start, count, after));
216 public void onTextChanged(CharSequence s, int start, int before, int count) {}
218 public void afterTextChanged(Editable s) {
219 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
220 // only one decimal separator is allowed
221 // plus and minus are allowed only at the beginning
222 String allowed = "0123456789";
223 String val = s.toString();
224 Logger.debug("input", val);
225 if (val.isEmpty() || (tvAmount.getSelectionStart() == 0))
227 if (!val.contains(decimalSeparator) && !val.contains(decimalDot))
228 allowed += decimalSeparator + decimalDot;
230 tvAmount.setKeyListener(DigitsKeyListener.getInstance(allowed));
234 adapter.checkTransactionSubmittable();
237 tvDescription.addTextChangedListener(tw);
238 tvAccount.addTextChangedListener(tw);
239 tvComment.addTextChangedListener(tw);
240 tvAmount.addTextChangedListener(amountWatcher);
242 tvCurrency.setOnClickListener(v -> {
243 CurrencySelectorFragment cpf = new CurrencySelectorFragment();
244 cpf.showPositionAndPadding();
245 cpf.setOnCurrencySelectedListener(c -> item.setCurrency(c));
246 final AppCompatActivity activity = (AppCompatActivity) v.getContext();
247 cpf.show(activity.getSupportFragmentManager(), "currency-selector");
250 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
251 tvAmount.setKeyListener(
252 DigitsKeyListener.getInstance(Data.locale.getValue(), true, true));
254 tvAmount.setKeyListener(
255 DigitsKeyListener.getInstance("0123456789+-" + decimalSeparator + decimalDot));
257 dateObserver = date -> {
262 tvDate.setText(item.getFormattedDate());
268 descriptionObserver = description -> {
273 tvDescription.setText(description);
279 hintObserver = hint -> {
285 tvAmount.setHint(R.string.zero_amount);
287 tvAmount.setHint(hint);
293 editableObserver = this::setEditable;
294 focusedAccountObserver = index -> {
295 if ((index != null) && index.equals(getAdapterPosition())) {
296 switch (item.getType()) {
298 // bad idea - double pop-up, and not really necessary.
299 // the user can tap the input to get the calendar
300 //if (!tvDate.hasFocus()) tvDate.requestFocus();
301 boolean focused = tvDescription.requestFocus();
302 tvDescription.dismissDropDown();
304 Misc.showSoftKeyboard(
305 (NewTransactionActivity) tvDescription.getContext());
308 // do nothing if a row element already has the focus
309 if (!itemView.hasFocus()) {
310 switch (item.getFocusedElement()) {
312 tvAmount.requestFocus();
315 tvComment.setVisibility(View.VISIBLE);
316 tvComment.requestFocus();
319 focused = tvAccount.requestFocus();
320 tvAccount.dismissDropDown();
322 Misc.showSoftKeyboard(
323 (NewTransactionActivity) tvAccount.getContext());
332 accountCountObserver = count -> {
333 final int adapterPosition = getAdapterPosition();
334 final int layoutPosition = getLayoutPosition();
335 Logger.debug("holder",
336 String.format(Locale.US, "count=%d; pos=%d, layoutPos=%d [%s]", count,
337 adapterPosition, layoutPosition, item.getType()
339 .concat(item.getType() ==
340 ItemType.transactionRow
341 ? String.format(Locale.US,
347 ? String.format(Locale.US,
352 if (adapterPosition == count)
353 tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE);
355 tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
358 localeObserver = locale -> {
359 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
360 tvAmount.setKeyListener(DigitsKeyListener.getInstance(locale, true, true));
363 currencyObserver = currency -> {
364 setCurrency(currency);
365 adapter.checkTransactionSubmittable();
368 currencyGapObserver =
369 hasGap -> updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(),
372 currencyPositionObserver =
373 position -> updateCurrencyPositionAndPadding(position, Data.currencyGap.getValue());
375 showCurrencyObserver = showCurrency -> {
377 tvCurrency.setVisibility(View.VISIBLE);
380 tvCurrency.setVisibility(View.GONE);
381 item.setCurrency(null);
385 commentObserver = comment -> {
386 final View focusedView = tvComment.findFocus();
387 tvComment.setTypeface(null,
388 (focusedView == tvComment) ? Typeface.NORMAL : Typeface.ITALIC);
389 tvComment.setVisibility(
390 ((focusedView != tvComment) && Misc.isEmptyOrNull(comment)) ? View.INVISIBLE
394 busyFlagObserver = isBusy -> {p.setVisibility(isBusy ? View.VISIBLE : View.INVISIBLE);};
396 private void updateCurrencyPositionAndPadding(Currency.Position position, boolean hasGap) {
397 ConstraintLayout.LayoutParams amountLP =
398 (ConstraintLayout.LayoutParams) tvAmount.getLayoutParams();
399 ConstraintLayout.LayoutParams currencyLP =
400 (ConstraintLayout.LayoutParams) tvCurrency.getLayoutParams();
402 if (position == Currency.Position.before) {
403 currencyLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
404 currencyLP.endToEnd = ConstraintLayout.LayoutParams.UNSET;
406 amountLP.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
407 amountLP.endToStart = ConstraintLayout.LayoutParams.UNSET;
408 amountLP.startToStart = ConstraintLayout.LayoutParams.UNSET;
409 amountLP.startToEnd = tvCurrency.getId();
411 tvCurrency.setGravity(Gravity.END);
414 currencyLP.startToStart = ConstraintLayout.LayoutParams.UNSET;
415 currencyLP.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
417 amountLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
418 amountLP.startToEnd = ConstraintLayout.LayoutParams.UNSET;
419 amountLP.endToEnd = ConstraintLayout.LayoutParams.UNSET;
420 amountLP.endToStart = tvCurrency.getId();
422 tvCurrency.setGravity(Gravity.START);
425 amountLP.resolveLayoutDirection(tvAmount.getLayoutDirection());
426 currencyLP.resolveLayoutDirection(tvCurrency.getLayoutDirection());
428 tvAmount.setLayoutParams(amountLP);
429 tvCurrency.setLayoutParams(currencyLP);
431 // distance between the amount and the currency symbol
432 int gapSize = DimensionUtils.sp2px(tvCurrency.getContext(), 5);
434 if (position == Currency.Position.before) {
435 tvCurrency.setPaddingRelative(0, 0, hasGap ? gapSize : 0, 0);
438 tvCurrency.setPaddingRelative(hasGap ? gapSize : 0, 0, 0, 0);
441 private void setCurrencyString(String currency) {
442 if ((currency == null) || currency.isEmpty()) {
443 tvCurrency.setText(R.string.currency_symbol);
444 tvCurrency.setTextColor(0x7f000000 + (0x00ffffff & Colors.defaultTextColor));
447 tvCurrency.setText(currency);
448 tvCurrency.setTextColor(Colors.defaultTextColor);
451 private void setCurrency(Currency currency) {
452 setCurrencyString((currency == null) ? null : currency.getName());
454 private void setEditable(Boolean editable) {
455 tvDate.setEnabled(editable);
456 tvDescription.setEnabled(editable);
457 tvAccount.setEnabled(editable);
458 tvAmount.setEnabled(editable);
460 private void setCommentVisible(@NotNull Boolean visible) {
462 // showing; show the comment view and align the comment button to it
463 tvComment.setVisibility(View.VISIBLE);
464 tvComment.requestFocus();
465 ConstraintLayout.LayoutParams lp =
466 (ConstraintLayout.LayoutParams) commentButton.getLayoutParams();
467 lp.bottomToBottom = R.id.comment;
469 commentButton.setLayoutParams(lp);
472 // hiding; hide the comment view and align the comment bottom to the amount
473 tvComment.setVisibility(View.GONE);
474 ConstraintLayout.LayoutParams lp =
475 (ConstraintLayout.LayoutParams) commentButton.getLayoutParams();
476 lp.bottomToBottom = R.id.amount_layout; // R.id.parent doesn't work here
478 commentButton.setLayoutParams(lp);
481 private void beginUpdates() {
483 throw new RuntimeException("Already in update mode");
486 private void endUpdates() {
488 throw new RuntimeException("Not in update mode");
494 * Stores the data from the UI elements into the model item
495 * Returns true if there were changes made that suggest transaction has to be
496 * checked for being submittable
498 private boolean syncData() {
503 Logger.debug("new-trans", "skipping syncData() loop");
510 switch (item.getType()) {
512 item.setDate(String.valueOf(tvDate.getText()));
513 item.setDescription(String.valueOf(tvDescription.getText()));
516 final LedgerTransactionAccount account = item.getAccount();
517 account.setAccountName(String.valueOf(tvAccount.getText()));
519 item.setComment(String.valueOf(tvComment.getText()));
521 String amount = String.valueOf(tvAmount.getText());
522 amount = amount.trim();
524 if (amount.isEmpty()) {
525 account.resetAmount();
526 // account.setCurrency(null);
530 amount = amount.replace(decimalSeparator, decimalDot);
531 account.setAmount(Float.parseFloat(amount));
533 catch (NumberFormatException e) {
534 Logger.debug("new-trans", String.format(
535 "assuming amount is not set due to number format exception. " +
536 "input was '%s'", amount));
537 account.resetAmount();
539 final String curr = String.valueOf(tvCurrency.getText());
540 if (curr.equals(tvCurrency.getContext()
542 .getString(R.string.currency_symbol)) ||
544 account.setCurrency(null);
546 account.setCurrency(curr);
551 throw new RuntimeException("Should not happen");
560 private void pickTransactionDate() {
561 DatePickerFragment picker = new DatePickerFragment();
562 picker.setFutureDates(mProfile.getFutureDates());
563 picker.setOnDatePickedListener(this);
564 picker.show(((NewTransactionActivity) tvDate.getContext()).getSupportFragmentManager(),
570 * @param item updates the UI elements with the data from the model item
572 @SuppressLint("DefaultLocale")
573 public void setData(NewTransactionModel.Item item) {
576 if (this.item != null && !this.item.equals(item)) {
577 this.item.stopObservingDate(dateObserver);
578 this.item.stopObservingDescription(descriptionObserver);
579 this.item.stopObservingAmountHint(hintObserver);
580 this.item.stopObservingEditableFlag(editableObserver);
582 .stopObservingFocusedItem(focusedAccountObserver);
584 .stopObservingAccountCount(accountCountObserver);
585 Data.currencySymbolPosition.removeObserver(currencyPositionObserver);
586 Data.currencyGap.removeObserver(currencyGapObserver);
587 Data.locale.removeObserver(localeObserver);
588 this.item.stopObservingCurrency(currencyObserver);
589 this.item.getModel().showCurrency.removeObserver(showCurrencyObserver);
590 this.item.stopObservingComment(commentObserver);
591 this.item.getModel().stopObservingBusyFlag(busyFlagObserver);
596 switch (item.getType()) {
598 tvDate.setText(item.getFormattedDate());
599 tvDescription.setText(item.getDescription());
600 lHead.setVisibility(View.VISIBLE);
601 lAccount.setVisibility(View.GONE);
602 lPadding.setVisibility(View.GONE);
606 LedgerTransactionAccount acc = item.getAccount();
607 tvAccount.setText(acc.getAccountName());
608 tvComment.setText(acc.getComment());
609 if (acc.isAmountSet()) {
610 tvAmount.setText(String.format("%1.2f", acc.getAmount()));
613 tvAmount.setText("");
614 // tvAmount.setHint(R.string.zero_amount);
616 tvAmount.setHint(item.getAmountHint());
617 setCurrencyString(acc.getCurrency());
618 lHead.setVisibility(View.GONE);
619 lAccount.setVisibility(View.VISIBLE);
620 lPadding.setVisibility(View.GONE);
624 lHead.setVisibility(View.GONE);
625 lAccount.setVisibility(View.GONE);
626 lPadding.setVisibility(View.VISIBLE);
630 if (this.item == null) { // was null or has changed
632 final NewTransactionActivity activity =
633 (NewTransactionActivity) tvDescription.getContext();
635 if (!item.isOfType(ItemType.bottomFiller)) {
636 item.observeEditableFlag(activity, editableObserver);
638 .observeFocusedItem(activity, focusedAccountObserver);
640 switch (item.getType()) {
642 item.observeDate(activity, dateObserver);
643 item.observeDescription(activity, descriptionObserver);
644 item.getModel().observeBusyFlag(activity, busyFlagObserver);
647 item.observeAmountHint(activity, hintObserver);
648 Data.currencySymbolPosition.observe(activity, currencyPositionObserver);
649 Data.currencyGap.observe(activity, currencyGapObserver);
650 Data.locale.observe(activity, localeObserver);
651 item.observeCurrency(activity, currencyObserver);
652 item.getModel().showCurrency.observe(activity, showCurrencyObserver);
653 item.observeComment(activity, commentObserver);
655 .observeAccountCount(activity, accountCountObserver);
665 public void onDatePicked(int year, int month, int day) {
666 final Calendar c = GregorianCalendar.getInstance();
667 c.set(year, month, day);
668 item.setDate(c.getTime());
669 boolean focused = tvDescription.requestFocus();
671 Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
675 public void descriptionSelected(String description) {
676 tvAccount.setText(description);
677 tvAmount.requestFocus(View.FOCUS_FORWARD);