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