]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
whitespace and wrapping
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionItemHolder.java
index 82403a87af20640063f872e4f38e28d61e5ada0d..9ee32ea728f683ca307ba695c3716071ef441e38 100644 (file)
@@ -30,7 +30,6 @@ import android.widget.LinearLayout;
 import android.widget.TextView;
 
 import androidx.annotation.NonNull;
-import androidx.constraintlayout.widget.ConstraintLayout;
 import androidx.lifecycle.Observer;
 import androidx.recyclerview.widget.RecyclerView;
 
@@ -39,6 +38,7 @@ 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.DatePickerFragment;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.MLDB;
@@ -59,7 +59,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private AutoCompleteTextView tvDescription;
     private AutoCompleteTextView tvAccount;
     private TextView tvAmount;
-    private ConstraintLayout lHead;
+    private LinearLayout lHead;
     private LinearLayout lAccount;
     private FrameLayout lPadding;
     private MobileLedgerProfile mProfile;
@@ -86,16 +86,36 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         tvAccount.setNextFocusForwardId(View.NO_ID);
         tvAmount.setNextFocusForwardId(View.NO_ID); // magic!
 
-        tvDate.setOnFocusChangeListener((v, hasFocus) -> {
-            if (hasFocus)
-                pickTransactionDate();
-        });
         tvDate.setOnClickListener(v -> pickTransactionDate());
 
         mProfile = Data.profile.getValue();
         if (mProfile == null)
             throw new AssertionError();
 
+        View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
+            if (hasFocus) {
+                boolean wasSyncing = syncingData;
+                syncingData = true;
+                try {
+                    final int pos = getAdapterPosition();
+                    adapter.updateFocusedItem(pos);
+                    if (v instanceof AutoCompleteTextViewWithClear) {
+                        adapter.noteFocusIsOnAccount(pos);
+                    }
+                    else {
+                        adapter.noteFocusIsOnAmount(pos);
+                    }
+                }
+                finally {
+                    syncingData = wasSyncing;
+                }
+            }
+        };
+
+        tvDescription.setOnFocusChangeListener(focusMonitor);
+        tvAccount.setOnFocusChangeListener(focusMonitor);
+        tvAmount.setOnFocusChangeListener(focusMonitor);
+
         MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription,
                 MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
         MLDB.hookAutocompletionAdapter(tvAccount.getContext(), tvAccount, MLDB.ACCOUNTS_TABLE,
@@ -196,9 +216,9 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
             syncingData = true;
             try {
                 if (hint == null)
-                    hint = tvAmount.getResources()
-                                   .getString(R.string.zero_amount);
-                tvAmount.setHint(hint);
+                    tvAmount.setHint(R.string.zero_amount);
+                else
+                    tvAmount.setHint(hint);
             }
             finally {
                 syncingData = false;
@@ -219,10 +239,19 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                                     (NewTransactionActivity) tvDescription.getContext());
                         break;
                     case transactionRow:
-                        focused = tvAccount.requestFocus();
-                        tvAccount.dismissDropDown();
-                        if (focused)
-                            Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
+                        // do nothing if a row element already has the focus
+                        if (!itemView.hasFocus()) {
+                            if (item.focusIsOnAmount()) {
+                                tvAmount.requestFocus();
+                            }
+                            else {
+                                focused = tvAccount.requestFocus();
+                                tvAccount.dismissDropDown();
+                                if (focused)
+                                    Misc.showSoftKeyboard(
+                                            (NewTransactionActivity) tvAccount.getContext());
+                            }
+                        }
 
                         break;
                 }
@@ -313,8 +342,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                         catch (NumberFormatException e) {
                             Logger.debug("new-trans", String.format(
                                     "assuming amount is not set due to number format exception. " +
-                                    "input was '%s'",
-                                    amount));
+                                    "input was '%s'", amount));
                             item.getAccount()
                                 .resetAmount();
                         }
@@ -331,6 +359,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     }
     private void pickTransactionDate() {
         DatePickerFragment picker = new DatePickerFragment();
+        picker.setFutureDates(mProfile.getFutureDates());
         picker.setOnDatePickedListener(this);
         picker.show(((NewTransactionActivity) tvDate.getContext()).getSupportFragmentManager(),
                 "datePicker");
@@ -369,8 +398,14 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 case transactionRow:
                     LedgerTransactionAccount acc = item.getAccount();
                     tvAccount.setText(acc.getAccountName());
-                    tvAmount.setText(
-                            acc.isAmountSet() ? String.format("%1.2f", acc.getAmount()) : "");
+                    if (acc.isAmountSet()) {
+                        tvAmount.setText(String.format("%1.2f", acc.getAmount()));
+                    }
+                    else {
+                        tvAmount.setText("");
+//                        tvAmount.setHint(R.string.zero_amount);
+                    }
+                    tvAmount.setHint(item.getAmountHint());
                     lHead.setVisibility(View.GONE);
                     lAccount.setVisibility(View.VISIBLE);
                     lPadding.setVisibility(View.GONE);