X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FNewTransactionItemHolder.java;h=94d85e769cd3ffd2d6ac16a1dbbde2265a05e917;hb=7ba876d1237f7ceb636035e0d6753195922c8b39;hp=b63ce8e9adeab74c7fcc1a5c814e1983ec3babfd;hpb=cc37874c011bd23ec4fd34c4423a89ceea2d6509;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java index b63ce8e9..94d85e76 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2021 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 @@ -19,20 +19,16 @@ package net.ktnx.mobileledger.ui.activity; 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.view.inputmethod.EditorInfo; -import android.widget.AutoCompleteTextView; import android.widget.EditText; -import android.widget.FrameLayout; -import android.widget.ProgressBar; import android.widget.TextView; +import androidx.annotation.ColorInt; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; @@ -41,6 +37,7 @@ import androidx.recyclerview.widget.RecyclerView; import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.async.DescriptionSelectedCallback; +import net.ktnx.mobileledger.databinding.NewTransactionRowBinding; import net.ktnx.mobileledger.model.Currency; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerTransactionAccount; @@ -48,86 +45,68 @@ import net.ktnx.mobileledger.model.MobileLedgerProfile; 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 showCommentsObserver; + private final MobileLedgerProfile mProfile; + private final Observer dateObserver; + private final Observer descriptionObserver; + private final Observer transactionCommentObserver; + private final Observer hintObserver; + private final Observer focusedAccountObserver; + private final Observer accountCountObserver; + private final Observer editableObserver; + private final Observer currencyPositionObserver; + private final Observer currencyGapObserver; + private final Observer localeObserver; + private final Observer currencyObserver; + private final Observer showCurrencyObserver; + private final Observer commentObserver; + private final Observer amountValidityObserver; + private final NewTransactionRowBinding b; + private String decimalSeparator; private NewTransactionModel.Item item; - private TextView tvDate; - private AutoCompleteTextView tvDescription; - private AutoCompleteTextView tvAccount; - private TextView tvComment; - private EditText tvAmount; - private ViewGroup lHead; - private ViewGroup lAccount; - private FrameLayout lPadding; - private MobileLedgerProfile mProfile; private Date date; - private Observer dateObserver; - private Observer descriptionObserver; - private Observer hintObserver; - private Observer focusedAccountObserver; - private Observer accountCountObserver; - private Observer editableObserver; - private Observer currencyPositionObserver; - private Observer currencyGapObserver; - private Observer localeObserver; - private Observer currencyObserver; - private Observer showCurrencyObserver; - private Observer commentObserver; - private Observer busyFlagObserver; 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); - 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); - 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); - ProgressBar p = itemView.findViewById(R.id.progressBar); - - tvDescription.setNextFocusForwardId(View.NO_ID); - tvAccount.setNextFocusForwardId(View.NO_ID); - tvAmount.setNextFocusForwardId(View.NO_ID); // magic! - - tvDate.setOnClickListener(v -> pickTransactionDate()); - - itemView.findViewById(R.id.comment_button) - .setOnClickListener(v -> { - tvComment.setVisibility(View.VISIBLE); - tvComment.requestFocus(); - }); - - mProfile = Data.profile.getValue(); - if (mProfile == null) - throw new AssertionError(); + NewTransactionItemHolder(@NonNull NewTransactionRowBinding b, + NewTransactionItemsAdapter adapter) { + super(b.getRoot()); + this.b = b; + new TextViewClearHelper().attachToTextView((EditText) b.comment); + + b.newTransactionDescription.setNextFocusForwardId(View.NO_ID); + b.accountRowAccName.setNextFocusForwardId(View.NO_ID); + b.accountRowAccAmounts.setNextFocusForwardId(View.NO_ID); // magic! + + b.newTransactionDate.setOnClickListener(v -> pickTransactionDate()); + + b.accountCommentButton.setOnClickListener(v -> { + b.comment.setVisibility(View.VISIBLE); + b.comment.requestFocus(); + }); + + b.transactionCommentButton.setOnClickListener(v -> { + b.transactionComment.setVisibility(View.VISIBLE); + b.transactionComment.requestFocus(); + }); + + mProfile = Data.getProfile(); View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> { final int id = v.getId(); @@ -137,16 +116,20 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder try { final int pos = getAdapterPosition(); adapter.updateFocusedItem(pos); - switch (id) { - case R.id.account_row_acc_name: - adapter.noteFocusIsOnAccount(pos); - break; - case R.id.account_row_acc_amounts: - adapter.noteFocusIsOnAmount(pos); - break; - case R.id.comment: - adapter.noteFocusIsOnComment(pos); - break; + if (id == R.id.account_row_acc_name) { + adapter.noteFocusIsOnAccount(pos); + } + else if (id == R.id.account_row_acc_amounts) { + adapter.noteFocusIsOnAmount(pos); + } + else if (id == R.id.comment) { + adapter.noteFocusIsOnComment(pos); + } + else if (id == R.id.transaction_comment) { + adapter.noteFocusIsOnTransactionComment(pos); + } + else if (id == R.id.new_transaction_description) { + adapter.noteFocusIsOnDescription(pos); } } finally { @@ -155,31 +138,32 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder } 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(b.comment, hasFocus); + } + else if (id == R.id.transaction_comment) { + commentFocusChanged(b.transactionComment, hasFocus); } }; - tvDescription.setOnFocusChangeListener(focusMonitor); - tvAccount.setOnFocusChangeListener(focusMonitor); - tvAmount.setOnFocusChangeListener(focusMonitor); - tvComment.setOnFocusChangeListener(focusMonitor); + b.newTransactionDescription.setOnFocusChangeListener(focusMonitor); + b.accountRowAccName.setOnFocusChangeListener(focusMonitor); + b.accountRowAccAmounts.setOnFocusChangeListener(focusMonitor); + b.comment.setOnFocusChangeListener(focusMonitor); + b.transactionComment.setOnFocusChangeListener(focusMonitor); - MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription, + MLDB.hookAutocompletionAdapter(b.getRoot() + .getContext(), b.newTransactionDescription, MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile); - MLDB.hookAutocompletionAdapter(tvAccount.getContext(), tvAccount, MLDB.ACCOUNTS_TABLE, + MLDB.hookAutocompletionAdapter(b.getRoot() + .getContext(), b.accountRowAccName, 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() { @@ -216,30 +200,18 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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); - tvAccount.addTextChangedListener(tw); - tvComment.addTextChangedListener(tw); - tvAmount.addTextChangedListener(amountWatcher); + b.newTransactionDescription.addTextChangedListener(tw); + b.transactionComment.addTextChangedListener(tw); + b.accountRowAccName.addTextChangedListener(tw); + b.comment.addTextChangedListener(tw); + b.accountRowAccAmounts.addTextChangedListener(amountWatcher); - tvCurrency.setOnClickListener(v -> { + b.currencyButton.setOnClickListener(v -> { CurrencySelectorFragment cpf = new CurrencySelectorFragment(); cpf.showPositionAndPadding(); cpf.setOnCurrencySelectedListener(c -> item.setCurrency(c)); @@ -247,19 +219,12 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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 = true; try { - tvDate.setText(item.getFormattedDate()); + b.newTransactionDate.setText(item.getFormattedDate()); } finally { syncingData = false; @@ -270,62 +235,80 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder return; syncingData = true; try { - tvDescription.setText(description); + b.newTransactionDescription.setText(description); } finally { syncingData = false; } }; + transactionCommentObserver = transactionComment -> { + final View focusedView = b.transactionComment.findFocus(); + b.transactionComment.setTypeface(null, + (focusedView == b.transactionComment) ? Typeface.NORMAL : Typeface.ITALIC); + b.transactionComment.setVisibility( + ((focusedView != b.transactionComment) && TextUtils.isEmpty(transactionComment)) + ? View.INVISIBLE : View.VISIBLE); + + }; hintObserver = hint -> { if (syncingData) return; syncingData = true; try { if (hint == null) - tvAmount.setHint(R.string.zero_amount); + b.accountRowAccAmounts.setHint(R.string.zero_amount); else - tvAmount.setHint(hint); + b.accountRowAccAmounts.setHint(hint); } finally { syncingData = false; } }; editableObserver = this::setEditable; + commentFocusChanged(b.transactionComment, false); + commentFocusChanged(b.comment, 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: + b.transactionComment.setVisibility(View.VISIBLE); + b.transactionComment.requestFocus(); + break; + case Description: + boolean focused = b.newTransactionDescription.requestFocus(); +// tvDescription.dismissDropDown(); + if (focused) + Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot() + .getContext()); + break; + } + break; + case transactionRow: + switch (item.getFocusedElement()) { + case Amount: + b.accountRowAccAmounts.requestFocus(); + break; + case Comment: + b.comment.setVisibility(View.VISIBLE); + b.comment.requestFocus(); + break; + case Account: + boolean focused = b.accountRowAccName.requestFocus(); + b.accountRowAccName.dismissDropDown(); + if (focused) + Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot() + .getContext()); + break; + } + + break; } }; accountCountObserver = count -> { @@ -349,14 +332,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder .getAmount()) : "unset") : ""))); if (adapterPosition == count) - tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE); + b.accountRowAccAmounts.setImeOptions(EditorInfo.IME_ACTION_DONE); else - tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT); - }; - - localeObserver = locale -> { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - tvAmount.setKeyListener(DigitsKeyListener.getInstance(locale, true, true)); + b.accountRowAccAmounts.setImeOptions(EditorInfo.IME_ACTION_NEXT); }; currencyObserver = currency -> { @@ -373,30 +351,90 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder showCurrencyObserver = showCurrency -> { if (showCurrency) { - tvCurrency.setVisibility(View.VISIBLE); + b.currency.setVisibility(View.VISIBLE); + b.currencyButton.setVisibility(View.VISIBLE); + String defaultCommodity = mProfile.getDefaultCommodity(); + item.setCurrency( + (defaultCommodity == null) ? null : Currency.loadByName(defaultCommodity)); } else { - tvCurrency.setVisibility(View.GONE); + b.currency.setVisibility(View.GONE); + b.currencyButton.setVisibility(View.GONE); item.setCurrency(null); } }; commentObserver = comment -> { - final View focusedView = tvComment.findFocus(); - tvComment.setTypeface(null, - (focusedView == tvComment) ? Typeface.NORMAL : Typeface.ITALIC); - tvComment.setVisibility( - ((focusedView != tvComment) && Misc.isEmptyOrNull(comment)) ? View.INVISIBLE - : View.VISIBLE); + final View focusedView = b.comment.findFocus(); + b.comment.setTypeface(null, + (focusedView == b.comment) ? Typeface.NORMAL : Typeface.ITALIC); + b.comment.setVisibility( + ((focusedView != b.comment) && TextUtils.isEmpty(comment)) ? View.INVISIBLE + : View.VISIBLE); }; - busyFlagObserver = isBusy -> {p.setVisibility(isBusy ? View.VISIBLE : View.INVISIBLE);}; + showCommentsObserver = show -> { + ConstraintLayout.LayoutParams amountLayoutParams = + (ConstraintLayout.LayoutParams) b.amountLayout.getLayoutParams(); + ConstraintLayout.LayoutParams accountParams = + (ConstraintLayout.LayoutParams) b.accountRowAccName.getLayoutParams(); + if (show) { + accountParams.endToStart = ConstraintLayout.LayoutParams.UNSET; + accountParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID; + + amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.UNSET; + amountLayoutParams.topToBottom = b.accountRowAccName.getId(); + + b.commentLayout.setVisibility(View.VISIBLE); + } + else { + accountParams.endToStart = b.amountLayout.getId(); + accountParams.endToEnd = ConstraintLayout.LayoutParams.UNSET; + + amountLayoutParams.topToBottom = ConstraintLayout.LayoutParams.UNSET; + amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID; + + b.commentLayout.setVisibility(View.GONE); + } + + b.accountRowAccName.setLayoutParams(accountParams); + b.amountLayout.setLayoutParams(amountLayoutParams); + + b.transactionCommentLayout.setVisibility(show ? View.VISIBLE : View.GONE); + }; + + amountValidityObserver = valid -> { + b.accountRowAccAmounts.setCompoundDrawablesRelativeWithIntrinsicBounds( + valid ? 0 : R.drawable.ic_error_outline_black_24dp, 0, 0, 0); + b.accountRowAccAmounts.setMinEms(valid ? 4 : 5); + }; + } + private void commentFocusChanged(TextView textView, boolean hasFocus) { + @ColorInt int textColor; + textColor = b.dummyText.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 = - (ConstraintLayout.LayoutParams) tvAmount.getLayoutParams(); + (ConstraintLayout.LayoutParams) b.accountRowAccAmounts.getLayoutParams(); ConstraintLayout.LayoutParams currencyLP = - (ConstraintLayout.LayoutParams) tvCurrency.getLayoutParams(); + (ConstraintLayout.LayoutParams) b.currency.getLayoutParams(); if (position == Currency.Position.before) { currencyLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID; @@ -405,9 +443,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder amountLP.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID; amountLP.endToStart = ConstraintLayout.LayoutParams.UNSET; amountLP.startToStart = ConstraintLayout.LayoutParams.UNSET; - amountLP.startToEnd = tvCurrency.getId(); + amountLP.startToEnd = b.currency.getId(); - tvCurrency.setGravity(Gravity.END); + b.currency.setGravity(Gravity.END); } else { currencyLP.startToStart = ConstraintLayout.LayoutParams.UNSET; @@ -416,66 +454,49 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder amountLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID; amountLP.startToEnd = ConstraintLayout.LayoutParams.UNSET; amountLP.endToEnd = ConstraintLayout.LayoutParams.UNSET; - amountLP.endToStart = tvCurrency.getId(); + amountLP.endToStart = b.currency.getId(); - tvCurrency.setGravity(Gravity.START); + b.currency.setGravity(Gravity.START); } - amountLP.resolveLayoutDirection(tvAmount.getLayoutDirection()); - currencyLP.resolveLayoutDirection(tvCurrency.getLayoutDirection()); + amountLP.resolveLayoutDirection(b.accountRowAccAmounts.getLayoutDirection()); + currencyLP.resolveLayoutDirection(b.currency.getLayoutDirection()); - tvAmount.setLayoutParams(amountLP); - tvCurrency.setLayoutParams(currencyLP); + b.accountRowAccAmounts.setLayoutParams(amountLP); + b.currency.setLayoutParams(currencyLP); // distance between the amount and the currency symbol - int gapSize = DimensionUtils.sp2px(tvCurrency.getContext(), 5); + int gapSize = DimensionUtils.sp2px(b.currency.getContext(), 5); if (position == Currency.Position.before) { - tvCurrency.setPaddingRelative(0, 0, hasGap ? gapSize : 0, 0); + b.currency.setPaddingRelative(0, 0, hasGap ? gapSize : 0, 0); } else { - tvCurrency.setPaddingRelative(hasGap ? gapSize : 0, 0, 0, 0); + b.currency.setPaddingRelative(hasGap ? gapSize : 0, 0, 0, 0); } } private void setCurrencyString(String currency) { + @ColorInt int textColor = b.dummyText.getTextColors() + .getDefaultColor(); if ((currency == null) || currency.isEmpty()) { - tvCurrency.setText(R.string.currency_symbol); - tvCurrency.setTextColor(0x7f000000 + (0x00ffffff & Colors.defaultTextColor)); + b.currency.setText(R.string.currency_symbol); + int alpha = (textColor >> 24) & 0xff; + alpha = alpha * 3 / 4; + b.currency.setTextColor((alpha << 24) | (0x00ffffff & textColor)); } else { - tvCurrency.setText(currency); - tvCurrency.setTextColor(Colors.defaultTextColor); + b.currency.setText(currency); + b.currency.setTextColor(textColor); } } private void setCurrency(Currency currency) { setCurrencyString((currency == null) ? null : currency.getName()); } private void setEditable(Boolean editable) { - tvDate.setEnabled(editable); - tvDescription.setEnabled(editable); - 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); - } + b.newTransactionDate.setEnabled(editable); + b.newTransactionDescription.setEnabled(editable); + b.accountRowAccName.setEnabled(editable); + b.accountRowAccAmounts.setEnabled(editable); } private void beginUpdates() { if (inUpdate) @@ -508,35 +529,38 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder try { switch (item.getType()) { case generalData: - item.setDate(String.valueOf(tvDate.getText())); - item.setDescription(String.valueOf(tvDescription.getText())); + item.setDate(String.valueOf(b.newTransactionDate.getText())); + item.setDescription(String.valueOf(b.newTransactionDescription.getText())); + item.setTransactionComment(String.valueOf(b.transactionComment.getText())); break; case transactionRow: final LedgerTransactionAccount account = item.getAccount(); - account.setAccountName(String.valueOf(tvAccount.getText())); + account.setAccountName(String.valueOf(b.accountRowAccName.getText())); - item.setComment(String.valueOf(tvComment.getText())); + item.setComment(String.valueOf(b.comment.getText())); - String amount = String.valueOf(tvAmount.getText()); + String amount = String.valueOf(b.accountRowAccAmounts.getText()); amount = amount.trim(); 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() + final String curr = String.valueOf(b.currency.getText()); + if (curr.equals(b.currency.getContext() .getResources() .getString(R.string.currency_symbol)) || curr.isEmpty()) @@ -552,6 +576,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder return true; } + catch (ParseException e) { + throw new RuntimeException("Should not happen", e); + } finally { syncingData = false; } @@ -560,8 +587,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder DatePickerFragment picker = new DatePickerFragment(); picker.setFutureDates(mProfile.getFutureDates()); picker.setOnDatePickedListener(this); - picker.show(((NewTransactionActivity) tvDate.getContext()).getSupportFragmentManager(), - "datePicker"); + picker.setCurrentDateFromText(b.newTransactionDate.getText()); + picker.show(((NewTransactionActivity) b.getRoot() + .getContext()).getSupportFragmentManager(), null); } /** * setData @@ -575,6 +603,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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() @@ -587,60 +616,64 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder this.item.stopObservingCurrency(currencyObserver); this.item.getModel().showCurrency.removeObserver(showCurrencyObserver); this.item.stopObservingComment(commentObserver); - this.item.getModel().stopObservingBusyFlag(busyFlagObserver); + this.item.getModel().showComments.removeObserver(showCommentsObserver); + this.item.stopObservingAmountValidity(amountValidityObserver); this.item = null; } switch (item.getType()) { case generalData: - tvDate.setText(item.getFormattedDate()); - tvDescription.setText(item.getDescription()); - lHead.setVisibility(View.VISIBLE); - lAccount.setVisibility(View.GONE); - lPadding.setVisibility(View.GONE); + b.newTransactionDate.setText(item.getFormattedDate()); + b.newTransactionDescription.setText(item.getDescription()); + b.transactionComment.setText(item.getTransactionComment()); + b.ntrData.setVisibility(View.VISIBLE); + b.ntrAccount.setVisibility(View.GONE); + b.ntrPadding.setVisibility(View.GONE); setEditable(true); break; case transactionRow: LedgerTransactionAccount acc = item.getAccount(); - tvAccount.setText(acc.getAccountName()); - tvComment.setText(acc.getComment()); + b.accountRowAccName.setText(acc.getAccountName()); + b.comment.setText(acc.getComment()); if (acc.isAmountSet()) { - tvAmount.setText(String.format("%1.2f", acc.getAmount())); + b.accountRowAccAmounts.setText(String.format("%1.2f", acc.getAmount())); } else { - tvAmount.setText(""); + b.accountRowAccAmounts.setText(""); // tvAmount.setHint(R.string.zero_amount); } - tvAmount.setHint(item.getAmountHint()); + b.accountRowAccAmounts.setHint(item.getAmountHint()); setCurrencyString(acc.getCurrency()); - lHead.setVisibility(View.GONE); - lAccount.setVisibility(View.VISIBLE); - lPadding.setVisibility(View.GONE); + b.ntrData.setVisibility(View.GONE); + b.ntrAccount.setVisibility(View.VISIBLE); + b.ntrPadding.setVisibility(View.GONE); setEditable(true); break; case bottomFiller: - lHead.setVisibility(View.GONE); - lAccount.setVisibility(View.GONE); - lPadding.setVisibility(View.VISIBLE); + b.ntrData.setVisibility(View.GONE); + b.ntrAccount.setVisibility(View.GONE); + b.ntrPadding.setVisibility(View.VISIBLE); setEditable(false); break; } if (this.item == null) { // was null or has changed this.item = item; - final NewTransactionActivity activity = - (NewTransactionActivity) tvDescription.getContext(); + final NewTransactionActivity activity = (NewTransactionActivity) b.getRoot() + .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.getModel().observeBusyFlag(activity, busyFlagObserver); + item.observeTransactionComment(activity, transactionCommentObserver); break; case transactionRow: item.observeAmountHint(activity, hintObserver); @@ -652,6 +685,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder item.observeComment(activity, commentObserver); item.getModel() .observeAccountCount(activity, accountCountObserver); + item.observeAmountValidity(activity, amountValidityObserver); break; } } @@ -662,17 +696,16 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder } @Override public void onDatePicked(int year, int month, int day) { - final Calendar c = GregorianCalendar.getInstance(); - c.set(year, month, day); - item.setDate(c.getTime()); - boolean focused = tvDescription.requestFocus(); + item.setDate(new SimpleDate(year, month + 1, day)); + boolean focused = b.newTransactionDescription.requestFocus(); if (focused) - Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext()); + Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot() + .getContext()); } @Override public void descriptionSelected(String description) { - tvAccount.setText(description); - tvAmount.requestFocus(View.FOCUS_FORWARD); + b.accountRowAccName.setText(description); + b.accountRowAccAmounts.requestFocus(View.FOCUS_FORWARD); } }