]> git.ktnx.net Git - mobile-ledger.git/commitdiff
rework comments: add clear icon, italics, visible when populated, grayed-out when...
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 1 May 2020 14:50:47 +0000 (17:50 +0300)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Fri, 1 May 2020 14:50:47 +0000 (17:50 +0300)
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
app/src/main/res/layout/new_transaction_row.xml

index 31ddee38ba1cdcddd2bf719e7fa75f64a7d93ba4..ab2f40325bbf9788098598aff998d2dbaded335f 100644 (file)
@@ -18,6 +18,7 @@
 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;
@@ -85,13 +86,12 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private Observer<Integer> focusedAccountObserver;
     private Observer<Integer> accountCountObserver;
     private Observer<Boolean> editableObserver;
-    private Observer<Boolean> commentVisibleObserver;
-    private Observer<String> commentObserver;
     private Observer<Currency.Position> currencyPositionObserver;
     private Observer<Boolean> currencyGapObserver;
     private Observer<Locale> localeObserver;
     private Observer<Currency> currencyObserver;
     private Observer<Boolean> showCurrencyObserver;
+    private Observer<String> commentObserver;
     private boolean inUpdate = false;
     private boolean syncingData = false;
     private View commentButton;
@@ -109,6 +109,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         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);
 
         tvDescription.setNextFocusForwardId(View.NO_ID);
         tvAccount.setNextFocusForwardId(View.NO_ID);
@@ -116,18 +117,25 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
 
         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();
 
         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;
@@ -143,17 +151,25 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     syncingData = wasSyncing;
                 }
             }
+
+            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);
+            }
         };
 
         tvDescription.setOnFocusChangeListener(focusMonitor);
         tvAccount.setOnFocusChangeListener(focusMonitor);
         tvAmount.setOnFocusChangeListener(focusMonitor);
+        tvComment.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,
@@ -273,8 +289,6 @@ 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()) {
@@ -364,6 +378,15 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 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);
+        };
     }
     private void updateCurrencyPositionAndPadding(Currency.Position position, boolean hasGap) {
         ConstraintLayout.LayoutParams amountLP =
@@ -450,12 +473,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
             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");
@@ -556,8 +573,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 this.item.stopObservingDescription(descriptionObserver);
                 this.item.stopObservingAmountHint(hintObserver);
                 this.item.stopObservingEditableFlag(editableObserver);
-                this.item.stopObservingCommentVisible(commentVisibleObserver);
-                this.item.stopObservingComment(commentObserver);
                 this.item.getModel()
                          .stopObservingFocusedItem(focusedAccountObserver);
                 this.item.getModel()
@@ -567,6 +582,7 @@ 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 = null;
             }
@@ -622,13 +638,12 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                         break;
                     case transactionRow:
                         item.observeAmountHint(activity, hintObserver);
-                        item.observeCommentVisible(activity, commentVisibleObserver);
-                        item.observeComment(activity, commentObserver);
                         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);
                         break;
index d20f402f806aafefa9906ec0f61044c1196b76c2..e25b9a10d01b66885af1ec0c2eb90c9631cab82c 100644 (file)
@@ -333,9 +333,6 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItem
     public void noteFocusIsOnComment(int position) {
         model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Comment);
     }
-    public void toggleComment(int position) {
-        model.toggleComment(position);
-    }
     private void holdSubmittableChecks() {
         checkHoldCounter++;
     }
