]> git.ktnx.net Git - mobile-ledger.git/commitdiff
handle transaction comment UI changes
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Wed, 13 May 2020 18:39:36 +0000 (21:39 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Thu, 14 May 2020 17:39:53 +0000 (17:39 +0000)
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java

index efb7b52256871d7714a297cdad08b8d92f6f2dcb..84d744b558238bb5f502b0ab6211f7fdc53f6f72 100644 (file)
@@ -81,6 +81,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;
@@ -152,6 +153,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 {
@@ -171,6 +178,7 @@ 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);
@@ -235,6 +243,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
             }
         };
         tvDescription.addTextChangedListener(tw);
+        tvTransactionComment.addTextChangedListener(tw);
         tvAccount.addTextChangedListener(tw);
         tvComment.addTextChangedListener(tw);
         tvAmount.addTextChangedListener(amountWatcher);
@@ -276,6 +285,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 +310,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 -> {
@@ -537,6 +561,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();
@@ -602,6 +627,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()
@@ -623,6 +649,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 +696,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);
index df58e539a74b2c5ddb369827b3c3a55a2144d2f6..5caa38a5581ae19d3b4fb403d5125a90e741ce0d 100644 (file)
@@ -371,6 +371,12 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
     void noteFocusIsOnComment(int position) {
         model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Comment);
     }
+    void noteFocusIsOnTransactionComment(int position) {
+        model.noteFocusChanged(position, NewTransactionModel.FocusedElement.TransactionComment);
+    }
+    public void noteFocusIsOnDescription(int pos) {
+        model.noteFocusChanged(pos, NewTransactionModel.FocusedElement.Description);
+    }
     private void holdSubmittableChecks() {
         checkHoldCounter++;
     }
index d7663bcd7ff272f13b9a070ec20136c63478a0f1..7fd177683f461db1be543200007585a74dec4efb 100644 (file)
@@ -93,12 +93,16 @@ public class NewTransactionModel extends ViewModel {
     public String getDescription() {
         return header.description.getValue();
     }
+    public String getComment() {
+        return header.comment.getValue();
+    }
     LiveData<Boolean> isSubmittable() {
         return this.isSubmittable;
     }
     void reset() {
         header.date.setValue(null);
         header.description.setValue(null);
+        header.comment.setValue(null);
         items.clear();
         items.add(new Item(this, new LedgerTransactionAccount("")));
         items.add(new Item(this, new LedgerTransactionAccount("")));
@@ -216,7 +220,7 @@ public class NewTransactionModel extends ViewModel {
     }
     enum ItemType {generalData, transactionRow, bottomFiller}
 
-    enum FocusedElement {Account, Comment, Amount}
+    enum FocusedElement {Account, Comment, Amount, Description, TransactionComment}
 
 
     //==========================================================================================
@@ -386,6 +390,23 @@ public class NewTransactionModel extends ViewModel {
                 @NonNull androidx.lifecycle.Observer<? super String> observer) {
             this.description.removeObserver(observer);
         }
+        public String getTransactionComment() {
+            ensureType(ItemType.generalData);
+            return comment.getValue();
+        }
+        public void setTransactionComment(String transactionComment) {
+            ensureType(ItemType.generalData);
+            this.comment.setValue(transactionComment);
+        }
+        void observeTransactionComment(@NonNull @NotNull LifecycleOwner owner,
+                                       @NonNull Observer<? super String> observer) {
+            ensureType(ItemType.generalData);
+            this.comment.observe(owner, observer);
+        }
+        void stopObservingTransactionComment(@NonNull Observer<? super String> observer) {
+            ensureType(ItemType.generalData);
+            this.comment.removeObserver(observer);
+        }
         public LedgerTransactionAccount getAccount() {
             ensureType(ItemType.transactionRow);
             return account;