]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
completely abandon fiddling with KeyListener's
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionItemHolder.java
index efb7b52256871d7714a297cdad08b8d92f6f2dcb..05b7258f50566af7e9ec212a7f45c418f394dd23 100644 (file)
@@ -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;
@@ -63,11 +61,11 @@ 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 String decimalSeparator;
     private NewTransactionModel.Item item;
     private TextView tvDate;
     private AutoCompleteTextView tvDescription;
@@ -81,6 +79,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private Date date;
     private Observer<Date> dateObserver;
     private Observer<String> descriptionObserver;
+    private Observer<String> transactionCommentObserver;
     private Observer<String> hintObserver;
     private Observer<Integer> focusedAccountObserver;
     private Observer<Integer> accountCountObserver;
@@ -91,6 +90,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private Observer<Currency> currencyObserver;
     private Observer<Boolean> showCurrencyObserver;
     private Observer<String> commentObserver;
+    private Observer<Boolean> amountValidityObserver;
     private boolean inUpdate = false;
     private boolean syncingData = false;
     private View commentButton;
@@ -125,10 +125,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)
@@ -152,6 +152,12 @@ 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 {
@@ -162,7 +168,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
             if (id == R.id.comment) {
                 commentFocusChanged(commentLayout, tvComment, hasFocus);
             }
-            else if ( id == R.id.transaction_comment) {
+            else if (id == R.id.transaction_comment) {
                 commentFocusChanged(transactionCommentLayout, tvTransactionComment, hasFocus);
             }
         };
@@ -171,15 +177,20 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         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() {
@@ -216,25 +227,13 @@ 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);
+        tvTransactionComment.addTextChangedListener(tw);
         tvAccount.addTextChangedListener(tw);
         tvComment.addTextChangedListener(tw);
         tvAmount.addTextChangedListener(amountWatcher);
@@ -247,13 +246,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;
@@ -276,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;
@@ -292,41 +293,47 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         };
         editableObserver = this::setEditable;
         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.setVisibility(View.VISIBLE);
-                                    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 -> {
@@ -355,11 +362,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();
@@ -421,6 +423,12 @@ 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;
@@ -537,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();
@@ -549,18 +558,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()
@@ -602,6 +613,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()
@@ -615,6 +627,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;
             }
@@ -623,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);
@@ -669,6 +683,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     case generalData:
                         item.observeDate(activity, dateObserver);
                         item.observeDescription(activity, descriptionObserver);
+                        item.observeTransactionComment(activity, transactionCommentObserver);
                         break;
                     case transactionRow:
                         item.observeAmountHint(activity, hintObserver);
@@ -680,6 +695,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                         item.observeComment(activity, commentObserver);
                         item.getModel()
                             .observeAccountCount(activity, accountCountObserver);
+                        item.observeAmountValidity(activity, amountValidityObserver);
                         break;
                 }
             }