From 69f3ecfe46fa70a3c2c7cfa2295dd5c8d5ffbc06 Mon Sep 17 00:00:00 2001 From: Damyan Ivanov Date: Fri, 1 May 2020 17:50:47 +0300 Subject: [PATCH] rework comments: add clear icon, italics, visible when populated, grayed-out when not focused --- .../ui/activity/NewTransactionItemHolder.java | 55 +++++++++------ .../activity/NewTransactionItemsAdapter.java | 3 - .../ui/activity/NewTransactionModel.java | 11 --- .../main/res/layout/new_transaction_row.xml | 69 +++++++++++-------- 4 files changed, 74 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java index 31ddee38..ab2f4032 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java @@ -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 focusedAccountObserver; private Observer accountCountObserver; private Observer editableObserver; - private Observer commentVisibleObserver; - private Observer commentObserver; private Observer currencyPositionObserver; private Observer currencyGapObserver; private Observer localeObserver; private Observer currencyObserver; private Observer showCurrencyObserver; + private Observer 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; diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java index d20f402f..e25b9a10 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemsAdapter.java @@ -333,9 +333,6 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter 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 editable = new MutableLiveData<>(true); private FocusedElement focusedElement = FocusedElement.Account; private MutableLiveData comment = new MutableLiveData<>(null); - private MutableLiveData commentVisible = new MutableLiveData<>(false); private MutableLiveData currency = new MutableLiveData<>(null); private boolean amountHintIsSet = false; Item(NewTransactionModel model) { @@ -407,12 +402,6 @@ public class NewTransactionModel extends ViewModel { void stopObservingEditableFlag(Observer observer) { editable.removeObserver(observer); } - void observeCommentVisible(NewTransactionActivity activity, Observer observer) { - commentVisible.observe(activity, observer); - } - void stopObservingCommentVisible(Observer observer) { - commentVisible.removeObserver(observer); - } void observeComment(NewTransactionActivity activity, Observer observer) { comment.observe(activity, observer); } diff --git a/app/src/main/res/layout/new_transaction_row.xml b/app/src/main/res/layout/new_transaction_row.xml index 90944e2c..c4097088 100644 --- a/app/src/main/res/layout/new_transaction_row.xml +++ b/app/src/main/res/layout/new_transaction_row.xml @@ -95,39 +95,44 @@ app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> - - - + 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"> + + + + + + app:layout_constraintTop_toBottomOf="@id/account_row_acc_name"> + 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" /> + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintRight_toRightOf="parent" + app:layout_constraintTop_toTopOf="parent" /> -- 2.39.2