]> git.ktnx.net Git - mobile-ledger.git/commitdiff
transaction-level comments in new transaction UI, optional
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 10 May 2020 18:47:31 +0000 (18:47 +0000)
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/NewTransactionFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
app/src/main/res/layout/new_transaction_row.xml
app/src/main/res/menu/new_transaction_fragment.xml
app/src/main/res/values-bg/strings.xml
app/src/main/res/values/strings.xml

index 1074bcb98f6b30a660f13a3c309eaf4e8e182bb2..39f53a97eb2f9a0cc074d488184442b0a27d4139 100644 (file)
@@ -75,20 +75,30 @@ public class NewTransactionFragment extends Fragment {
     @Override
     public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflater) {
         super.onCreateOptionsMenu(menu, inflater);
+        final FragmentActivity activity = getActivity();
+
         inflater.inflate(R.menu.new_transaction_fragment, menu);
         menu.findItem(R.id.action_reset_new_transaction_activity)
             .setOnMenuItemClickListener(item -> {
                 listAdapter.reset();
                 return true;
             });
+
         final MenuItem toggleCurrencyItem = menu.findItem(R.id.toggle_currency);
         toggleCurrencyItem.setOnMenuItemClickListener(item -> {
             viewModel.toggleCurrencyVisible();
             return true;
         });
-        final FragmentActivity activity = getActivity();
         if (activity != null)
             viewModel.showCurrency.observe(activity, toggleCurrencyItem::setChecked);
+
+        final MenuItem toggleCommentsItem = menu.findItem(R.id.toggle_comments);
+        toggleCommentsItem.setOnMenuItemClickListener(item -> {
+            viewModel.toggleShowComments();
+            return true;
+        });
+        if (activity != null)
+            viewModel.showComments.observe(activity, toggleCommentsItem::setChecked);
     }
     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
index 63de53709c8e11471e4bcf0ddaa33e4d61b32ea3..d92a4c69d542aaef4496ac002d4de7b6a80aed9e 100644 (file)
@@ -53,8 +53,6 @@ import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.MLDB;
 import net.ktnx.mobileledger.utils.Misc;
 
-import org.jetbrains.annotations.NotNull;
-
 import java.text.DecimalFormatSymbols;
 import java.util.Calendar;
 import java.util.Date;
@@ -68,6 +66,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private final String decimalSeparator;
     private final String decimalDot;
     private final TextView tvCurrency;
+    private final Observer<Boolean> showCommentsObserver;
+    private final TextView tvTransactionComment;
     private NewTransactionModel.Item item;
     private TextView tvDate;
     private AutoCompleteTextView tvDescription;
@@ -99,6 +99,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         super(itemView);
         tvAccount = itemView.findViewById(R.id.account_row_acc_name);
         tvComment = itemView.findViewById(R.id.comment);
+        tvTransactionComment = itemView.findViewById(R.id.transaction_comment);
         new TextViewClearHelper().attachToTextView((EditText) tvComment);
         commentButton = itemView.findViewById(R.id.comment_button);
         tvAmount = itemView.findViewById(R.id.account_row_acc_amounts);
@@ -108,7 +109,9 @@ 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);
+        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,10 +119,15 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
 
         tvDate.setOnClickListener(v -> pickTransactionDate());
 
-        itemView.findViewById(R.id.comment_button)
+        commentButton.setOnClickListener(v -> {
+            tvComment.setVisibility(View.VISIBLE);
+            tvComment.requestFocus();
+        });
+
+        itemView.findViewById(R.id.transaction_comment_button)
                 .setOnClickListener(v -> {
-                    tvComment.setVisibility(View.VISIBLE);
-                    tvComment.requestFocus();
+                    tvTransactionComment.setVisibility(View.VISIBLE);
+                    tvTransactionComment.requestFocus();
                 });
 
         mProfile = Data.profile.getValue();
@@ -152,15 +160,10 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
             }
 
             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);
+                commentFocusChanged(commentLayout, tvComment, hasFocus);
+            }
+            else if ( id == R.id.transaction_comment) {
+                commentFocusChanged(transactionCommentLayout, tvTransactionComment, hasFocus);
             }
         };
 
@@ -387,6 +390,49 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     ((focusedView != tvComment) && Misc.isEmptyOrNull(comment)) ? View.INVISIBLE
                                                                                 : View.VISIBLE);
         };
+
+        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) {
+                commentLayout.setVisibility(View.VISIBLE);
+
+                accountParams.endToStart = ConstraintLayout.LayoutParams.UNSET;
+                accountParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
+
+                amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.UNSET;
+                amountLayoutParams.topToBottom = tvAccount.getId();
+            }
+            else {
+                commentLayout.setVisibility(View.GONE);
+
+                accountParams.endToStart = amountLayout.getId();
+                accountParams.endToEnd = ConstraintLayout.LayoutParams.UNSET;
+
+                amountLayoutParams.topToBottom = ConstraintLayout.LayoutParams.UNSET;
+                amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
+
+            }
+
+            tvAccount.setLayoutParams(accountParams);
+            amountLayout.setLayoutParams(amountLayoutParams);
+
+            transactionCommentLayout.setVisibility(show ? View.VISIBLE : View.GONE);
+        };
+    }
+    private void commentFocusChanged(View layout, TextView textView, boolean hasFocus) {
+        layout.setAlpha(hasFocus ? 1f : 0.5f);
+        textView.setTypeface(null, hasFocus ? Typeface.NORMAL : Typeface.ITALIC);
+        if (hasFocus)
+            textView.setHint(R.string.transaction_account_comment_hint);
+        else
+            textView.setHint("");
+
+        if (!hasFocus && Misc.isEmptyOrNull(textView.getText()))
+            textView.setVisibility(View.INVISIBLE);
     }
     private void updateCurrencyPositionAndPadding(Currency.Position position, boolean hasGap) {
         ConstraintLayout.LayoutParams amountLP =
@@ -452,27 +498,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         tvAccount.setEnabled(editable);
         tvAmount.setEnabled(editable);
     }
