]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
whitespace
[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<>();
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)
64             return false;
65
66         return obj.getClass()
67                   .equals(this.getClass()) && name.equals(((LedgerAccount) obj).getName());
68     }
69     // an account is visible if:
70     //  - it has an expanded parent or is a top account
71     public boolean isVisible(List<LedgerAccount> list) {
72         for (LedgerAccount acc : list) {
73             if (acc.isParentOf(this)) {
74                 if (!acc.isExpanded())
75                     return false;
76             }
77         }
78         return true;
79     }
80     public boolean isParentOf(LedgerAccount potentialChild) {
81         return potentialChild.getName()
82                              .startsWith(name + ":");
83     }
84     private void stripName() {
85         level = 0;
86         shortName = name;
87         StringBuilder parentBuilder = new StringBuilder();
88         while (true) {
89             Matcher m = reHigherAccount.matcher(shortName);
90             if (m.find()) {
91                 level++;
92                 parentBuilder.append(m.group(0));
93                 shortName = m.replaceFirst("");
94             }
95             else
96                 break;
97         }
98         if (parentBuilder.length() > 0)
99             parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
100         else
101             parentName = null;
102     }
103     public String getName() {
104         return name;
105     }
106     public void setName(String name) {
107         this.name = name;
108         stripName();
109     }
110     public void addAmount(float amount, @NonNull String currency) {
111         if (amounts == null)
112             amounts = new ArrayList<>();
113         amounts.add(new LedgerAmount(amount, currency));
114     }
115     public void addAmount(float amount) {
116         this.addAmount(amount, "");
117     }
118     public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
119     public String getAmountsString() {
120         if ((amounts == null) || amounts.isEmpty())
121             return "";
122
123         StringBuilder builder = new StringBuilder();
124         for (LedgerAmount amount : amounts) {
125             String amt = amount.toString();
126             if (builder.length() > 0)
127                 builder.append('\n');
128             builder.append(amt);
129         }
130
131         return builder.toString();
132     }
133     public String getAmountsString(int limit) {
134         if ((amounts == null) || amounts.isEmpty())
135             return "";
136
137         int included = 0;
138         StringBuilder builder = new StringBuilder();
139         for (LedgerAmount amount : amounts) {
140             String amt = amount.toString();
141             if (builder.length() > 0)
142                 builder.append('\n');
143             builder.append(amt);
144             included++;
145             if (included == limit)
146                 break;
147         }
148
149         return builder.toString();
150     }
151     public int getLevel() {
152         return level;
153     }
154
155     @NonNull
156     public String getShortName() {
157         return shortName;
158     }
159
160     public String getParentName() {
161         return parentName;
162     }
163     public boolean isHiddenByStarToBe() {
164         return hiddenByStarToBe;
165     }
166     public void setHiddenByStarToBe(boolean hiddenByStarToBe) {
167         this.hiddenByStarToBe = hiddenByStarToBe;
168     }
169     public void toggleHiddenToBe() {
170         setHiddenByStarToBe(!hiddenByStarToBe);
171     }
172     public boolean hasSubAccounts() {
173         return hasSubAccounts;
174     }
175     public void setHasSubAccounts(boolean hasSubAccounts) {
176         this.hasSubAccounts = hasSubAccounts;
177     }
178     public boolean isExpanded() {
179         return expanded;
180     }
181     public void setExpanded(boolean expanded) {
182         this.expanded = expanded;
183     }
184     public void toggleExpanded() {
185         expanded = !expanded;
186     }
187     public void removeAmounts() {
188         if (amounts != null)
189             amounts.clear();
190     }
191     public boolean amountsExpanded() { return amountsExpanded; }
192     public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
193     public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }
194
195 }