]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
7510576a81a65fdd31792158514f40c474f8c8ef
[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.util.ArrayList;
24 import java.util.List;
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27
28 public class LedgerAccount {
29     static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
30     private String name;
31     private String shortName;
32     private int level;
33     private String parentName;
34     private boolean hiddenByStarToBe;
35     private boolean expanded;
36     private List<LedgerAmount> amounts;
37     private boolean hasSubAccounts;
38     private boolean amountsExpanded;
39
40     public LedgerAccount(String name) {
41         this.setName(name);
42     }
43
44     public LedgerAccount(String name, float amount) {
45         this.setName(name);
46         this.expanded = true;
47         this.amounts = new ArrayList<LedgerAmount>();
48         this.addAmount(amount);
49     }
50     @Override
51     public int hashCode() {
52         return name.hashCode();
53     }
54     @Override
55     public boolean equals(@Nullable Object obj) {
56         if (obj == null) return false;
57
58         return obj.getClass().equals(this.getClass()) &&
59                name.equals(((LedgerAccount) obj).getName());
60     }
61     // an account is visible if:
62     //  - it is starred (not hidden by a star)
63     //  - and it has an expanded parent or is a top account
64     public boolean isVisible() {
65         if (level == 0) return true;
66
67         return isVisible(Data.accounts);
68     }
69     public boolean isVisible(List<LedgerAccount> list) {
70         for (LedgerAccount acc : list) {
71             if (acc.isParentOf(this)) {
72                 if (!acc.isExpanded()) return false;
73             }
74         }
75         return true;
76     }
77     public boolean isParentOf(LedgerAccount potentialChild) {
78         return potentialChild.getName().startsWith(name + ":");
79     }
80     private void stripName() {
81         level = 0;
82         shortName = name;
83         StringBuilder parentBuilder = new StringBuilder();
84         while (true) {
85             Matcher m = reHigherAccount.matcher(shortName);
86             if (m.find()) {
87                 level++;
88                 parentBuilder.append(m.group(0));
89                 shortName = m.replaceFirst("");
90             }
91             else break;
92         }
93         if (parentBuilder.length() > 0)
94             parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
95         else parentName = null;
96     }
97     public String getName() {
98         return name;
99     }
100     public void setName(String name) {
101         this.name = name;
102         stripName();
103     }
104     public void addAmount(float amount, String currency) {
105         if (amounts == null) amounts = new ArrayList<>();
106         amounts.add(new LedgerAmount(amount, currency));
107     }
108     public void addAmount(float amount) {
109         this.addAmount(amount, null);
110     }
111     public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
112     public String getAmountsString() {
113         if ((amounts == null) || amounts.isEmpty()) return "";
114
115         StringBuilder builder = new StringBuilder();
116         for (LedgerAmount amount : amounts) {
117             String amt = amount.toString();
118             if (builder.length() > 0) builder.append('\n');
119             builder.append(amt);
120         }
121
122         return builder.toString();
123     }
124     public String getAmountsString(int limit) {
125         if ((amounts == null) || amounts.isEmpty()) return "";
126
127         int included = 0;
128         StringBuilder builder = new StringBuilder();
129         for (LedgerAmount amount : amounts) {
130             String amt = amount.toString();
131             if (builder.length() > 0) builder.append('\n');
132             builder.append(amt);
133             included++;
134             if (included == limit) break;
135         }
136
137         return builder.toString();
138     }
139     public int getLevel() {
140         return level;
141     }
142
143     @NonNull
144     public String getShortName() {
145         return shortName;
146     }
147
148     public String getParentName() {
149         return parentName;
150     }
151     public boolean isHiddenByStarToBe() {
152         return hiddenByStarToBe;
153     }
154     public void setHiddenByStarToBe(boolean hiddenByStarToBe) {
155         this.hiddenByStarToBe = hiddenByStarToBe;
156     }
157     public void toggleHiddenToBe() {
158         setHiddenByStarToBe(!hiddenByStarToBe);
159     }
160     public boolean hasSubAccounts() {
161         return hasSubAccounts;
162     }
163     public void setHasSubAccounts(boolean hasSubAccounts) {
164         this.hasSubAccounts = hasSubAccounts;
165     }
166     public boolean isExpanded() {
167         return expanded;
168     }
169     public void setExpanded(boolean expanded) {
170         this.expanded = expanded;
171     }
172     public void toggleExpanded() {
173         expanded = !expanded;
174     }
175     public void removeAmounts() {
176         if (amounts != null) amounts.clear();
177     }
178     public boolean amountsExpanded() { return amountsExpanded; }
179     public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
180     public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }
181
182 }