]> git.ktnx.net Git - mobile-ledger.git/commitdiff
fully functional comments UI
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 5 Jan 2020 18:23:29 +0000 (20:23 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 5 Jan 2020 18:23:29 +0000 (20:23 +0200)
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/drawable-anydpi-v21/ic_comment_gray_24dp.xml [new file with mode: 0644]
app/src/main/res/layout/new_transaction_row.xml

index 6988fe4f4ba4b0b17e0584d4e9491f03a1807fd9..504ec0adc0811477726869e4c2810ffaefec7da0 100644 (file)
@@ -26,11 +26,14 @@ import android.view.View;
 import android.view.ViewGroup;
 import android.view.inputmethod.EditorInfo;
 import android.widget.AutoCompleteTextView;
+import android.widget.EditText;
 import android.widget.FrameLayout;
 import android.widget.LinearLayout;
 import android.widget.TextView;
 
 import androidx.annotation.NonNull;
+import androidx.appcompat.app.AppCompatActivity;
+import androidx.constraintlayout.widget.ConstraintLayout;
 import androidx.lifecycle.Observer;
 import androidx.recyclerview.widget.RecyclerView;
 
@@ -39,8 +42,10 @@ import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
-import net.ktnx.mobileledger.ui.AutoCompleteTextViewWithClear;
+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.Logger;
 import net.ktnx.mobileledger.utils.MLDB;
 import net.ktnx.mobileledger.utils.Misc;
@@ -74,12 +79,17 @@ 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 boolean inUpdate = false;
     private boolean syncingData = false;
+    private View commentButton;
     NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
         super(itemView);
         tvAccount = itemView.findViewById(R.id.account_row_acc_name);
         tvComment = itemView.findViewById(R.id.comment);
+        new TextViewClearHelper().attachToTextView((EditText) tvComment);
+        commentButton = itemView.findViewById(R.id.comment_button);
         tvAmount = itemView.findViewById(R.id.account_row_acc_amounts);
         tvDate = itemView.findViewById(R.id.new_transaction_date);
         tvDescription = itemView.findViewById(R.id.new_transaction_description);
@@ -126,6 +136,11 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         tvAccount.setOnFocusChangeListener(focusMonitor);
         tvAmount.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,
@@ -236,6 +251,8 @@ 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()) {
@@ -305,6 +322,33 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         tvAccount.setEnabled(editable);
         tvAmount.setEnabled(editable);
     }
+    private void setCommentVisible(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 comment view and align amounts layout under it
+            tvComment.setVisibility(View.GONE);
+            ConstraintLayout.LayoutParams lp =
+                    (ConstraintLayout.LayoutParams) commentButton.getLayoutParams();
+            lp.bottomToBottom = R.id.ntr_account;   // R.id.parent doesn't work here
+
+            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");
@@ -341,7 +385,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     final LedgerTransactionAccount account = item.getAccount();
                     account.setAccountName(String.valueOf(tvAccount.getText()));
 
-                    account.setComment(String.valueOf(tvComment.getText()));
+                    item.setComment(String.valueOf(tvComment.getText()));
 
                     // TODO: handle multiple amounts
                     String amount = String.valueOf(tvAmount.getText());
@@ -393,6 +437,8 @@ 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()
@@ -443,6 +489,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 item.observeDescription(activity, descriptionObserver);
                 item.observeAmountHint(activity, hintObserver);
                 item.observeEditableFlag(activity, editableObserver);
+                item.observeCommentVisible(activity, commentVisibleObserver);
+                item.observeComment(activity, commentObserver);
                 item.getModel()
                     .observeFocusedItem(activity, focusedAccountObserver);
                 item.getModel()
index 2cf972f89ed597df5bba2bce282381f4976b1bfb..b956c7e8ea34a019e3986ed3300a45e2bb46505f 100644 (file)
@@ -295,4 +295,7 @@ 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);
+    }
 }