index ed283b211685798f80ea5c64851f4730094ee759..41f2af4034bd4caaed178fff69a8516821dea654 100644 (file)
@@ -171,10 +171,6 @@ public class NewTransactionModel extends ViewModel {
     void swapItems(int one, int two) {
         Collections.swap(items, one - 1, two - 1);
     }
-    void toggleComment(int position) {
-        final MutableLiveData<Boolean> commentVisible = getItem(position).commentVisible;
-        commentVisible.postValue(!commentVisible.getValue());
-    }
     void moveItemLast(int index) {
         /*   0
              1   <-- index
@@ -209,7 +205,6 @@ public class NewTransactionModel extends ViewModel {
         private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
         private FocusedElement focusedElement = FocusedElement.Account;
         private MutableLiveData<String> comment = new MutableLiveData<>(null);
-        private MutableLiveData<Boolean> commentVisible = new MutableLiveData<>(false);
         private MutableLiveData<Currency> currency = new MutableLiveData<>(null);
         private boolean amountHintIsSet = false;
         Item(NewTransactionModel model) {
@@ -407,12 +402,6 @@ public class NewTransactionModel extends ViewModel {
         void stopObservingEditableFlag(Observer<Boolean> observer) {
             editable.removeObserver(observer);
         }
-        void observeCommentVisible(NewTransactionActivity activity, Observer<Boolean> observer) {
-            commentVisible.observe(activity, observer);
-        }
-        void stopObservingCommentVisible(Observer<Boolean> observer) {
-            commentVisible.removeObserver(observer);
-        }
         void observeComment(NewTransactionActivity activity, Observer<String> observer) {
             comment.observe(activity, observer);
         }
index 90944e2c3d5f73260b272645d3d38c20fe0832b0..c40970883330d6fa173cd79e8acb85755ff4fcf7 100644 (file)
             app:layout_constraintStart_toStartOf="parent"
             app:layout_constraintTop_toTopOf="parent" />
 
-        <TextView
-            android:id="@+id/comment_button"
-            android:layout_width="wrap_content"
-            android:layout_height="wrap_content"
-            android:background="@drawable/ic_comment_gray_24dp"
-            app:layout_constraintBottom_toBottomOf="@id/amount_layout"
-            app:layout_constraintStart_toStartOf="@+id/ntr_account"
-            app:layout_constraintTop_toBottomOf="@+id/account_row_acc_name" />
-
-        <EditText
-            android:id="@+id/comment"
+        <androidx.constraintlayout.widget.ConstraintLayout
+            android:id="@+id/comment_layout"
             android:layout_width="0dp"
             android:layout_height="wrap_content"
-            android:hint="@string/transaction_account_comment_hint"
-            android:imeOptions="actionNext"
-            android:inputType="text"
-            android:visibility="gone"
-            app:layout_constraintEnd_toEndOf="@+id/ntr_account"
-            app:layout_constraintStart_toEndOf="@id/comment_button"
-            app:layout_constraintTop_toBottomOf="@id/account_row_acc_name" />
+            android:alpha="0.50"
+            app:layout_constraintEnd_toStartOf="@id/amount_layout"
+            app:layout_constraintStart_toStartOf="parent"
+            app:layout_constraintTop_toBottomOf="@id/account_row_acc_name">
+
+            <TextView
+                android:id="@+id/comment_button"
+                android:layout_width="wrap_content"
+                android:layout_height="wrap_content"
+                android:background="@drawable/ic_comment_gray_24dp"
+                app:layout_constraintBottom_toBottomOf="parent"
+                app:layout_constraintStart_toStartOf="parent"
+                app:layout_constraintTop_toTopOf="parent" />
+
+            <net.ktnx.mobileledger.ui.EditTextWithClear
+                android:id="@+id/comment"
+                android:layout_width="0dp"
+                android:layout_height="wrap_content"
+                android:imeOptions="actionNext"
+                android:inputType="text"
+                android:visibility="invisible"
+                app:layout_constraintEnd_toEndOf="parent"
+                app:layout_constraintStart_toEndOf="@id/comment_button"
+                app:layout_constraintTop_toTopOf="parent" />
+        </androidx.constraintlayout.widget.ConstraintLayout>
 
         <androidx.constraintlayout.widget.ConstraintLayout
             android:id="@+id/amount_layout"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             app:layout_constraintEnd_toEndOf="parent"
-            app:layout_constraintTop_toBottomOf="@id/comment">
+            app:layout_constraintTop_toBottomOf="@id/account_row_acc_name">
 
             <EditText
-                app:layout_constraintTop_toTopOf="parent"
-                app:layout_constraintBottom_toBottomOf="parent"
-                app:layout_constraintLeft_toLeftOf="parent"
-                app:layout_constraintRight_toLeftOf="@id/currency"
                 android:id="@+id/account_row_acc_amounts"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:minWidth="80sp"
                 android:selectAllOnFocus="true"
                 android:textAlignment="viewEnd"
-                app:layout_constraintWidth_min="@dimen/text_margin"/>
+                app:layout_constraintBottom_toBottomOf="parent"
+                app:layout_constraintLeft_toLeftOf="parent"
+                app:layout_constraintRight_toLeftOf="@id/currency"
+                app:layout_constraintTop_toTopOf="parent"
+                app:layout_constraintWidth_min="@dimen/text_margin" />
 
             <TextView
-                app:layout_constraintTop_toTopOf="parent"
-                app:layout_constraintBottom_toBottomOf="parent"
-                app:layout_constraintRight_toRightOf="parent"
                 android:id="@+id/currency"
+                style="@style/TextAppearance.AppCompat.Widget.Button"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:layout_gravity="center_vertical"
+                android:gravity="center_horizontal"
+                android:minWidth="24dp"
                 android:text="@string/currency_symbol"
-                style="@style/TextAppearance.AppCompat.Widget.Button"
                 android:textAllCaps="false"
                 android:visibility="gone"
-                android:minWidth="24dp"
-                android:gravity="center_horizontal"/>
+                app:layout_constraintBottom_toBottomOf="parent"
+                app:layout_constraintRight_toRightOf="parent"
+                app:layout_constraintTop_toTopOf="parent" />
         </androidx.constraintlayout.widget.ConstraintLayout>
 
     </androidx.constraintlayout.widget.ConstraintLayout>