X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FLedgerAccount.java;h=c76f2d564970193e29b37740f88a5ec86857c5d4;hb=8098a8b37a4331b9faf6cf50a51a0d7aa9677421;hp=2aa558bbb599a0a3af99817d0fdd7bf7d7dad16b;hpb=1a8e78dc629aca59604408ee6a1078761ef9ea45;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java index 2aa558bb..c76f2d56 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java @@ -1,5 +1,5 @@ /* - * Copyright © 2020 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -20,39 +20,58 @@ package net.ktnx.mobileledger.model; import androidx.annotation.NonNull; import androidx.annotation.Nullable; -import java.lang.ref.WeakReference; +import net.ktnx.mobileledger.db.Account; +import net.ktnx.mobileledger.db.AccountValue; +import net.ktnx.mobileledger.db.AccountWithAmounts; + import java.util.ArrayList; import java.util.List; -import java.util.regex.Matcher; import java.util.regex.Pattern; public class LedgerAccount { static Pattern reHigherAccount = Pattern.compile("^[^:]+:"); + private final LedgerAccount parent; + private long dbId; + private long profileId; private String name; private String shortName; private int level; - private String parentName; - private boolean hiddenByStarToBe; private boolean expanded; private List amounts; private boolean hasSubAccounts; private boolean amountsExpanded; - private WeakReference profileWeakReference; - public LedgerAccount(MobileLedgerProfile profile, String name) { - this.profileWeakReference = new WeakReference<>(profile); + public LedgerAccount(String name, @Nullable LedgerAccount parent) { + this.parent = parent; + if (parent != null && !name.startsWith(parent.getName() + ":")) + throw new IllegalStateException( + String.format("Account name '%s' doesn't match parent account '%s'", name, + parent.getName())); this.setName(name); } - - public LedgerAccount(MobileLedgerProfile profile, String name, float amount) { - this.profileWeakReference = new WeakReference<>(profile); - this.setName(name); - this.expanded = true; - this.amounts = new ArrayList<>(); - this.addAmount(amount); + @Nullable + public static String extractParentName(@NonNull String accName) { + int colonPos = accName.lastIndexOf(':'); + if (colonPos < 0) + return null; // no parent account -- this is a top-level account + else + return accName.substring(0, colonPos); } - public @Nullable MobileLedgerProfile getProfile() { - return profileWeakReference.get(); + @NonNull + static public LedgerAccount fromDBO(AccountWithAmounts in, LedgerAccount parent) { + LedgerAccount res = new LedgerAccount(in.account.getName(), parent); + res.dbId = in.account.getId(); + res.profileId = in.account.getProfileId(); + res.setName(in.account.getName()); + res.setExpanded(in.account.isExpanded()); + res.setAmountsExpanded(in.account.isAmountsExpanded()); + + res.amounts = new ArrayList<>(); + for (AccountValue val : in.amounts) { + res.amounts.add(new LedgerAmount(val.getValue(), val.getCurrency())); + } + + return res; } @Override public int hashCode() { @@ -63,42 +82,34 @@ public class LedgerAccount { if (obj == null) return false; - return obj.getClass() - .equals(this.getClass()) && name.equals(((LedgerAccount) obj).getName()); + if (!(obj instanceof LedgerAccount)) + return false; + + LedgerAccount acc = (LedgerAccount) obj; + if (!name.equals(acc.name)) + return false; + + if (!getAmountsString().equals(acc.getAmountsString())) + return false; + + return expanded == acc.expanded && amountsExpanded == acc.amountsExpanded; } // an account is visible if: - // - it has an expanded parent or is a top account - public boolean isVisible(List list) { - for (LedgerAccount acc : list) { - if (acc.isParentOf(this)) { - if (!acc.isExpanded()) - return false; - } - } - return true; + // - it has an expanded visible parent or is a top account + public boolean isVisible() { + if (parent == null) + return true; + + return (parent.isExpanded() && parent.isVisible()); } public boolean isParentOf(LedgerAccount potentialChild) { return potentialChild.getName() .startsWith(name + ":"); } private void stripName() { - level = 0; - shortName = name; - StringBuilder parentBuilder = new StringBuilder(); - while (true) { - Matcher m = reHigherAccount.matcher(shortName); - if (m.find()) { - level++; - parentBuilder.append(m.group(0)); - shortName = m.replaceFirst(""); - } - else - break; - } - if (parentBuilder.length() > 0) - parentName = parentBuilder.substring(0, parentBuilder.length() - 1); - else - parentName = null; + String[] split = name.split(":"); + shortName = split[split.length - 1]; + level = split.length - 1; } public String getName() { return name; @@ -151,23 +162,12 @@ public class LedgerAccount { public int getLevel() { return level; } - @NonNull public String getShortName() { return shortName; } - public String getParentName() { - return parentName; - } - public boolean isHiddenByStarToBe() { - return hiddenByStarToBe; - } - public void setHiddenByStarToBe(boolean hiddenByStarToBe) { - this.hiddenByStarToBe = hiddenByStarToBe; - } - public void toggleHiddenToBe() { - setHiddenByStarToBe(!hiddenByStarToBe); + return (parent == null) ? null : parent.getName(); } public boolean hasSubAccounts() { return hasSubAccounts; @@ -191,5 +191,28 @@ public class LedgerAccount { public boolean amountsExpanded() { return amountsExpanded; } public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; } public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; } + public void propagateAmountsTo(LedgerAccount acc) { + for (LedgerAmount a : amounts) + a.propagateToAccount(acc); + } + public List getAmounts() { + return amounts; + } + @NonNull + public Account toDBO() { + Account dbo = new Account(); + dbo.setName(name); + dbo.setNameUpper(name.toUpperCase()); + dbo.setParentName(extractParentName(name)); + dbo.setLevel(level); + dbo.setId(dbId); + dbo.setProfileId(profileId); + dbo.setExpanded(expanded); + dbo.setAmountsExpanded(amountsExpanded); + return dbo; + } + public long getId() { + return dbId; + } }