]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
transaction-level comments in new transaction UI, optional
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionItemHolder.java
index a72de5d09d12cc5b9ccb1d36aab98e91ab05fa90..d92a4c69d542aaef4496ac002d4de7b6a80aed9e 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;
@@ -29,7 +30,6 @@ 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;
@@ -46,7 +46,6 @@ import net.ktnx.mobileledger.model.LedgerTransactionAccount;
 import net.ktnx.mobileledger.model.MobileLedgerProfile;
 import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
 import net.ktnx.mobileledger.ui.DatePickerFragment;
-import net.ktnx.mobileledger.ui.OnCurrencySelectedListener;
 import net.ktnx.mobileledger.ui.TextViewClearHelper;
 import net.ktnx.mobileledger.utils.Colors;
 import net.ktnx.mobileledger.utils.DimensionUtils;
@@ -63,18 +62,19 @@ import java.util.Locale;
 import static net.ktnx.mobileledger.ui.activity.NewTransactionModel.ItemType;
 
 class NewTransactionItemHolder extends RecyclerView.ViewHolder
-        implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback,
-        OnCurrencySelectedListener {
+        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 NewTransactionModel.Item item;
     private TextView tvDate;
     private AutoCompleteTextView tvDescription;
     private AutoCompleteTextView tvAccount;
     private TextView tvComment;
     private EditText tvAmount;
-    private LinearLayout lHead;
+    private ViewGroup lHead;
     private ViewGroup lAccount;
     private FrameLayout lPadding;
     private MobileLedgerProfile mProfile;
@@ -85,23 +85,21 @@ 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;
-    private NewTransactionItemsAdapter adapter;
     //TODO multiple amounts with different currencies per posting
     NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
         super(itemView);
-        this.adapter = adapter;
         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);
@@ -111,6 +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);
+        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);
@@ -118,18 +119,30 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
 
         tvDate.setOnClickListener(v -> pickTransactionDate());
 
+        commentButton.setOnClickListener(v -> {
+            tvComment.setVisibility(View.VISIBLE);
+            tvComment.requestFocus();
+        });
+
+        itemView.findViewById(R.id.transaction_comment_button)
+                .setOnClickListener(v -> {
+                    tvTransactionComment.setVisibility(View.VISIBLE);
+                    tvTransactionComment.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;
@@ -145,17 +158,20 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     syncingData = wasSyncing;
                 }
             }
+
+            if (id == R.id.comment) {
+                commentFocusChanged(commentLayout, tvComment, hasFocus);
+            }
+            else if ( id == R.id.transaction_comment) {
+                commentFocusChanged(transactionCommentLayout, tvTransactionComment, hasFocus);
+            }
         };
 
         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,
@@ -185,7 +201,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 syncData();
                 Logger.debug("textWatcher",
                         "syncData() returned, checking if transaction is submittable");
-                adapter.model.checkTransactionSubmittable(adapter);
+                adapter.checkTransactionSubmittable();
                 Logger.debug("textWatcher", "done");
             }
         };
@@ -215,7 +231,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 }
 
                 if (syncData())
-                    adapter.model.checkTransactionSubmittable(adapter);
+                    adapter.checkTransactionSubmittable();
             }
         };
         tvDescription.addTextChangedListener(tw);
@@ -225,7 +241,8 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
 
         tvCurrency.setOnClickListener(v -> {
             CurrencySelectorFragment cpf = new CurrencySelectorFragment();
-            cpf.setOnCurrencySelectedListener(this);
+            cpf.showPositionAndPadding();
+            cpf.setOnCurrencySelectedListener(c -> item.setCurrency(c));
             final AppCompatActivity activity = (AppCompatActivity) v.getContext();
             cpf.show(activity.getSupportFragmentManager(), "currency-selector");
         });
@@ -274,8 +291,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()) {
@@ -297,6 +312,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                                     tvAmount.requestFocus();
                                     break;
                                 case Comment:
+                                    tvComment.setVisibility(View.VISIBLE);
                                     tvComment.requestFocus();
                                     break;
                                 case Account:
@@ -346,16 +362,15 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
 
         currencyObserver = currency -> {
             setCurrency(currency);
-            adapter.model.checkTransactionSubmittable(adapter);
+            adapter.checkTransactionSubmittable();
         };
 
-        currencyGapObserver = hasGap -> {
-            updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(), hasGap);
-        };
+        currencyGapObserver =
+                hasGap -> updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(),
+                        hasGap);
 
-        currencyPositionObserver = position -> {
-            updateCurrencyPositionAndPadding(position, Data.currencyGap.getValue());
-        };
+        currencyPositionObserver =
+                position -> updateCurrencyPositionAndPadding(position, Data.currencyGap.getValue());
 
         showCurrencyObserver = showCurrency -> {
             if (showCurrency) {
@@ -366,6 +381,58 @@ 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);
+        };
+
+        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 =
@@ -431,33 +498,6 @@ 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");
@@ -558,8 +598,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()
@@ -569,6 +607,8 @@ 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.getModel().showComments.removeObserver(showCommentsObserver);
 
                 this.item = null;
             }
@@ -616,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:
@@ -624,13 +666,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;
@@ -652,10 +693,6 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
 
     }
     @Override
-    public void onCurrencySelected(Currency currency) {
-        adapter.model.setItemCurrency(this.item, currency, adapter);
-    }
-    @Override
     public void descriptionSelected(String description) {
         tvAccount.setText(description);
         tvAmount.requestFocus(View.FOCUS_FORWARD);