]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
add progress while retrieving previous transaction
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionItemHolder.java
index 3a75760718d102f882f9ed3956f2b2331e7e8991..b63ce8e9adeab74c7fcc1a5c814e1983ec3babfd 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,7 @@ import android.view.inputmethod.EditorInfo;
 import android.widget.AutoCompleteTextView;
 import android.widget.EditText;
 import android.widget.FrameLayout;
-import android.widget.LinearLayout;
+import android.widget.ProgressBar;
 import android.widget.TextView;
 
 import androidx.annotation.NonNull;
@@ -74,7 +75,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     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,21 +86,19 @@ 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 Observer<Boolean> busyFlagObserver;
     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);
         new TextViewClearHelper().attachToTextView((EditText) tvComment);
@@ -111,6 +110,8 @@ 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);
+        ProgressBar p = itemView.findViewById(R.id.progressBar);
 
         tvDescription.setNextFocusForwardId(View.NO_ID);
         tvAccount.setNextFocusForwardId(View.NO_ID);
@@ -118,18 +119,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;
@@ -145,17 +153,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,
@@ -275,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()) {
@@ -350,13 +364,12 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
             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) {
@@ -367,6 +380,17 @@ 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);
+        };
+
+        busyFlagObserver = isBusy -> {p.setVisibility(isBusy ? View.VISIBLE : View.INVISIBLE);};
     }
     private void updateCurrencyPositionAndPadding(Currency.Position position, boolean hasGap) {
         ConstraintLayout.LayoutParams amountLP =
@@ -444,21 +468,15 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
             commentButton.setLayoutParams(lp);
         }
         else {
-            // hiding; hide the comment comment view and align amounts layout under it
+            // 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.ntr_account;   // R.id.parent doesn't work here
+            lp.bottomToBottom = R.id.amount_layout;   // 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");
@@ -559,8 +577,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()
@@ -570,6 +586,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().stopObservingBusyFlag(busyFlagObserver);
 
                 this.item = null;
             }
@@ -622,16 +640,16 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     case generalData:
                         item.observeDate(activity, dateObserver);
                         item.observeDescription(activity, descriptionObserver);
+                        item.getModel().observeBusyFlag(activity, busyFlagObserver);
                         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;