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=f283aee53e124f609bd4084b46ada0920743f5fd;hp=84d744b558238bb5f502b0ab6211f7fdc53f6f72;hb=ff27cd8c41424676ec046c58448d23cd4313d1dc;hpb=356b7a4d11096b57996b0aaca854a8ef63d77670 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 84d744b5..f283aee5 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 © 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 @@ -19,10 +19,8 @@ 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.TextWatcher; -import android.text.method.DigitsKeyListener; import android.view.Gravity; import android.view.View; import android.view.ViewGroup; @@ -32,6 +30,7 @@ import android.widget.EditText; import android.widget.FrameLayout; import android.widget.TextView; +import androidx.annotation.ColorInt; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import androidx.constraintlayout.widget.ConstraintLayout; @@ -47,30 +46,30 @@ 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 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 TextView tvTransactionComment; + private String decimalSeparator; private NewTransactionModel.Item item; private TextView tvDate; private AutoCompleteTextView tvDescription; + private TextView tvDummy; private AutoCompleteTextView tvAccount; private TextView tvComment; private EditText tvAmount; @@ -79,7 +78,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder private FrameLayout lPadding; private MobileLedgerProfile mProfile; private Date date; - private Observer dateObserver; + private Observer dateObserver; private Observer descriptionObserver; private Observer transactionCommentObserver; private Observer hintObserver; @@ -92,6 +91,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder private Observer currencyObserver; private Observer showCurrencyObserver; private Observer commentObserver; + private Observer amountValidityObserver; private boolean inUpdate = false; private boolean syncingData = false; private View commentButton; @@ -108,6 +108,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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); lPadding = itemView.findViewById(R.id.ntr_padding); final View commentLayout = itemView.findViewById(R.id.comment_layout); @@ -126,10 +127,10 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder }); transactionCommentLayout.findViewById(R.id.comment_button) - .setOnClickListener(v -> { - tvTransactionComment.setVisibility(View.VISIBLE); - tvTransactionComment.requestFocus(); - }); + .setOnClickListener(v -> { + tvTransactionComment.setVisibility(View.VISIBLE); + tvTransactionComment.requestFocus(); + }); mProfile = Data.profile.getValue(); if (mProfile == null) @@ -167,10 +168,10 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder } if (id == R.id.comment) { - commentFocusChanged(commentLayout, tvComment, hasFocus); + commentFocusChanged(tvComment, hasFocus); } - else if ( id == R.id.transaction_comment) { - commentFocusChanged(transactionCommentLayout, tvTransactionComment, hasFocus); + else if (id == R.id.transaction_comment) { + commentFocusChanged(tvTransactionComment, hasFocus); } }; @@ -185,9 +186,13 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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() { @@ -224,19 +229,6 @@ 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(); @@ -256,13 +248,6 @@ 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; @@ -309,6 +294,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder } }; editableObserver = this::setEditable; + commentFocusChanged(tvTransactionComment, false); + commentFocusChanged(tvComment, false); focusedAccountObserver = index -> { if ((index == null) || !index.equals(getAdapterPosition()) || itemView.hasFocus()) return; @@ -379,11 +366,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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(); @@ -445,16 +427,25 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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; + private void commentFocusChanged(TextView textView, boolean hasFocus) { + @ColorInt int textColor; + textColor = tvDummy.getTextColors() + .getDefaultColor(); if (hasFocus) { - textColor = Colors.defaultTextColor; textView.setTypeface(null, Typeface.NORMAL); textView.setHint(R.string.transaction_account_comment_hint); } else { - textColor = Colors.defaultTextColorDisabled; + int alpha = (textColor >> 24 & 0xff); + alpha = 3 * alpha / 4; + textColor = (alpha << 24) | (0x00ffffff & textColor); textView.setTypeface(null, Typeface.ITALIC); textView.setHint(""); if (Misc.isEmptyOrNull(textView.getText())) { @@ -510,13 +501,17 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder } } 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) { @@ -574,18 +569,20 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder 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() @@ -604,6 +601,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder return true; } + catch (ParseException e) { + throw new RuntimeException("Should not happen", e); + } finally { syncingData = false; } @@ -612,8 +612,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder DatePickerFragment picker = new DatePickerFragment(); picker.setFutureDates(mProfile.getFutureDates()); picker.setOnDatePickedListener(this); + picker.setCurrentDateFromText(tvDate.getText()); picker.show(((NewTransactionActivity) tvDate.getContext()).getSupportFragmentManager(), - "datePicker"); + null); } /** * setData @@ -641,6 +642,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder this.item.getModel().showCurrency.removeObserver(showCurrencyObserver); this.item.stopObservingComment(commentObserver); this.item.getModel().showComments.removeObserver(showCommentsObserver); + this.item.stopObservingAmountValidity(amountValidityObserver); this.item = null; } @@ -708,6 +710,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder item.observeComment(activity, commentObserver); item.getModel() .observeAccountCount(activity, accountCountObserver); + item.observeAmountValidity(activity, amountValidityObserver); break; } } @@ -718,9 +721,7 @@ 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()); + item.setDate(new SimpleDate(year, month+1, day)); boolean focused = tvDescription.requestFocus(); if (focused) Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());