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