]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
no direct interface to ObservableList's value
[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
40     public LedgerAccount(String name) {
41         this.setName(name);
42         hiddenByStar = false;
43     }
44
45     public LedgerAccount(String name, float amount) {
46         this.setName(name);
47         this.hiddenByStar = false;
48         this.expanded = true;
49         this.amounts = new ArrayList<LedgerAmount>();
50         this.addAmount(amount);
51     }
52     @Override
53     public int hashCode() {
54         return name.hashCode();
55     }
56     @Override
57     public boolean equals(@Nullable Object obj) {
58         if (obj == null) return false;
59
60         return obj.getClass().equals(this.getClass()) &&
61                name.equals(((LedgerAccount) obj).getName());
62     }
63     // an account is visible if:
64     //  - it is starred (not hidden by a star)
65     //  - and it has an expanded parent or is a top account
66     public boolean isVisible() {
67         if (hiddenByStar) return false;
68
69         if (level == 0) return true;
70
71         return isVisible(Data.accounts);
72     }
73     public boolean isVisible(List<LedgerAccount> list) {
74         for (LedgerAccount acc : list) {
75             if (acc.isParentOf(this)) {
76                 if (!acc.isExpanded()) return false;
77             }
78         }
79         return true;
80     }
81     public boolean isParentOf(LedgerAccount potentialChild) {
82         return potentialChild.getName().startsWith(name + ":");
83     }
84     public boolean isHiddenByStar() {
85         return hiddenByStar;
86     }
87     public void setHiddenByStar(boolean hiddenByStar) {
88         this.hiddenByStar = hiddenByStar;
89     }
90     private void stripName() {
91         level = 0;
92         shortName = name;
93         StringBuilder parentBuilder = new StringBuilder();
94         while (true) {
95             Matcher m = reHigherAccount.matcher(shortName);
96             if (m.find()) {
97                 level++;
98                 parentBuilder.append(m.group(0));
99                 shortName = m.replaceFirst("");
100             }
101             else break;
102         }
103         if (parentBuilder.length() > 0)
104             parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
105         else parentName = null;
106     }
107     public String getName() {
108         return name;
109     }
110     public void setName(String name) {
111         this.name = name;
112         stripName();
113     }
114     public void addAmount(float amount, String currency) {
115         if (amounts == null) amounts = new ArrayList<>();
116         amounts.add(new LedgerAmount(amount, currency));
117     }
118     public void addAmount(float amount) {
119         this.addAmount(amount, null);
120     }
121
122     public String getAmountsString() {
123         if ((amounts == null) || amounts.isEmpty()) return "";
124
125         StringBuilder builder = new StringBuilder();
126         for (LedgerAmount amount : amounts) {
127             String amt = amount.toString();
128             if (builder.length() > 0) builder.append('\n');
129             builder.append(amt);
130         }
131
132         return builder.toString();
133     }
134
135     public int getLevel() {
136         return level;
137     }
138
139     @NonNull
140     public String getShortName() {
141         return shortName;
142     }
143
144     public String getParentName() {
145         return parentName;
146     }
147     public void togglehidden() {
148         hiddenByStar = !hiddenByStar;
149     }
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 }