]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
rework account list management to be fully asynchronous
[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.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 LedgerAccount parent;
34     private boolean expanded;
35     private List<LedgerAmount> amounts;
36     private boolean hasSubAccounts;
37     private boolean amountsExpanded;
38     private WeakReference<MobileLedgerProfile> profileWeakReference;
39
40     public LedgerAccount(MobileLedgerProfile profile, String name, @Nullable LedgerAccount parent) {
41         this.profileWeakReference = new WeakReference<>(profile);
42         this.parent = parent;
43         if (parent != null && !name.startsWith(parent.getName() + ":"))
44             throw new IllegalStateException(
45                     String.format("Account name '%s' doesn't match parent account '%s'", name,
46                             parent.getName()));
47         this.setName(name);
48     }
49     @Nullable
50     public static String extractParentName(@NonNull String accName) {
51         int colonPos = accName.lastIndexOf(':');
52         if (colonPos < 0)
53             return null;    // no parent account -- this is a top-level account
54         else
55             return accName.substring(0, colonPos);
56     }
57     public @Nullable
58     MobileLedgerProfile getProfile() {
59         return profileWeakReference.get();
60     }
61     @Override
62     public int hashCode() {
63         return name.hashCode();
64     }
65     @Override
66     public boolean equals(@Nullable Object obj) {
67         if (obj == null)
68             return false;
69
70         if (!(obj instanceof LedgerAccount))
71             return false;
72
73         LedgerAccount acc = (LedgerAccount) obj;
74         if (!name.equals(acc.name))
75             return false;
76
77         if (!getAmountsString().equals(acc.getAmountsString()))
78             return false;
79
80         return expanded == acc.expanded && amountsExpanded == acc.amountsExpanded;
81     }
82     // an account is visible if:
83     //  - it has an expanded visible parent or is a top account
84     public boolean isVisible() {
85         if (parent == null)
86             return true;
87
88         return (parent.isExpanded() && parent.isVisible());
89     }
90     public boolean isParentOf(LedgerAccount potentialChild) {
91         return potentialChild.getName()
92                              .startsWith(name + ":");
93     }
94     private void stripName() {
95         if (parent == null) {
96             level = 0;
97             shortName = name;
98         }
99         else {
100             level = parent.level + 1;
101             shortName = name.substring(parent.getName()
102                                              .length() + 1);
103         }
104     }
105     public String getName() {
106         return name;
107     }
108     public void setName(String name) {
109         this.name = name;
110         stripName();
111     }
112     public void addAmount(float amount, @NonNull String currency) {
113         if (amounts == null)
114             amounts = new ArrayList<>();
115         amounts.add(new LedgerAmount(amount, currency));
116     }
117     public void addAmount(float amount) {
118         this.addAmount(amount, "");
119     }
120     public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
121     public String getAmountsString() {
122         if ((amounts == null) || amounts.isEmpty())
123             return "";
124
125         StringBuilder builder = new StringBuilder();
126         for (LedgerAmount amount : amounts) {
127             String amt = amount.toString();
128             if (builder.length() > 0)
129                 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())
137             return "";
138
139         int included = 0;
140         StringBuilder builder = new StringBuilder();
141         for (LedgerAmount amount : amounts) {
142             String amt = amount.toString();
143             if (builder.length() > 0)
144                 builder.append('\n');
145             builder.append(amt);
146             included++;
147             if (included == limit)
148                 break;
149         }
150
151         return builder.toString();
152     }
153     public int getLevel() {
154         return level;
155     }
156
157     @NonNull
158     public String getShortName() {
159         return shortName;
160     }
161
162     public String getParentName() {
163         return (parent == null) ? null : parent.getName();
164     }
165     public boolean hasSubAccounts() {
166         return hasSubAccounts;
167     }
168     public void setHasSubAccounts(boolean hasSubAccounts) {
169         this.hasSubAccounts = hasSubAccounts;
170     }
171     public boolean isExpanded() {
172         return expanded;
173     }
174     public void setExpanded(boolean expanded) {
175         this.expanded = expanded;
176     }
177     public void toggleExpanded() {
178         expanded = !expanded;
179     }
180     public void removeAmounts() {
181         if (amounts != null)
182             amounts.clear();
183     }
184     public boolean amountsExpanded() { return amountsExpanded; }
185     public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
186     public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }
187
188     public void propagateAmountsTo(LedgerAccount acc) {
189         for (LedgerAmount a : amounts)
190             a.propagateToAccount(acc);
191     }
192     public List<LedgerAmount> getAmounts() {
193         return amounts;
194     }
195 }