X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FTemplateDetailsItem.java;h=9a8f04cebf1a4ea9eee17039d12411464bfcc8e1;hb=fa6ed80a6ea4cfb968a60e45423980c2ab513531;hp=d0a6e240f27472e3196fb524819362628103d9eb;hpb=f11c8a93f92f5a372ea423cdf2d3d9629bd5f45f;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/model/TemplateDetailsItem.java b/app/src/main/java/net/ktnx/mobileledger/model/TemplateDetailsItem.java index d0a6e240..9a8f04ce 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/TemplateDetailsItem.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/TemplateDetailsItem.java @@ -32,11 +32,13 @@ import net.ktnx.mobileledger.R; import net.ktnx.mobileledger.db.TemplateAccount; import net.ktnx.mobileledger.db.TemplateBase; import net.ktnx.mobileledger.db.TemplateHeader; +import net.ktnx.mobileledger.utils.Logger; import net.ktnx.mobileledger.utils.Misc; import org.jetbrains.annotations.Contract; import org.jetbrains.annotations.NotNull; +import java.util.Locale; import java.util.Objects; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -45,7 +47,7 @@ import java.util.regex.PatternSyntaxException; abstract public class TemplateDetailsItem { private final Type type; protected Long id; - protected Long position; + protected long position; protected TemplateDetailsItem(Type type) { this.type = type; @@ -96,6 +98,8 @@ abstract public class TemplateDetailsItem { else header.setDateYearMatchGroup(ph.getDateYearMatchGroup()); + header.setFallback(ph.isFallback()); + return header; } else if (p instanceof TemplateAccount) { @@ -115,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()); @@ -172,7 +174,7 @@ abstract public class TemplateDetailsItem { public long getPosition() { return position; } - public void setPosition(Long position) { + public void setPosition(long position) { this.position = position; } abstract public String getProblem(@NonNull Resources r, int patternGroupCount); @@ -276,6 +278,12 @@ abstract public class TemplateDetailsItem { return "grp:" + matchGroup; return ""; } + public boolean isEmpty() { + if (literalValue) + return value == null || Misc.emptyIsNull(value.toString()) == null; + + return matchGroup > 0; + } } public static class TYPE { @@ -290,7 +298,8 @@ abstract public class TemplateDetailsItem { PossiblyMatchedValue.withLiteralString(""); private final PossiblyMatchedValue amount = PossiblyMatchedValue.withLiteralFloat(null); - private final PossiblyMatchedValue currency = new PossiblyMatchedValue<>(); + private final PossiblyMatchedValue currency = + new PossiblyMatchedValue<>(); private boolean negateAmount; public AccountRow() { super(Type.ACCOUNT_ITEM); @@ -329,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() { @@ -375,13 +384,24 @@ 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", + String.format(Locale.US, "[%d] != [%d]: pos %d != pos %d", getId(), + o.getId(), position, o.position)); + return false; + } return amount.equals(o.amount) && accountName.equals(o.accountName) && - accountComment.equals(o.accountComment) && negateAmount == o.negateAmount; + position == o.position && accountComment.equals(o.accountComment) && + negateAmount == o.negateAmount; } public void switchToLiteralAmount() { amount.switchToLiteral(); } + public void switchToLiteralCurrency() { + currency.switchToLiteral(); + } public void switchToLiteralAccountName() { accountName.switchToLiteral(); } @@ -410,8 +430,19 @@ 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() { + return accountName.isEmpty() && accountComment.isEmpty() && amount.isEmpty(); + } } public static class Header extends TemplateDetailsItem { @@ -428,6 +459,7 @@ abstract public class TemplateDetailsItem { private PossiblyMatchedValue dateMonth = PossiblyMatchedValue.withLiteralInt(null); private PossiblyMatchedValue dateDay = PossiblyMatchedValue.withLiteralInt(null); private SpannableString testMatch; + private boolean isFallback; private Header() { super(Type.HEADER); } @@ -445,12 +477,20 @@ abstract public class TemplateDetailsItem { dateYear = new PossiblyMatchedValue<>(origin.dateYear); dateMonth = new PossiblyMatchedValue<>(origin.dateMonth); dateDay = new PossiblyMatchedValue<>(origin.dateDay); + + isFallback = origin.isFallback; } private static StyleSpan capturedSpan() { return new StyleSpan(Typeface.BOLD); } private static UnderlineSpan matchedSpan() { return new UnderlineSpan(); } private static ForegroundColorSpan notMatchedSpan() { return new ForegroundColorSpan(Color.GRAY); } + public boolean isFallback() { + return isFallback; + } + public void setFallback(boolean fallback) { + this.isFallback = fallback; + } public String getName() { return name; } @@ -471,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 @@ -585,7 +626,7 @@ abstract public class TemplateDetailsItem { return Misc.equalStrings(name, o.name) && Misc.equalStrings(pattern, o.pattern) && Misc.equalStrings(testText, o.testText) && Misc.equalStrings(patternError, o.patternError) && - Objects.equals(testMatch, o.testMatch); + Objects.equals(testMatch, o.testMatch) && isFallback == o.isFallback; } public String getMatchGroupText(int group) { if (compiledPattern != null && testText != null) { @@ -655,6 +696,8 @@ abstract public class TemplateDetailsItem { else result.setDateDayMatchGroup(dateDay.getMatchGroup()); + result.setFallback(isFallback); + return result; } public SpannableString getTestMatch() {