From: Damyan Ivanov Date: Sun, 5 Jan 2020 18:23:29 +0000 (+0200) Subject: fully functional comments UI X-Git-Tag: v0.12.0~107 X-Git-Url: https://git.ktnx.net/?a=commitdiff_plain;h=5f09bee380291c06a5fee1bb55049a1b09cf4e3c;p=mobile-ledger.git fully functional comments UI --- 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 6988fe4f..504ec0ad 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 @@ -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 focusedAccountObserver; private Observer accountCountObserver; private Observer editableObserver; + private Observer commentVisibleObserver; + private Observer 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() 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 2cf972f8..b956c7e8 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 @@ -295,4 +295,7 @@ class NewTransactionItemsAdapter extends RecyclerView.Adapter 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 editable = new MutableLiveData<>(true); private FocusedElement focusedElement = FocusedElement.Account; + private MutableLiveData comment = new MutableLiveData<>(null); + private MutableLiveData 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 observer) { editable.removeObserver(observer); } + public void observeCommentVisible(NewTransactionActivity activity, + Observer observer) { + commentVisible.observe(activity, observer); + } + public void stopObservingCommentVisible(Observer observer) { + commentVisible.removeObserver(observer); + } + public void observeComment(NewTransactionActivity activity, + Observer observer) { + comment.observe(activity, observer); + } + public void stopObservingComment(Observer 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 index 00000000..bea11d0c --- /dev/null +++ b/app/src/main/res/drawable-anydpi-v21/ic_comment_gray_24dp.xml @@ -0,0 +1,29 @@ + + + + + diff --git a/app/src/main/res/layout/new_transaction_row.xml b/app/src/main/res/layout/new_transaction_row.xml index 508eea98..0b36bc4e 100644 --- a/app/src/main/res/layout/new_transaction_row.xml +++ b/app/src/main/res/layout/new_transaction_row.xml @@ -78,7 +78,8 @@ + android:layout_height="wrap_content" + android:animateLayoutChanges="true"> @@ -111,6 +111,7 @@ 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" />