index bd2c640fd0ddfaa7b477f1287e6875c9f306b3e1..6dd640f9d8d22d505811f309dc0bd1b733979488 100644 (file)
@@ -300,6 +300,10 @@ public class NewTransactionModel extends ViewModel {
     public void swapItems(int one, int two) {
         Collections.swap(items, one-1, two-1);
     }
+    public void toggleComment(int position) {
+        final MutableLiveData<Boolean> commentVisible = getItem(position).commentVisible;
+        commentVisible.postValue(!commentVisible.getValue());
+    }
     enum ItemType {generalData, transactionRow, bottomFiller}
 
     //==========================================================================================
@@ -315,6 +319,8 @@ public class NewTransactionModel extends ViewModel {
         private NewTransactionModel model;
         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);
         public Item(NewTransactionModel model) {
             this.model = model;
             type = ItemType.bottomFiller;
@@ -506,5 +512,23 @@ public class NewTransactionModel extends ViewModel {
         public void stopObservingEditableFlag(Observer<Boolean> observer) {
             editable.removeObserver(observer);
         }
+        public void observeCommentVisible(NewTransactionActivity activity,
+                                          Observer<Boolean> observer) {
+            commentVisible.observe(activity, observer);
+        }
+        public void stopObservingCommentVisible(Observer<Boolean> observer) {
+            commentVisible.removeObserver(observer);
+        }
+        public void observeComment(NewTransactionActivity activity,
+                                          Observer<String> observer) {
+            comment.observe(activity, observer);
+        }
+        public void stopObservingComment(Observer<String> observer) {
+            comment.removeObserver(observer);
+        }
+        public void setComment(String comment) {
+            getAccount().setComment(comment);
+            this.comment.postValue(comment);
+        }
     }
 }
diff --git a/app/src/main/res/drawable-anydpi-v21/ic_comment_gray_24dp.xml b/app/src/main/res/drawable-anydpi-v21/ic_comment_gray_24dp.xml
new file mode 100644 (file)
index 0000000..bea11d0
--- /dev/null
@@ -0,0 +1,29 @@
+<!--
+  ~ Copyright Google Inc.
+  ~
+  ~ Licensed under the Apache License, version 2.0 ("the License");
+  ~ you may not use this file except in compliance with the License.
+  ~ You may obtain a copy of the license at:
+  ~
+  ~ https://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing, software
+  ~ distribution under the License is distributed on an "AS IS" BASIS,
+  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~ See the License for the specific language governing permissions and
+  ~ limitations under the License.
+  ~
+  ~ Modified/adapted by Damyan Ivanov for MoLe
+  -->
+
+<vector xmlns:android="http://schemas.android.com/apk/res/android"
+    android:width="24dp"
+    android:height="24dp"
+    android:autoMirrored="true"
+    android:tint="#CCCCCC"
+    android:viewportWidth="24.0"
+    android:viewportHeight="24.0">
+    <path
+        android:fillColor="#FF000000"
+        android:pathData="M21.99,4c0,-1.1 -0.89,-2 -1.99,-2L4,2c-1.1,0 -2,0.9 -2,2v12c0,1.1 0.9,2 2,2h14l4,4 -0.01,-18zM18,14L6,14v-2h12v2zM18,11L6,11L6,9h12v2zM18,8L6,8L6,6h12v2z" />
+</vector>
index 508eea9892b1529a7ae7e58d6625689d1bd2e66a..0b36bc4eec912c64e555e1f4a2c7f83a0844bf5b 100644 (file)
@@ -78,7 +78,8 @@
     <androidx.constraintlayout.widget.ConstraintLayout
         android:id="@+id/ntr_account"
         android:layout_width="match_parent"
-        android:layout_height="wrap_content">
+        android:layout_height="wrap_content"
+        android:animateLayoutChanges="true">
 
         <net.ktnx.mobileledger.ui.AutoCompleteTextViewWithClear
             android:id="@+id/account_row_acc_name"
@@ -98,9 +99,8 @@
             android:id="@+id/comment_button"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
-            android:alpha="0.5"
-            android:drawableStart="@drawable/ic_comment_black_24dp"
-            app:layout_constraintBottom_toBottomOf="@+id/comment"
+            android:background="@drawable/ic_comment_gray_24dp"
+            app:layout_constraintBottom_toBottomOf="parent"
             app:layout_constraintStart_toStartOf="@+id/ntr_account"
             app:layout_constraintTop_toBottomOf="@+id/account_row_acc_name" />
 
             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" />