]> git.ktnx.net Git - mobile-ledger.git/commitdiff
ui/machinery support for negated pattern amounts
authorDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 31 Jan 2021 20:00:41 +0000 (22:00 +0200)
committerDamyan Ivanov <dam+mobileledger@ktnx.net>
Sun, 31 Jan 2021 20:00:41 +0000 (22:00 +0200)
app/src/main/java/net/ktnx/mobileledger/model/PatternDetailsItem.java
app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionFragment.java
app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternDetailsAdapter.java
app/src/main/java/net/ktnx/mobileledger/ui/patterns/PatternDetailsViewModel.java
app/src/main/res/layout/pattern_details_account.xml

index 52fa1dc7671c1f877be9590eb22a179dae95223e..eeab37aed6a53431cda383e8c0067063e41ac533 100644 (file)
@@ -114,8 +114,11 @@ abstract public class PatternDetailsItem {
                 acc.setCurrencyMatchGroup(pa.getCurrencyMatchGroup());
 
             final Integer amountMatchGroup = pa.getAmountMatchGroup();
-            if (amountMatchGroup != null && amountMatchGroup > 0)
+            if (amountMatchGroup != null && amountMatchGroup > 0) {
                 acc.setAmountMatchGroup(amountMatchGroup);
+                final Boolean negateAmount = pa.getNegateAmount();
+                acc.setNegateAmount(negateAmount != null && negateAmount);
+            }
             else
                 acc.setAmount(pa.getAmount());
 
@@ -274,9 +277,16 @@ abstract public class PatternDetailsItem {
         private final PossiblyMatchedValue<Float> amount =
                 PossiblyMatchedValue.withLiteralFloat(0f);
         private final PossiblyMatchedValue<Currency> currency = new PossiblyMatchedValue<>();
+        private boolean negateAmount;
         private AccountRow() {
             super(Type.ACCOUNT_ITEM);
         }
+        public boolean isNegateAmount() {
+            return negateAmount;
+        }
+        public void setNegateAmount(boolean negateAmount) {
+            this.negateAmount = negateAmount;
+        }
         public int getAccountCommentMatchGroup() {
             return accountComment.getMatchGroup();
         }
@@ -343,7 +353,7 @@ abstract public class PatternDetailsItem {
         }
         public boolean equalContents(AccountRow o) {
             return amount.equals(o.amount) && accountName.equals(o.accountName) &&
-                   accountComment.equals(o.accountComment);
+                   accountComment.equals(o.accountComment) && negateAmount == o.negateAmount;
         }
         public void switchToLiteralAmount() {
             amount.switchToLiteral();
@@ -367,10 +377,14 @@ abstract public class PatternDetailsItem {
             else
                 result.setAccountCommentMatchGroup(accountComment.getMatchGroup());
 
-            if (amount.hasLiteralValue())
+            if (amount.hasLiteralValue()) {
                 result.setAmount(amount.getValue());
-            else
+                result.setNegateAmount(null);
+            }
+            else {
                 result.setAmountMatchGroup(amount.getMatchGroup());
+                result.setNegateAmount(negateAmount ? true : null);
+            }
 
             return result;
         }
index cdd06c09dc7eabcf938281cb655d50f9ef426020..140b3a74e0512d68e39159366af8817cbec3fe85 100644 (file)
@@ -262,7 +262,7 @@ public class NewTransactionFragment extends QRScanCapableFragment {
                                   acc.getAccountComment());
                   Float amount =
                           extractFloatFromMatches(m, acc.getAmountMatchGroup(), acc.getAmount());
-                  if (amount != null && acc.getNegateAmount())
+                  if (amount != null && acc.getNegateAmount() != null && acc.getNegateAmount())
                       amount = -amount;
 
                   if (accountsInInitialState) {
index 1484eab7925a7afd755dc664b65a771b09b4c338..c7f42ec178dc3026afc9c79f6284a3366c577514 100644 (file)
@@ -502,6 +502,9 @@ class PatternDetailsAdapter extends RecyclerView.Adapter<PatternDetailsAdapter.V
 
                 b.patternDetailsAccountAmount.setText(Data.formatNumber(amt));
             });
+
+            b.negateAmountSwitch.setOnCheckedChangeListener(
+                    (buttonView, isChecked) -> getItem().setNegateAmount(isChecked));
         }
         @Override
         void bind(PatternDetailsItem item) {
@@ -539,12 +542,15 @@ class PatternDetailsAdapter extends RecyclerView.Adapter<PatternDetailsAdapter.V
                 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(
index 4a964accfa2b053a01ea9c7417a13c9822a1f894..982bb47c6ae1440c49eace99976e9edb940cb997 100644 (file)
@@ -170,9 +170,9 @@ public class PatternDetailsViewModel extends ViewModel {
                     paDAO.update(dbAccount);
 
                 Logger.debug("pattern-db", String.format(Locale.US,
-                        "Stored pattern account %d, account=%s, comment=%s, item=%s",
+                        "Stored pattern account %d, account=%s, comment=%s, neg=%s, item=%s",
                         dbAccount.getId(), dbAccount.getAccountName(),
-                        dbAccount.getAccountComment(), accRowItem));
+                        dbAccount.getAccountComment(), dbAccount.getNegateAmount(), accRowItem));
             }
         });
     }
index 6a40b0cb8c6a078d968b78d66c32d55ac41e9842..49944789a08fcb01c0bc4ba96cf8e5308ed76959 100644 (file)
         android:paddingTop="@dimen/text_margin"
         android:text="@string/account_amount_source_label"
         android:textAppearance="?attr/textAppearanceListItem"
-        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintEnd_toStartOf="@id/negate_amount_switch"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@id/pattern_details_account_comment_layout"
         />
         android:layout_width="0dp"
         android:layout_height="wrap_content"
         android:textAppearance="?attr/textAppearanceListItemSecondary"
-        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintEnd_toStartOf="@id/negate_amount_switch"
         app:layout_constraintStart_toStartOf="parent"
         app:layout_constraintTop_toBottomOf="@id/pattern_account_amount_source_label"
         />
+    <com.google.android.material.switchmaterial.SwitchMaterial
+        android:id="@+id/negate_amount_switch"
+        android:layout_width="wrap_content"
+        android:layout_height="wrap_content"
+        app:layout_constraintBottom_toBottomOf="@id/pattern_details_account_amount_source"
+        app:layout_constraintEnd_toEndOf="parent"
+        app:layout_constraintTop_toTopOf="@id/pattern_account_amount_source_label"
+        />
     <com.google.android.material.textfield.TextInputLayout
         android:id="@+id/pattern_details_account_amount_layout"
         android:layout_width="match_parent"