]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternDetailsAdapter.java
ui/machinery support for negated pattern amounts
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / patterns / PatternDetailsAdapter.java
index e98a28f7596393447dc479518eff01b40505c84d..c7f42ec178dc3026afc9c79f6284a3366c577514 100644 (file)
@@ -36,12 +36,13 @@ import net.ktnx.mobileledger.db.PatternBase;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.PatternDetailsItem;
 import net.ktnx.mobileledger.ui.PatternDetailSourceSelectorFragment;
-import net.ktnx.mobileledger.ui.QRScanAbleFragment;
+import net.ktnx.mobileledger.ui.QRScanCapableFragment;
 import net.ktnx.mobileledger.utils.Logger;
 import net.ktnx.mobileledger.utils.Misc;
 
 import org.jetbrains.annotations.NotNull;
 
+import java.text.ParseException;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Locale;
@@ -416,7 +417,7 @@ class PatternDetailsAdapter extends RecyclerView.Adapter<PatternDetailsAdapter.V
 
         }
         private void scanTestQR(View view) {
-            QRScanAbleFragment.triggerQRScan();
+            QRScanCapableFragment.triggerQRScan();
         }
     }
 
@@ -454,6 +455,56 @@ class PatternDetailsAdapter extends RecyclerView.Adapter<PatternDetailsAdapter.V
                 }
             };
             b.patternDetailsAccountComment.addTextChangedListener(accountCommentWatcher);
+
+            b.patternDetailsAccountAmount.addTextChangedListener(new TextWatcher() {
+                @Override
+                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
+
+                }
+                @Override
+                public void onTextChanged(CharSequence s, int start, int before, int count) {
+
+                }
+                @Override
+                public void afterTextChanged(Editable s) {
+                    PatternDetailsItem.AccountRow accRow = getItem();
+
+                    String str = String.valueOf(s);
+                    if (Misc.emptyIsNull(str) == null) {
+                        accRow.setAmount(null);
+                    }
+                    else {
+                        try {
+                            final float amount = Data.parseNumber(str);
+                            accRow.setAmount(amount);
+                            b.patternDetailsAccountAmountLayout.setError(null);
+
+                            Logger.debug(D_PATTERN_UI, String.format(Locale.US,
+                                    "Storing changed account amount %s [%4.2f]; accRow=%s", s,
+                                    amount, accRow));
+                        }
+                        catch (NumberFormatException | ParseException e) {
+                            b.patternDetailsAccountAmountLayout.setError("!");
+                        }
+                    }
+                }
+            });
+            b.patternDetailsAccountAmount.setOnFocusChangeListener((v, hasFocus) -> {
+                if (hasFocus)
+                    return;
+
+                PatternDetailsItem.AccountRow accRow = getItem();
+                if (!accRow.hasLiteralAmount())
+                    return;
+                Float amt = accRow.getAmount();
+                if (amt == null)
+                    return;
+
+                b.patternDetailsAccountAmount.setText(Data.formatNumber(amt));
+            });
+
+            b.negateAmountSwitch.setOnCheckedChangeListener(
+                    (buttonView, isChecked) -> getItem().setNegateAmount(isChecked));
         }
         @Override
         void bind(PatternDetailsItem item) {
@@ -488,13 +539,18 @@ class PatternDetailsAdapter extends RecyclerView.Adapter<PatternDetailsAdapter.V
                 b.patternDetailsAccountAmountSource.setText(
                         R.string.pattern_details_source_literal);
                 b.patternDetailsAccountAmount.setVisibility(View.VISIBLE);
-                b.patternDetailsAccountAmount.setText(Data.formatNumber(accRow.getAmount()));
+                Float amt = accRow.getAmount();
+                b.patternDetailsAccountAmount.setText((amt == null) ? null : String.format(
+                        Data.locale.getValue(), "%,4.2f", (accRow.getAmount())));
+                b.negateAmountSwitch.setVisibility(View.GONE);
             }
             else {
                 b.patternDetailsAccountAmountSource.setText(
                         String.format(Locale.US, "Group %d (%s)", accRow.getAmountMatchGroup(),
                                 getMatchGroupText(accRow.getAmountMatchGroup())));
                 b.patternDetailsAccountAmountLayout.setVisibility(View.GONE);
+                b.negateAmountSwitch.setVisibility(View.VISIBLE);
+                b.negateAmountSwitch.setChecked(accRow.isNegateAmount());
             }
 
             b.patternAccountNameSourceLabel.setOnClickListener(