]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/model/TemplateDetailsItem.java
add commodity support to template editor
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / TemplateDetailsItem.java
index 2eb3346df313e6c9ae040e84e05aa594d1856064..9a8f04cebf1a4ea9eee17039d12411464bfcc8e1 100644 (file)
@@ -119,9 +119,7 @@ abstract public class TemplateDetailsItem {
                 acc.setAccountCommentMatchGroup(pa.getAccountCommentMatchGroup());
 
             if (pa.getCurrencyMatchGroup() == null) {
-                final Integer currencyId = pa.getCurrency();
-                if (currencyId != null && currencyId > 0)
-                    acc.setCurrency(Currency.loadById(currencyId));
+                acc.setCurrency(pa.getCurrencyObject());
             }
             else
                 acc.setCurrencyMatchGroup(pa.getCurrencyMatchGroup());
@@ -300,7 +298,8 @@ abstract public class TemplateDetailsItem {
                 PossiblyMatchedValue.withLiteralString("");
         private final PossiblyMatchedValue<Float> amount =
                 PossiblyMatchedValue.withLiteralFloat(null);
-        private final PossiblyMatchedValue<Currency> currency = new PossiblyMatchedValue<>();
+        private final PossiblyMatchedValue<net.ktnx.mobileledger.db.Currency> currency =
+                new PossiblyMatchedValue<>();
         private boolean negateAmount;
         public AccountRow() {
             super(Type.ACCOUNT_ITEM);
@@ -339,10 +338,10 @@ abstract public class TemplateDetailsItem {
         public void setCurrencyMatchGroup(int group) {
             currency.setMatchGroup(group);
         }
-        public Currency getCurrency() {
+        public net.ktnx.mobileledger.db.Currency getCurrency() {
             return currency.getValue();
         }
-        public void setCurrency(Currency currency) {
+        public void setCurrency(net.ktnx.mobileledger.db.Currency currency) {
             this.currency.setValue(currency);
         }
         public int getAccountNameMatchGroup() {
@@ -385,6 +384,7 @@ abstract public class TemplateDetailsItem {
         public boolean hasLiteralAccountComment() {
             return accountComment.hasLiteralValue();
         }
+        public boolean hasLiteralCurrency() { return currency.hasLiteralValue(); }
         public boolean equalContents(AccountRow o) {
             if (position != o.position) {
                 Logger.debug("cmpAcc",
@@ -399,6 +399,9 @@ abstract public class TemplateDetailsItem {
         public void switchToLiteralAmount() {
             amount.switchToLiteral();
         }
+        public void switchToLiteralCurrency() {
+            currency.switchToLiteral();
+        }
         public void switchToLiteralAccountName() {
             accountName.switchToLiteral();
         }
@@ -427,6 +430,14 @@ abstract public class TemplateDetailsItem {
                 result.setNegateAmount(negateAmount ? true : null);
             }
 
+            if (currency.hasLiteralValue()) {
+                net.ktnx.mobileledger.db.Currency c = currency.getValue();
+                result.setCurrency((c == null) ? null : c.getId());
+            }
+            else {
+                result.setCurrencyMatchGroup(currency.getMatchGroup());
+            }
+
             return result;
         }
         public boolean isEmpty() {
@@ -500,8 +511,9 @@ abstract public class TemplateDetailsItem {
                 compiledPattern = null;
 
                 testMatch = new SpannableString(testText);
-                testMatch.setSpan(notMatchedSpan(), 0, testText.length() - 1,
-                        Spanned.SPAN_INCLUSIVE_INCLUSIVE);
+                if (!testText.isEmpty())
+                    testMatch.setSpan(notMatchedSpan(), 0, testText.length() - 1,
+                            Spanned.SPAN_INCLUSIVE_INCLUSIVE);
             }
         }
         @NonNull