]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
add UI and API support for sending posting (transaction account) comments
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionItemHolder.java
index 507055ff0eda9eac0dcc9e74d824a06c3db2d8cd..6988fe4f4ba4b0b17e0584d4e9491f03a1807fd9 100644 (file)
@@ -23,6 +23,7 @@ import android.text.Editable;
 import android.text.TextWatcher;
 import android.text.method.DigitsKeyListener;
 import android.view.View;
+import android.view.ViewGroup;
 import android.view.inputmethod.EditorInfo;
 import android.widget.AutoCompleteTextView;
 import android.widget.FrameLayout;
@@ -30,7 +31,6 @@ import android.widget.LinearLayout;
 import android.widget.TextView;
 
 import androidx.annotation.NonNull;
-import androidx.appcompat.widget.LinearLayoutCompat;
 import androidx.lifecycle.Observer;
 import androidx.recyclerview.widget.RecyclerView;
 
@@ -39,6 +39,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;
@@ -50,6 +51,8 @@ import java.util.Date;
 import java.util.GregorianCalendar;
 import java.util.Locale;
 
+import static net.ktnx.mobileledger.ui.activity.NewTransactionModel.ItemType;
+
 class NewTransactionItemHolder extends RecyclerView.ViewHolder
         implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
     private final String decimalSeparator;
@@ -58,9 +61,10 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     private TextView tvDate;
     private AutoCompleteTextView tvDescription;
     private AutoCompleteTextView tvAccount;
+    private TextView tvComment;
     private TextView tvAmount;
-    private LinearLayoutCompat lHead;
-    private LinearLayout lAccount;
+    private LinearLayout lHead;
+    private ViewGroup lAccount;
     private FrameLayout lPadding;
     private MobileLedgerProfile mProfile;
     private Date date;
@@ -75,6 +79,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
     NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
         super(itemView);
         tvAccount = itemView.findViewById(R.id.account_row_acc_name);
+        tvComment = itemView.findViewById(R.id.comment);
         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);
@@ -97,7 +102,19 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 boolean wasSyncing = syncingData;
                 syncingData = true;
                 try {
-                    adapter.updateFocusedItem(getAdapterPosition());
+                    final int pos = getAdapterPosition();
+                    adapter.updateFocusedItem(pos);
+                    switch (v.getId()) {
+                        case R.id.account_row_acc_name:
+                            adapter.noteFocusIsOnAccount(pos);
+                            break;
+                        case R.id.account_row_acc_amounts:
+                            adapter.noteFocusIsOnAmount(pos);
+                            break;
+                        case R.id.comment:
+                            adapter.noteFocusIsOnComment(pos);
+                            break;
+                    }
                 }
                 finally {
                     syncingData = wasSyncing;
@@ -172,6 +189,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
         };
         tvDescription.addTextChangedListener(tw);
         tvAccount.addTextChangedListener(tw);
+        tvComment.addTextChangedListener(tw);
         tvAmount.addTextChangedListener(amountWatcher);
 
         // FIXME: react on locale changes
@@ -234,10 +252,21 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     case transactionRow:
                         // do nothing if a row element already has the focus
                         if (!itemView.hasFocus()) {
-                            focused = tvAccount.requestFocus();
-                            tvAccount.dismissDropDown();
-                            if (focused)
-                                Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
+                            switch (item.getFocusedElement()) {
+                                case Amount:
+                                    tvAmount.requestFocus();
+                                    break;
+                                case Comment:
+                                    tvComment.requestFocus();
+                                    break;
+                                case Account:
+                                    focused = tvAccount.requestFocus();
+                                    tvAccount.dismissDropDown();
+                                    if (focused)
+                                        Misc.showSoftKeyboard(
+                                                (NewTransactionActivity) tvAccount.getContext());
+                                    break;
+                            }
                         }
 
                         break;
@@ -252,7 +281,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                             adapterPosition, layoutPosition, item.getType()
                                                                  .toString()
                                                                  .concat(item.getType() ==
-                                                                         NewTransactionModel.ItemType.transactionRow
+                                                                         ItemType.transactionRow
                                                                          ? String.format(Locale.US,
                                                                          "'%s'=%s",
                                                                          item.getAccount()
@@ -309,29 +338,28 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                     item.setDescription(String.valueOf(tvDescription.getText()));
                     break;
                 case transactionRow:
-                    item.getAccount()
-                        .setAccountName(String.valueOf(tvAccount.getText()));
+                    final LedgerTransactionAccount account = item.getAccount();
+                    account.setAccountName(String.valueOf(tvAccount.getText()));
+
+                    account.setComment(String.valueOf(tvComment.getText()));
 
                     // TODO: handle multiple amounts
                     String amount = String.valueOf(tvAmount.getText());
                     amount = amount.trim();
 
                     if (amount.isEmpty()) {
-                        item.getAccount()
-                            .resetAmount();
+                        account.resetAmount();
                     }
                     else {
                         try {
                             amount = amount.replace(decimalSeparator, decimalDot);
-                            item.getAccount()
-                                .setAmount(Float.parseFloat(amount));
+                            account.setAmount(Float.parseFloat(amount));
                         }
                         catch (NumberFormatException e) {
                             Logger.debug("new-trans", String.format(
                                     "assuming amount is not set due to number format exception. " +
                                     "input was '%s'", amount));
-                            item.getAccount()
-                                .resetAmount();
+                            account.resetAmount();
                         }
                     }
 
@@ -346,6 +374,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");
@@ -384,6 +413,7 @@ class NewTransactionItemHolder extends RecyclerView.ViewHolder
                 case transactionRow:
                     LedgerTransactionAccount acc = item.getAccount();
                     tvAccount.setText(acc.getAccountName());
+                    tvComment.setText(acc.getComment());
                     if (acc.isAmountSet()) {
                         tvAmount.setText(String.format("%1.2f", acc.getAmount()));
                     }