-    private void setCommentVisible(@NotNull 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 view and align the comment bottom to the amount
-            tvComment.setVisibility(View.GONE);
-            ConstraintLayout.LayoutParams lp =
-                    (ConstraintLayout.LayoutParams) commentButton.getLayoutParams();
-            lp.bottomToBottom = R.id.amount_layout;   // R.id.parent doesn't work here
-
-            commentButton.setLayoutParams(lp);
-        }
-    }
     private void beginUpdates() {
         if (inUpdate)
             throw new RuntimeException("Already in update mode");
@@ -583,6 +608,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 this.item.stopObservingCurrency(currencyObserver);
                 this.item.getModel().showCurrency.removeObserver(showCurrencyObserver);
                 this.item.stopObservingComment(commentObserver);
+                this.item.getModel().showComments.removeObserver(showCommentsObserver);
 
                 this.item = null;
             }
@@ -630,6 +656,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     item.observeEditableFlag(activity, editableObserver);
                     item.getModel()
                         .observeFocusedItem(activity, focusedAccountObserver);
+                    item.getModel()
+                        .observeShowComments(activity, showCommentsObserver);
                 }
                 switch (item.getType()) {
                     case generalData:
index 062495c1537a0a169fe7febc7015a44292b6529b..d7663bcd7ff272f13b9a070ec20136c63478a0f1 100644 (file)
@@ -59,6 +59,10 @@ public class NewTransactionModel extends ViewModel {
             profile -> showCurrency.postValue(profile.getShowCommodityByDefault());
     private final AtomicInteger busyCounter = new AtomicInteger(0);
     private final MutableLiveData<Boolean> busyFlag = new MutableLiveData<>(false);
+    final MutableLiveData<Boolean> showComments = new MutableLiveData<>(false);
+    void observeShowComments(LifecycleOwner owner, Observer<? super Boolean> observer) {
+        showComments.observe(owner, observer);
+    }
     void observeBusyFlag(@NonNull LifecycleOwner owner, Observer<? super Boolean> observer) {
         busyFlag.observe(owner, observer);
     }
@@ -207,6 +211,9 @@ public class NewTransactionModel extends ViewModel {
     public boolean getBusyFlag() {
         return busyFlag.getValue();
     }
+    public void toggleShowComments() {
+        showComments.setValue(!showComments.getValue());
+    }
     enum ItemType {generalData, transactionRow, bottomFiller}
 
     enum FocusedElement {Account, Comment, Amount}
index 5a057934ffb300e47c98ff1fcb354a2429073a88..44d902e032240c3dcea046416fc0f6bb35dfbc67 100644 (file)
             app:layout_constraintStart_toEndOf="@id/new_transaction_date"
             app:layout_constraintTop_toTopOf="parent" />
 
+        <androidx.constraintlayout.widget.ConstraintLayout
+            android:id="@+id/transaction_comment_layout"
+            android:layout_width="0dp"
+            android:layout_height="wrap_content"
+            android:alpha="0.50"
+            app:layout_constraintEnd_toEndOf="parent"
+            app:layout_constraintStart_toStartOf="parent"
+            app:layout_constraintTop_toBottomOf="@id/new_transaction_description">
+
+            <TextView
+                android:id="@+id/transaction_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/transaction_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/transaction_comment_button"
+                app:layout_constraintTop_toTopOf="parent" />
+        </androidx.constraintlayout.widget.ConstraintLayout>
+
     </androidx.constraintlayout.widget.ConstraintLayout>
 
     <androidx.constraintlayout.widget.ConstraintLayout
index a48b4ad687e4cd23a50ef44b0270def3ada1770d..c55428e95440feff2cb8b1a4a912fd0de971cf22 100644 (file)
         android:checkable="true"
         android:checked="false"
         app:showAsAction="never" />
+    <item
+        android:id="@+id/toggle_comments"
+        android:checkable="true"
+        android:title="@string/show_comments_switch"
+        app:actionLayout="@layout/switch_item"
+        app:showAsAction="never" />
     <item
         android:id="@+id/action_reset_new_transaction_activity"
         android:icon="@drawable/ic_refresh_white_24dp"
index 7b124b52c5a5f7e2893b6185e5772a33250d5f50..266db229c5e8f5df25f8a6638a5da5e90c450c0b 100644 (file)
     <string name="currency_input_by_default">Показване по подразбиране на полето за валута</string>
     <string name="profile_default_commodity">Валута по подразбиране</string>
     <string name="ignoring_preferred_account">Липсват трансакции с предпочитаната сметка</string>
-
+    <string name="show_comments_switch">Коментари</string>
 </resources>
index 187a8f64f0b9eac85b0bb2cd9d3ff64a5349acb3..a23586968b230dca4d33ebb36c15d3ecb01d3497 100644 (file)
     <string name="profile_default_commodity">Default commodity</string>
     <string name="ignoring_preferred_account">No transactions with preferred account found</string>
     <string name="icon">icon</string>
+    <string name="show_comments_switch">Comments</string>
 </resources>