X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FLedgerAccount.java;h=2aa558bbb599a0a3af99817d0fdd7bf7d7dad16b;hb=1a8e78dc629aca59604408ee6a1078761ef9ea45;hp=a762e3a9dba1d8a3dc295d605b3ee73bb56fbec7;hpb=29fa90b17cbb87f0b16f3607f0628fe0057d6560;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 a762e3a9..2aa558bb 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,14 +17,15 @@ 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; -import androidx.annotation.Nullable; - public class LedgerAccount { static Pattern reHigherAccount = Pattern.compile("^[^:]+:"); private String name; @@ -36,46 +37,49 @@ public class LedgerAccount { 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); } - public LedgerAccount(String name, float amount) { + public LedgerAccount(MobileLedgerProfile profile, String name, float amount) { + this.profileWeakReference = new WeakReference<>(profile); this.setName(name); this.expanded = true; - this.amounts = new ArrayList(); + 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; + if (obj == null) + return false; - return obj.getClass().equals(this.getClass()) && - name.equals(((LedgerAccount) obj).getName()); + 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 (level == 0) return true; - - return isVisible(Data.accounts); - } + // - 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; + if (!acc.isExpanded()) + return false; } } return true; } public boolean isParentOf(LedgerAccount potentialChild) { - return potentialChild.getName().startsWith(name + ":"); + return potentialChild.getName() + .startsWith(name + ":"); } private void stripName() { level = 0; @@ -88,11 +92,13 @@ public class LedgerAccount { parentBuilder.append(m.group(0)); shortName = m.replaceFirst(""); } - else break; + else + break; } if (parentBuilder.length() > 0) parentName = parentBuilder.substring(0, parentBuilder.length() - 1); - else parentName = null; + else + parentName = null; } public String getName() { return name; @@ -101,37 +107,43 @@ public class LedgerAccount { this.name = name; stripName(); } - public void addAmount(float amount, String currency) { - if (amounts == null) amounts = new ArrayList<>(); + public void addAmount(float amount, @NonNull String currency) { + if (amounts == null) + amounts = new ArrayList<>(); amounts.add(new LedgerAmount(amount, currency)); } public void addAmount(float amount) { - this.addAmount(amount, null); + this.addAmount(amount, ""); } public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; } public String getAmountsString() { - if ((amounts == null) || amounts.isEmpty()) return ""; + if ((amounts == null) || amounts.isEmpty()) + return ""; StringBuilder builder = new StringBuilder(); for (LedgerAmount amount : amounts) { String amt = amount.toString(); - if (builder.length() > 0) builder.append('\n'); + if (builder.length() > 0) + builder.append('\n'); builder.append(amt); } return builder.toString(); } public String getAmountsString(int limit) { - if ((amounts == null) || amounts.isEmpty()) return ""; + 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'); + if (builder.length() > 0) + builder.append('\n'); builder.append(amt); included++; - if (included == limit) break; + if (included == limit) + break; } return builder.toString(); @@ -173,7 +185,8 @@ public class LedgerAccount { expanded = !expanded; } public void removeAmounts() { - if (amounts != null) amounts.clear(); + if (amounts != null) + amounts.clear(); } public boolean amountsExpanded() { return amountsExpanded; } public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }