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