X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FNewTransactionItemHolder.java;h=05b7258f50566af7e9ec212a7f45c418f394dd23;hp=d7017e21a6816056c18a7fc104eff8639b2ac97a;hb=8616cfa852b90f9fc9cc37c435205f138151978e;hpb=6d8645bc5ffd9ad56124ddc21b2477c687c15ff7 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 d7017e21..05b7258f 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 @@ -18,10 +18,9 @@ package net.ktnx.mobileledger.ui.activity; import android.annotation.SuppressLint; -import android.os.Build; +import android.graphics.Typeface; import android.text.Editable; import android.text.TextWatcher; -import android.text.method.DigitsKeyListener; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; @@ -29,7 +28,6 @@ import android.view.inputmethod.EditorInfo; import android.widget.AutoCompleteTextView; import android.widget.EditText; import android.widget.FrameLayout; -import android.widget.LinearLayout; import android.widget.TextView; import androidx.annotation.NonNull; @@ -46,7 +44,6 @@ import net.ktnx.mobileledger.model.LedgerTransactionAccount; import net.ktnx.mobileledger.model.MobileLedgerProfile; import net.ktnx.mobileledger.ui.CurrencySelectorFragment; import net.ktnx.mobileledger.ui.DatePickerFragment; -import net.ktnx.mobileledger.ui.OnCurrencySelectedListener; import net.ktnx.mobileledger.ui.TextViewClearHelper; import net.ktnx.mobileledger.utils.Colors; import net.ktnx.mobileledger.utils.DimensionUtils; @@ -63,52 +60,58 @@ import java.util.Locale; import static net.ktnx.mobileledger.ui.activity.NewTransactionModel.ItemType; class NewTransactionItemHolder extends RecyclerView.ViewHolder - implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback, - OnCurrencySelectedListener { - private final String decimalSeparator; + implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback { private final String decimalDot; private final TextView tvCurrency; + private final Observer showCommentsObserver; + private final TextView tvTransactionComment; + 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 lHead; private ViewGroup lAccount; private FrameLayout lPadding; private MobileLedgerProfile mProfile; private Date date; private Observer dateObserver; private Observer descriptionObserver; + private Observer transactionCommentObserver; private Observer hintObserver; private Observer focusedAccountObserver; private Observer accountCountObserver; private Observer editableObserver; - private Observer commentVisibleObserver; - private Observer commentObserver; private Observer currencyPositionObserver; private Observer currencyGapObserver; private Observer localeObserver; private Observer currencyObserver; private Observer showCurrencyObserver; + private Observer commentObserver; + private Observer amountValidityObserver; 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); + commentButton = lAccount.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); + 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); @@ -116,18 +119,30 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder tvDate.setOnClickListener(v -> pickTransactionDate()); + commentButton.setOnClickListener(v -> { + tvComment.setVisibility(View.VISIBLE); + tvComment.requestFocus(); + }); + + transactionCommentLayout.findViewById(R.id.comment_button) + .setOnClickListener(v -> { + tvTransactionComment.setVisibility(View.VISIBLE); + tvTransactionComment.requestFocus(); + }); + mProfile = Data.profile.getValue(); if (mProfile == null) throw new AssertionError(); View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> { + final int id = v.getId(); if (hasFocus) { boolean wasSyncing = syncingData; syncingData = true; try { final int pos = getAdapterPosition(); adapter.updateFocusedItem(pos); - switch (v.getId()) { + switch (id) { case R.id.account_row_acc_name: adapter.noteFocusIsOnAccount(pos); break; @@ -137,31 +152,45 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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 { syncingData = wasSyncing; } } + + if (id == R.id.comment) { + commentFocusChanged(commentLayout, tvComment, hasFocus); + } + else if (id == R.id.transaction_comment) { + commentFocusChanged(transactionCommentLayout, tvTransactionComment, hasFocus); + } }; tvDescription.setOnFocusChangeListener(focusMonitor); tvAccount.setOnFocusChangeListener(focusMonitor); tvAmount.setOnFocusChangeListener(focusMonitor); + tvComment.setOnFocusChangeListener(focusMonitor); + tvTransactionComment.setOnFocusChangeListener(focusMonitor); - itemView.findViewById(R.id.comment_button) - .setOnClickListener(v -> { - final int pos = getAdapterPosition(); - adapter.toggleComment(pos); - }); 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() { @@ -183,7 +212,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder syncData(); Logger.debug("textWatcher", "syncData() returned, checking if transaction is submittable"); - adapter.model.checkTransactionSubmittable(adapter); + adapter.checkTransactionSubmittable(); Logger.debug("textWatcher", "done"); } }; @@ -195,49 +224,28 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder start, count, after)); } @Override - public void onTextChanged(CharSequence s, int start, int before, int count) { - Logger.debug("num", - String.format(Locale.US, "onTextChanged: start=%d, before=%d, count=%d", - start, before, count)); - } + 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(); - if (val.isEmpty() || (tvAmount.getSelectionStart() == 0)) - allowed += "-"; - if (!val.contains(decimalSeparator) && !val.contains(decimalDot)) - allowed += decimalSeparator + decimalDot; - - tvAmount.setKeyListener(DigitsKeyListener.getInstance(allowed)); - - syncData(); - adapter.model.checkTransactionSubmittable(adapter); - } + + if (syncData()) + adapter.checkTransactionSubmittable(); } }; tvDescription.addTextChangedListener(tw); + tvTransactionComment.addTextChangedListener(tw); tvAccount.addTextChangedListener(tw); tvComment.addTextChangedListener(tw); tvAmount.addTextChangedListener(amountWatcher); tvCurrency.setOnClickListener(v -> { CurrencySelectorFragment cpf = new CurrencySelectorFragment(); - cpf.setOnCurrencySelectedListener(this); + cpf.showPositionAndPadding(); + cpf.setOnCurrencySelectedListener(c -> item.setCurrency(c)); final AppCompatActivity activity = (AppCompatActivity) v.getContext(); 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; @@ -260,6 +268,15 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder syncingData = false; } }; + transactionCommentObserver = transactionComment -> { + final View focusedView = tvTransactionComment.findFocus(); + tvTransactionComment.setTypeface(null, + (focusedView == tvTransactionComment) ? Typeface.NORMAL : Typeface.ITALIC); + tvTransactionComment.setVisibility(((focusedView != tvTransactionComment) && + Misc.isEmptyOrNull(transactionComment)) + ? View.INVISIBLE : View.VISIBLE); + + }; hintObserver = hint -> { if (syncingData) return; @@ -275,43 +292,48 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder } }; editableObserver = this::setEditable; - commentVisibleObserver = this::setCommentVisible; - commentObserver = this::setComment; 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 -> { @@ -340,31 +362,92 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT); }; - currencyPositionObserver = position -> { - updateCurrencyPositionAndPadding(position, Data.currencyGap.getValue()); + currencyObserver = currency -> { + setCurrency(currency); + adapter.checkTransactionSubmittable(); }; - currencyGapObserver = hasGap -> { - updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(), hasGap); + currencyGapObserver = + hasGap -> updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(), + hasGap); + + currencyPositionObserver = + position -> updateCurrencyPositionAndPadding(position, Data.currencyGap.getValue()); + + showCurrencyObserver = showCurrency -> { + if (showCurrency) { + tvCurrency.setVisibility(View.VISIBLE); + } + else { + tvCurrency.setVisibility(View.GONE); + item.setCurrency(null); + } }; - localeObserver = locale -> { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) - tvAmount.setKeyListener(DigitsKeyListener.getInstance(locale, true, true)); + 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); }; - currencyObserver = this::setCurrency; + 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; - showCurrencyObserver = showCurrency -> { - if (showCurrency) { - tvCurrency.setVisibility(View.VISIBLE); - } + amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.UNSET; + amountLayoutParams.topToBottom = tvAccount.getId(); + + commentLayout.setVisibility(View.VISIBLE); + } else { - tvCurrency.setVisibility(View.GONE); - setCurrencyString(null); - } + 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(View layout, TextView textView, boolean hasFocus) { + int textColor; + if (hasFocus) { + textColor = Colors.defaultTextColor; + textView.setTypeface(null, Typeface.NORMAL); + textView.setHint(R.string.transaction_account_comment_hint); + } + else { + textColor = Colors.defaultTextColorDisabled; + textView.setTypeface(null, Typeface.ITALIC); + textView.setHint(""); + if (Misc.isEmptyOrNull(textView.getText())) { + textView.setVisibility(View.INVISIBLE); + } + } + textView.setTextColor(textColor); + + } private void updateCurrencyPositionAndPadding(Currency.Position position, boolean hasGap) { ConstraintLayout.LayoutParams amountLP = (ConstraintLayout.LayoutParams) tvAmount.getLayoutParams(); @@ -429,33 +512,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder tvAccount.setEnabled(editable); tvAmount.setEnabled(editable); } - private void setCommentVisible(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 comment view and align amounts layout under it - tvComment.setVisibility(View.GONE); - ConstraintLayout.LayoutParams lp = - (ConstraintLayout.LayoutParams) commentButton.getLayoutParams(); - lp.bottomToBottom = R.id.ntr_account; // R.id.parent doesn't work here - - commentButton.setLayoutParams(lp); - } - } - private void setComment(String comment) { - if ((comment != null) && !comment.isEmpty()) - commentButton.setBackgroundResource(R.drawable.ic_comment_black_24dp); - else - commentButton.setBackgroundResource(R.drawable.ic_comment_gray_24dp); - } private void beginUpdates() { if (inUpdate) throw new RuntimeException("Already in update mode"); @@ -470,14 +526,16 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder * syncData() *

* Stores the data from the UI elements into the model item + * Returns true if there were changes made that suggest transaction has to be + * checked for being submittable */ - private void syncData() { + private boolean syncData() { if (item == null) - return; + return false; if (syncingData) { Logger.debug("new-trans", "skipping syncData() loop"); - return; + return false; } syncingData = true; @@ -487,6 +545,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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(); @@ -494,24 +553,25 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder item.setComment(String.valueOf(tvComment.getText())); - // TODO: handle multiple amounts String amount = String.valueOf(tvAmount.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() @@ -527,6 +587,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder case bottomFiller: throw new RuntimeException("Should not happen"); } + + return true; } finally { syncingData = false; @@ -551,10 +613,9 @@ 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.stopObservingCommentVisible(commentVisibleObserver); - this.item.stopObservingComment(commentObserver); this.item.getModel() .stopObservingFocusedItem(focusedAccountObserver); this.item.getModel() @@ -564,6 +625,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder Data.locale.removeObserver(localeObserver); 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; } @@ -572,6 +636,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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); @@ -602,26 +667,37 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder setEditable(false); break; } - if (this.item == null) { // was null or has changed this.item = item; final NewTransactionActivity activity = (NewTransactionActivity) tvDescription.getContext(); - item.observeDate(activity, dateObserver); - item.observeDescription(activity, descriptionObserver); - item.observeAmountHint(activity, hintObserver); - item.observeEditableFlag(activity, editableObserver); - item.observeCommentVisible(activity, commentVisibleObserver); - item.observeComment(activity, commentObserver); - item.getModel() - .observeFocusedItem(activity, focusedAccountObserver); - item.getModel() - .observeAccountCount(activity, accountCountObserver); - Data.currencySymbolPosition.observe(activity, currencyPositionObserver); - Data.currencyGap.observe(activity, currencyGapObserver); - Data.locale.observe(activity, localeObserver); - item.observeCurrency(activity, currencyObserver); - item.getModel().showCurrency.observe(activity, showCurrencyObserver); + + if (!item.isOfType(ItemType.bottomFiller)) { + 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); + Data.currencySymbolPosition.observe(activity, currencyPositionObserver); + Data.currencyGap.observe(activity, currencyGapObserver); + Data.locale.observe(activity, localeObserver); + item.observeCurrency(activity, currencyObserver); + item.getModel().showCurrency.observe(activity, showCurrencyObserver); + item.observeComment(activity, commentObserver); + item.getModel() + .observeAccountCount(activity, accountCountObserver); + item.observeAmountValidity(activity, amountValidityObserver); + break; + } } } finally { @@ -639,10 +715,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder } @Override - public void onCurrencySelected(Currency item) { - this.item.setCurrency(item); - } - @Override public void descriptionSelected(String description) { tvAccount.setText(description); tvAmount.requestFocus(View.FOCUS_FORWARD);