]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
store a weak reference to the profile in the account object
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerAccount.java
1 /*
2  * Copyright © 2020 Damyan Ivanov.
3  * This file is part of MoLe.
4  * MoLe is free software: you can distribute it and/or modify it
5  * under the term of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your opinion), any later version.
8  *
9  * MoLe is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License terms for details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
16  */
17
18 package net.ktnx.mobileledger.model;
19
20 import androidx.annotation.NonNull;
21 import androidx.annotation.Nullable;
22
23 import java.lang.ref.WeakReference;
24 import java.util.ArrayList;
25 import java.util.List;
26 import java.util.regex.Matcher;
27 import java.util.regex.Pattern;
28
29 public class LedgerAccount {
30     static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
31     private String name;
32     private String shortName;
33     private int level;
34     private String parentName;
35     private boolean hiddenByStarToBe;
36     private boolean expanded;
37     private List<LedgerAmount> amounts;
38     private boolean hasSubAccounts;
39     private boolean amountsExpanded;
40     private WeakReference<MobileLedgerProfile> profileWeakReference;
41
42     public LedgerAccount(MobileLedgerProfile profile, String name) {
43         this.profileWeakReference = new WeakReference<>(profile);
44         this.setName(name);
45     }
46
47     public LedgerAccount(MobileLedgerProfile profile, String name, float amount) {
48         this.profileWeakReference = new WeakReference<>(profile);
49         this.setName(name);
50         this.expanded = true;
51         this.amounts = new ArrayList<LedgerAmount>();
52         this.addAmount(amount);
53     }
54     public @Nullable MobileLedgerProfile getProfile() {
55         return profileWeakReference.get();
56     }
57     @Override
58     public int hashCode() {
59         return name.hashCode();
60     }
61     @Override
62     public boolean equals(@Nullable Object obj) {
63         if (obj == null) return false;
64
65         return obj.getClass().equals(this.getClass()) &&
66                name.equals(((LedgerAccount) obj).getName());
67     }
68     // an account is visible if:
69     //  - it is starred (not hidden by a star)
70     //  - and it has an expanded parent or is a top account
71     public boolean isVisible() {
72         if (level == 0) return true;
73
74         return isVisible(Data.accounts);
75     }
76     public boolean isVisible(List<LedgerAccount> list) {
77         for (LedgerAccount acc : list) {
78             if (acc.isParentOf(this)) {
79                 if (!acc.isExpanded()) return false;
80             }
81         }
82         return true;
83     }
84     public boolean isParentOf(LedgerAccount potentialChild) {
85         return potentialChild.getName().startsWith(name + ":");
86     }
87     private void stripName() {
88         level = 0;
89         shortName = name;
90         StringBuilder parentBuilder = new StringBuilder();
91         while (true) {
92             Matcher m = reHigherAccount.matcher(shortName);
93             if (m.find()) {
94                 level++;
95                 parentBuilder.append(m.group(0));
96                 shortName = m.replaceFirst("");
97             }
98             else break;
99         }
100         if (parentBuilder.length() > 0)
101             parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
102         else parentName = null;
103     }
104     public String getName() {
105         return name;
106     }
107     public void setName(String name) {
108         this.name = name;
109         stripName();
110     }
111     public void addAmount(float amount, String currency) {
112         if (amounts == null) amounts = new ArrayList<>();
113         amounts.add(new LedgerAmount(amount, currency));
114     }
115     public void addAmount(float amount) {
116         this.addAmount(amount, null);
117     }
118     public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
119     public String getAmountsString() {
120         if ((amounts == null) || amounts.isEmpty()) return "";
121
122         StringBuilder builder = new StringBuilder();
123         for (LedgerAmount amount : amounts) {
124             String amt = amount.toString();
125             if (builder.length() > 0) builder.append('\n');
126             builder.append(amt);
127         }
128
129         return builder.toString();
130     }
131     public String getAmountsString(int limit) {
132         if ((amounts == null) || amounts.isEmpty()) return "";
133
134         int included = 0;
135         StringBuilder builder = new StringBuilder();
136         for (LedgerAmount amount : amounts) {
137             String amt = amount.toString();
138             if (builder.length() > 0) builder.append('\n');
139             builder.append(amt);
140             included++;
141             if (included == limit) break;
142         }
143
144         return builder.toString();
145     }
146     public int getLevel() {
147         return level;
148     }
149
150     @NonNull
151     public String getShortName() {
152         return shortName;
153     }
154
155     public String getParentName() {
156         return parentName;
157     }
158     public boolean isHiddenByStarToBe() {
159         return hiddenByStarToBe;
160     }
161     public void setHiddenByStarToBe(boolean hiddenByStarToBe) {
162         this.hiddenByStarToBe = hiddenByStarToBe;
163     }
164     public void toggleHiddenToBe() {
165         setHiddenByStarToBe(!hiddenByStarToBe);
166     }
167     public boolean hasSubAccounts() {
168         return hasSubAccounts;
169     }
170     public void setHasSubAccounts(boolean hasSubAccounts) {
171         this.hasSubAccounts = hasSubAccounts;
172     }
173     public boolean isExpanded() {
174         return expanded;
175     }
176     public void setExpanded(boolean expanded) {
177         this.expanded = expanded;
178     }
179     public void toggleExpanded() {
180         expanded = !expanded;
181     }
182     public void removeAmounts() {
183         if (amounts != null) amounts.clear();
184     }
185     public boolean amountsExpanded() { return amountsExpanded; }
186     public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
187     public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }
188
189 }