X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FLedgerAccount.java;h=3bc01d1fecd852cc957aeb205640465dec80320f;hb=d58f8f4a9edd8b96005d1900c51b589471424165;hp=0751d10ac4aec966b6d289f4fccdcc78f43fa9b3;hpb=010406b05a5a4f8447af34187afef8c1e78a4552;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 0751d10a..3bc01d1f 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 © 2019 Damyan Ivanov. + * Copyright © 2020 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 @@ -17,48 +17,63 @@ package net.ktnx.mobileledger.model; +import androidx.annotation.NonNull; +import androidx.annotation.Nullable; + +import java.lang.ref.WeakReference; import java.util.ArrayList; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -import androidx.annotation.NonNull; - public class LedgerAccount { static Pattern reHigherAccount = Pattern.compile("^[^:]+:"); private String name; private String shortName; private int level; private String parentName; - private boolean hiddenByStar; private boolean hiddenByStarToBe; private boolean expanded; private List amounts; private boolean hasSubAccounts; + private boolean amountsExpanded; + private WeakReference profileWeakReference; - public LedgerAccount(String name) { + public LedgerAccount(MobileLedgerProfile profile, String name) { + this.profileWeakReference = new WeakReference<>(profile); this.setName(name); - hiddenByStar = false; } - public LedgerAccount(String name, float amount) { + public LedgerAccount(MobileLedgerProfile profile, String name, float amount) { + this.profileWeakReference = new WeakReference<>(profile); this.setName(name); - this.hiddenByStar = false; this.expanded = true; this.amounts = new ArrayList(); this.addAmount(amount); } + public @Nullable MobileLedgerProfile getProfile() { + return profileWeakReference.get(); + } + @Override + public int hashCode() { + return name.hashCode(); + } + @Override + public boolean equals(@Nullable Object obj) { + if (obj == null) return false; + + return obj.getClass().equals(this.getClass()) && + name.equals(((LedgerAccount) obj).getName()); + } // an account is visible if: // - it is starred (not hidden by a star) // - and it has an expanded parent or is a top account public boolean isVisible() { - if (hiddenByStar) return false; - if (level == 0) return true; - return isVisible(Data.accounts.get()); + return isVisible(Data.accounts); } - public boolean isVisible(ArrayList list) { + public boolean isVisible(List list) { for (LedgerAccount acc : list) { if (acc.isParentOf(this)) { if (!acc.isExpanded()) return false; @@ -69,12 +84,6 @@ public class LedgerAccount { public boolean isParentOf(LedgerAccount potentialChild) { return potentialChild.getName().startsWith(name + ":"); } - public boolean isHiddenByStar() { - return hiddenByStar; - } - public void setHiddenByStar(boolean hiddenByStar) { - this.hiddenByStar = hiddenByStar; - } private void stripName() { level = 0; shortName = name; @@ -106,7 +115,7 @@ public class LedgerAccount { public void addAmount(float amount) { this.addAmount(amount, null); } - + public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; } public String getAmountsString() { if ((amounts == null) || amounts.isEmpty()) return ""; @@ -119,7 +128,21 @@ public class LedgerAccount { return builder.toString(); } + public String getAmountsString(int limit) { + if ((amounts == null) || amounts.isEmpty()) return ""; + int included = 0; + StringBuilder builder = new StringBuilder(); + for (LedgerAmount amount : amounts) { + String amt = amount.toString(); + if (builder.length() > 0) builder.append('\n'); + builder.append(amt); + included++; + if (included == limit) break; + } + + return builder.toString(); + } public int getLevel() { return level; } @@ -132,10 +155,6 @@ public class LedgerAccount { public String getParentName() { return parentName; } - public void togglehidden() { - hiddenByStar = !hiddenByStar; - } - public boolean isHiddenByStarToBe() { return hiddenByStarToBe; } @@ -161,6 +180,10 @@ public class LedgerAccount { expanded = !expanded; } public void removeAmounts() { - amounts.clear(); + if (amounts != null) amounts.clear(); } + public boolean amountsExpanded() { return amountsExpanded; } + public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; } + public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; } + }