/*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2020 Damyan Ivanov.
* This file is part of MoLe.
* MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
import android.annotation.SuppressLint;
import android.graphics.Typeface;
-import android.os.Build;
import android.text.Editable;
+import android.text.TextUtils;
import android.text.TextWatcher;
-import android.text.method.DigitsKeyListener;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AutoCompleteTextView;
import android.widget.EditText;
import android.widget.FrameLayout;
-import android.widget.LinearLayout;
import android.widget.TextView;
+import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
import net.ktnx.mobileledger.ui.DatePickerFragment;
import net.ktnx.mobileledger.ui.TextViewClearHelper;
-import net.ktnx.mobileledger.utils.Colors;
import net.ktnx.mobileledger.utils.DimensionUtils;
import net.ktnx.mobileledger.utils.Logger;
import net.ktnx.mobileledger.utils.MLDB;
import net.ktnx.mobileledger.utils.Misc;
-
-import org.jetbrains.annotations.NotNull;
+import net.ktnx.mobileledger.utils.SimpleDate;
import java.text.DecimalFormatSymbols;
-import java.util.Calendar;
+import java.text.ParseException;
import java.util.Date;
-import java.util.GregorianCalendar;
import java.util.Locale;
import static net.ktnx.mobileledger.ui.activity.NewTransactionModel.ItemType;
class NewTransactionItemHolder extends RecyclerView.ViewHolder
implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
- private final String decimalSeparator;
private final String decimalDot;
private final TextView tvCurrency;
+ private final Observer<Boolean> showCommentsObserver;
+ private final TextView tvTransactionComment;
+ private final TextView tvDate;
+ private final AutoCompleteTextView tvDescription;
+ private final TextView tvDummy;
+ private final AutoCompleteTextView tvAccount;
+ private final TextView tvComment;
+ private final EditText tvAmount;
+ private final ViewGroup lHead;
+ private final ViewGroup lAccount;
+ private final FrameLayout lPadding;
+ private final MobileLedgerProfile mProfile;
+ private final Observer<SimpleDate> dateObserver;
+ private final Observer<String> descriptionObserver;
+ private final Observer<String> transactionCommentObserver;
+ private final Observer<String> hintObserver;
+ private final Observer<Integer> focusedAccountObserver;
+ private final Observer<Integer> accountCountObserver;
+ private final Observer<Boolean> editableObserver;
+ private final Observer<Currency.Position> currencyPositionObserver;
+ private final Observer<Boolean> currencyGapObserver;
+ private final Observer<Locale> localeObserver;
+ private final Observer<Currency> currencyObserver;
+ private final Observer<Boolean> showCurrencyObserver;
+ private final Observer<String> commentObserver;
+ private final Observer<Boolean> amountValidityObserver;
+ private String decimalSeparator;
private NewTransactionModel.Item item;
- private TextView tvDate;
- private AutoCompleteTextView tvDescription;
- private AutoCompleteTextView tvAccount;
- private TextView tvComment;
- private EditText tvAmount;
- private LinearLayout lHead;
- private ViewGroup lAccount;
- private FrameLayout lPadding;
- private MobileLedgerProfile mProfile;
private Date date;
- private Observer<Date> dateObserver;
- private Observer<String> descriptionObserver;
- private Observer<String> hintObserver;
- private Observer<Integer> focusedAccountObserver;
- private Observer<Integer> accountCountObserver;
- private Observer<Boolean> editableObserver;
- private Observer<Currency.Position> currencyPositionObserver;
- private Observer<Boolean> currencyGapObserver;
- private Observer<Locale> localeObserver;
- private Observer<Currency> currencyObserver;
- private Observer<Boolean> showCurrencyObserver;
- private Observer<String> commentObserver;
private boolean inUpdate = false;
private boolean syncingData = false;
- private View commentButton;
//TODO multiple amounts with different currencies per posting
NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
super(itemView);
- tvAccount = itemView.findViewById(R.id.account_row_acc_name);
- tvComment = itemView.findViewById(R.id.comment);
+ lAccount = itemView.findViewById(R.id.ntr_account);
+ tvAccount = lAccount.findViewById(R.id.account_row_acc_name);
+ tvComment = lAccount.findViewById(R.id.comment);
+ tvTransactionComment = itemView.findViewById(R.id.transaction_comment);
new TextViewClearHelper().attachToTextView((EditText) tvComment);
- commentButton = itemView.findViewById(R.id.comment_button);
tvAmount = itemView.findViewById(R.id.account_row_acc_amounts);
tvCurrency = itemView.findViewById(R.id.currency);
tvDate = itemView.findViewById(R.id.new_transaction_date);
tvDescription = itemView.findViewById(R.id.new_transaction_description);
+ tvDummy = itemView.findViewById(R.id.dummy_text);
lHead = itemView.findViewById(R.id.ntr_data);
- lAccount = itemView.findViewById(R.id.ntr_account);
lPadding = itemView.findViewById(R.id.ntr_padding);
- View commentLayout = itemView.findViewById(R.id.comment_layout);
+ final View commentLayout = itemView.findViewById(R.id.comment_layout);
+ final View transactionCommentLayout =
+ itemView.findViewById(R.id.transaction_comment_layout);
tvDescription.setNextFocusForwardId(View.NO_ID);
tvAccount.setNextFocusForwardId(View.NO_ID);
tvDate.setOnClickListener(v -> pickTransactionDate());
- itemView.findViewById(R.id.comment_button)
+ lAccount.findViewById(R.id.comment_button)
.setOnClickListener(v -> {
tvComment.setVisibility(View.VISIBLE);
tvComment.requestFocus();
});
- mProfile = Data.profile.getValue();
- if (mProfile == null)
- throw new AssertionError();
+ transactionCommentLayout.findViewById(R.id.comment_button)
+ .setOnClickListener(v -> {
+ tvTransactionComment.setVisibility(View.VISIBLE);
+ tvTransactionComment.requestFocus();
+ });
+
+ mProfile = Data.getProfile();
View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
final int id = v.getId();
case R.id.comment:
adapter.noteFocusIsOnComment(pos);
break;
+ case R.id.transaction_comment:
+ adapter.noteFocusIsOnTransactionComment(pos);
+ break;
+ case R.id.new_transaction_description:
+ adapter.noteFocusIsOnDescription(pos);
+ break;
}
}
finally {
}
if (id == R.id.comment) {
- commentLayout.setAlpha(hasFocus ? 1f : 0.5f);
- tvComment.setTypeface(null, hasFocus ? Typeface.NORMAL : Typeface.ITALIC);
- if (hasFocus)
- tvComment.setHint(R.string.transaction_account_comment_hint);
- else
- tvComment.setHint("");
-
- if (!hasFocus && Misc.isEmptyOrNull(tvComment.getText()))
- tvComment.setVisibility(View.INVISIBLE);
+ commentFocusChanged(tvComment, hasFocus);
+ }
+ else if (id == R.id.transaction_comment) {
+ commentFocusChanged(tvTransactionComment, hasFocus);
}
};
tvAccount.setOnFocusChangeListener(focusMonitor);
tvAmount.setOnFocusChangeListener(focusMonitor);
tvComment.setOnFocusChangeListener(focusMonitor);
+ tvTransactionComment.setOnFocusChangeListener(focusMonitor);
MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription,
MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
MLDB.hookAutocompletionAdapter(tvAccount.getContext(), tvAccount, MLDB.ACCOUNTS_TABLE,
"name", true, this, mProfile);
- // FIXME: react on configuration (locale) changes
decimalSeparator = String.valueOf(DecimalFormatSymbols.getInstance()
.getMonetaryDecimalSeparator());
+ localeObserver = locale -> decimalSeparator = String.valueOf(
+ DecimalFormatSymbols.getInstance(locale)
+ .getMonetaryDecimalSeparator());
+
decimalDot = ".";
final TextWatcher tw = new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {}
@Override
public void afterTextChanged(Editable s) {
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
- // only one decimal separator is allowed
- // plus and minus are allowed only at the beginning
- String allowed = "0123456789";
- String val = s.toString();
- Logger.debug("input", val);
- if (val.isEmpty() || (tvAmount.getSelectionStart() == 0))
- allowed += "-";
- if (!val.contains(decimalSeparator) && !val.contains(decimalDot))
- allowed += decimalSeparator + decimalDot;
-
- tvAmount.setKeyListener(DigitsKeyListener.getInstance(allowed));
- }
if (syncData())
adapter.checkTransactionSubmittable();
}
};
tvDescription.addTextChangedListener(tw);
+ tvTransactionComment.addTextChangedListener(tw);
tvAccount.addTextChangedListener(tw);
tvComment.addTextChangedListener(tw);
tvAmount.addTextChangedListener(amountWatcher);
cpf.show(activity.getSupportFragmentManager(), "currency-selector");
});
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
- tvAmount.setKeyListener(
- DigitsKeyListener.getInstance(Data.locale.getValue(), true, true));
- else
- tvAmount.setKeyListener(
- DigitsKeyListener.getInstance("0123456789+-" + decimalSeparator + decimalDot));
-
dateObserver = date -> {
if (syncingData)
return;
syncingData = false;
}
};
+ transactionCommentObserver = transactionComment -> {
+ final View focusedView = tvTransactionComment.findFocus();
+ tvTransactionComment.setTypeface(null,
+ (focusedView == tvTransactionComment) ? Typeface.NORMAL : Typeface.ITALIC);
+ tvTransactionComment.setVisibility(
+ ((focusedView != tvTransactionComment) && TextUtils.isEmpty(transactionComment))
+ ? View.INVISIBLE : View.VISIBLE);
+
+ };
hintObserver = hint -> {
if (syncingData)
return;
}
};
editableObserver = this::setEditable;
+ commentFocusChanged(tvTransactionComment, false);
+ commentFocusChanged(tvComment, false);
focusedAccountObserver = index -> {
- if ((index != null) && index.equals(getAdapterPosition())) {
- switch (item.getType()) {
- case generalData:
- // bad idea - double pop-up, and not really necessary.
- // the user can tap the input to get the calendar
- //if (!tvDate.hasFocus()) tvDate.requestFocus();
- boolean focused = tvDescription.requestFocus();
- tvDescription.dismissDropDown();
- if (focused)
- Misc.showSoftKeyboard(
- (NewTransactionActivity) tvDescription.getContext());
- break;
- case transactionRow:
- // do nothing if a row element already has the focus
- if (!itemView.hasFocus()) {
- switch (item.getFocusedElement()) {
- case Amount:
- tvAmount.requestFocus();
- break;
- case Comment:
- tvComment.requestFocus();
- break;
- case Account:
- focused = tvAccount.requestFocus();
- tvAccount.dismissDropDown();
- if (focused)
- Misc.showSoftKeyboard(
- (NewTransactionActivity) tvAccount.getContext());
- break;
- }
- }
+ if ((index == null) || !index.equals(getAdapterPosition()) || itemView.hasFocus())
+ return;
- break;
- }
+ switch (item.getType()) {
+ case generalData:
+ // bad idea - double pop-up, and not really necessary.
+ // the user can tap the input to get the calendar
+ //if (!tvDate.hasFocus()) tvDate.requestFocus();
+ switch (item.getFocusedElement()) {
+ case TransactionComment:
+ tvTransactionComment.setVisibility(View.VISIBLE);
+ tvTransactionComment.requestFocus();
+ break;
+ case Description:
+ boolean focused = tvDescription.requestFocus();
+ tvDescription.dismissDropDown();
+ if (focused)
+ Misc.showSoftKeyboard(
+ (NewTransactionActivity) tvDescription.getContext());
+ break;
+ }
+ break;
+ case transactionRow:
+ switch (item.getFocusedElement()) {
+ case Amount:
+ tvAmount.requestFocus();
+ break;
+ case Comment:
+ tvComment.setVisibility(View.VISIBLE);
+ tvComment.requestFocus();
+ break;
+ case Account:
+ boolean focused = tvAccount.requestFocus();
+ tvAccount.dismissDropDown();
+ if (focused)
+ Misc.showSoftKeyboard(
+ (NewTransactionActivity) tvAccount.getContext());
+ break;
+ }
+
+ break;
}
};
accountCountObserver = count -> {
tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
};
- localeObserver = locale -> {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
- tvAmount.setKeyListener(DigitsKeyListener.getInstance(locale, true, true));
- };
-
currencyObserver = currency -> {
setCurrency(currency);
adapter.checkTransactionSubmittable();
tvComment.setTypeface(null,
(focusedView == tvComment) ? Typeface.NORMAL : Typeface.ITALIC);
tvComment.setVisibility(
- ((focusedView != tvComment) && Misc.isEmptyOrNull(comment)) ? View.INVISIBLE
- : View.VISIBLE);
+ ((focusedView != tvComment) && TextUtils.isEmpty(comment)) ? View.INVISIBLE
+ : View.VISIBLE);
+ };
+
+ showCommentsObserver = show -> {
+ final View amountLayout = itemView.findViewById(R.id.amount_layout);
+ ConstraintLayout.LayoutParams amountLayoutParams =
+ (ConstraintLayout.LayoutParams) amountLayout.getLayoutParams();
+ ConstraintLayout.LayoutParams accountParams =
+ (ConstraintLayout.LayoutParams) tvAccount.getLayoutParams();
+ if (show) {
+ accountParams.endToStart = ConstraintLayout.LayoutParams.UNSET;
+ accountParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
+
+ amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.UNSET;
+ amountLayoutParams.topToBottom = tvAccount.getId();
+
+ commentLayout.setVisibility(View.VISIBLE);
+ }
+ else {
+ accountParams.endToStart = amountLayout.getId();
+ accountParams.endToEnd = ConstraintLayout.LayoutParams.UNSET;
+
+ amountLayoutParams.topToBottom = ConstraintLayout.LayoutParams.UNSET;
+ amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
+
+ commentLayout.setVisibility(View.GONE);
+ }
+
+ tvAccount.setLayoutParams(accountParams);
+ amountLayout.setLayoutParams(amountLayoutParams);
+
+ transactionCommentLayout.setVisibility(show ? View.VISIBLE : View.GONE);
};
+
+ amountValidityObserver = valid -> {
+ tvAmount.setCompoundDrawablesRelativeWithIntrinsicBounds(
+ valid ? 0 : R.drawable.ic_error_outline_black_24dp, 0, 0, 0);
+ tvAmount.setMinEms(valid ? 4 : 5);
+ };
+ }
+ private void commentFocusChanged(TextView textView, boolean hasFocus) {
+ @ColorInt int textColor;
+ textColor = tvDummy.getTextColors()
+ .getDefaultColor();
+ if (hasFocus) {
+ textView.setTypeface(null, Typeface.NORMAL);
+ textView.setHint(R.string.transaction_account_comment_hint);
+ }
+ else {
+ int alpha = (textColor >> 24 & 0xff);
+ alpha = 3 * alpha / 4;
+ textColor = (alpha << 24) | (0x00ffffff & textColor);
+ textView.setTypeface(null, Typeface.ITALIC);
+ textView.setHint("");
+ if (TextUtils.isEmpty(textView.getText())) {
+ textView.setVisibility(View.INVISIBLE);
+ }
+ }
+ textView.setTextColor(textColor);
+
}
private void updateCurrencyPositionAndPadding(Currency.Position position, boolean hasGap) {
ConstraintLayout.LayoutParams amountLP =
}
}
private void setCurrencyString(String currency) {
+ @ColorInt int textColor = tvDummy.getTextColors()
+ .getDefaultColor();
if ((currency == null) || currency.isEmpty()) {
tvCurrency.setText(R.string.currency_symbol);
- tvCurrency.setTextColor(0x7f000000 + (0x00ffffff & Colors.defaultTextColor));
+ int alpha = (textColor >> 24) & 0xff;
+ alpha = alpha * 3 / 4;
+ tvCurrency.setTextColor((alpha << 24) | (0x00ffffff & textColor));
}
else {
tvCurrency.setText(currency);
- tvCurrency.setTextColor(Colors.defaultTextColor);
+ tvCurrency.setTextColor(textColor);
}
}
private void setCurrency(Currency currency) {
tvAccount.setEnabled(editable);
tvAmount.setEnabled(editable);
}
- private void setCommentVisible(@NotNull Boolean visible) {
- if (visible) {
- // showing; show the comment view and align the comment button to it
- tvComment.setVisibility(View.VISIBLE);
- tvComment.requestFocus();
- ConstraintLayout.LayoutParams lp =
- (ConstraintLayout.LayoutParams) commentButton.getLayoutParams();
- lp.bottomToBottom = R.id.comment;
-
- commentButton.setLayoutParams(lp);
- }
- else {
- // hiding; hide the comment view and align the comment bottom to the amount
- tvComment.setVisibility(View.GONE);
- ConstraintLayout.LayoutParams lp =
- (ConstraintLayout.LayoutParams) commentButton.getLayoutParams();
- lp.bottomToBottom = R.id.amount_layout; // R.id.parent doesn't work here
-
- commentButton.setLayoutParams(lp);
- }
- }
private void beginUpdates() {
if (inUpdate)
throw new RuntimeException("Already in update mode");
case generalData:
item.setDate(String.valueOf(tvDate.getText()));
item.setDescription(String.valueOf(tvDescription.getText()));
+ item.setTransactionComment(String.valueOf(tvTransactionComment.getText()));
break;
case transactionRow:
final LedgerTransactionAccount account = item.getAccount();
if (amount.isEmpty()) {
account.resetAmount();
-// account.setCurrency(null);
+ item.validateAmount();
}
else {
try {
amount = amount.replace(decimalSeparator, decimalDot);
account.setAmount(Float.parseFloat(amount));
+ item.validateAmount();
}
catch (NumberFormatException e) {
Logger.debug("new-trans", String.format(
"assuming amount is not set due to number format exception. " +
"input was '%s'", amount));
- account.resetAmount();
+ account.invalidateAmount();
+ item.invalidateAmount();
}
final String curr = String.valueOf(tvCurrency.getText());
if (curr.equals(tvCurrency.getContext()
return true;
}
+ catch (ParseException e) {
+ throw new RuntimeException("Should not happen", e);
+ }
finally {
syncingData = false;
}
DatePickerFragment picker = new DatePickerFragment();
picker.setFutureDates(mProfile.getFutureDates());
picker.setOnDatePickedListener(this);
+ picker.setCurrentDateFromText(tvDate.getText());
picker.show(((NewTransactionActivity) tvDate.getContext()).getSupportFragmentManager(),
- "datePicker");
+ null);
}
/**
* setData
if (this.item != null && !this.item.equals(item)) {
this.item.stopObservingDate(dateObserver);
this.item.stopObservingDescription(descriptionObserver);
+ this.item.stopObservingTransactionComment(transactionCommentObserver);
this.item.stopObservingAmountHint(hintObserver);
this.item.stopObservingEditableFlag(editableObserver);
this.item.getModel()
this.item.stopObservingCurrency(currencyObserver);
this.item.getModel().showCurrency.removeObserver(showCurrencyObserver);
this.item.stopObservingComment(commentObserver);
+ this.item.getModel().showComments.removeObserver(showCommentsObserver);
+ this.item.stopObservingAmountValidity(amountValidityObserver);
this.item = null;
}
case generalData:
tvDate.setText(item.getFormattedDate());
tvDescription.setText(item.getDescription());
+ tvTransactionComment.setText(item.getTransactionComment());
lHead.setVisibility(View.VISIBLE);
lAccount.setVisibility(View.GONE);
lPadding.setVisibility(View.GONE);
final NewTransactionActivity activity =
(NewTransactionActivity) tvDescription.getContext();
- if (!item.isOfType(ItemType.bottomFiller)) {
+ if (!item.isBottomFiller()) {
item.observeEditableFlag(activity, editableObserver);
item.getModel()
.observeFocusedItem(activity, focusedAccountObserver);
+ item.getModel()
+ .observeShowComments(activity, showCommentsObserver);
}
switch (item.getType()) {
case generalData:
item.observeDate(activity, dateObserver);
item.observeDescription(activity, descriptionObserver);
+ item.observeTransactionComment(activity, transactionCommentObserver);
break;
case transactionRow:
item.observeAmountHint(activity, hintObserver);
item.observeComment(activity, commentObserver);
item.getModel()
.observeAccountCount(activity, accountCountObserver);
+ item.observeAmountValidity(activity, amountValidityObserver);
break;
}
}
}
@Override
public void onDatePicked(int year, int month, int day) {
- final Calendar c = GregorianCalendar.getInstance();
- c.set(year, month, day);
- item.setDate(c.getTime());
+ item.setDate(new SimpleDate(year, month + 1, day));
boolean focused = tvDescription.requestFocus();
if (focused)
Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());