]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
19a33ad6166f2f6f2231d619d9b02ddd28550c36
[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 expanded;
36     private List<LedgerAmount> amounts;
37     private boolean hasSubAccounts;
38     private boolean amountsExpanded;
39     private WeakReference<MobileLedgerProfile> profileWeakReference;
40
41     public LedgerAccount(MobileLedgerProfile profile, String name) {
42         this.profileWeakReference = new WeakReference<>(profile);
43         this.setName(name);
44     }
45
46     public LedgerAccount(MobileLedgerProfile profile, String name, float amount) {
47         this.profileWeakReference = new WeakReference<>(profile);
48         this.setName(name);
49         this.expanded = true;
50         this.amounts = new ArrayList<>();
51         this.addAmount(amount);
52     }
53     public @Nullable MobileLedgerProfile getProfile() {
54         return profileWeakReference.get();
55     }
56     @Override
57     public int hashCode() {
58         return name.hashCode();
59     }
60     @Override
61     public boolean equals(@Nullable Object obj) {
62         if (obj == null)
63             return false;
64
65         return obj.getClass()
66                   .equals(this.getClass()) && 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())
74                     return false;
75             }
76         }
77         return true;
78     }
79     public boolean isParentOf(LedgerAccount potentialChild) {
80         return potentialChild.getName()
81                              .startsWith(name + ":");
82     }
83     private void stripName() {
84         level = 0;
85         shortName = name;
86         StringBuilder parentBuilder = new StringBuilder();
87         while (true) {
88             Matcher m = reHigherAccount.matcher(shortName);
89             if (m.find()) {
90                 level++;
91                 parentBuilder.append(m.group(0));
92                 shortName = m.replaceFirst("");
93             }
94             else
95                 break;
96         }
97         if (parentBuilder.length() > 0)
98             parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
99         else
100             parentName = null;
101     }
102     public String getName() {
103         return name;
104     }
105     public void setName(String name) {
106         this.name = name;
107         stripName();
108     }
109     public void addAmount(float amount, @NonNull String currency) {
110         if (amounts == null)
111             amounts = new ArrayList<>();
112         amounts.add(new LedgerAmount(amount, currency));
113     }
114     public void addAmount(float amount) {
115         this.addAmount(amount, "");
116     }
117     public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
118     public String getAmountsString() {
119         if ((amounts == null) || amounts.isEmpty())
120             return "";
121
122         StringBuilder builder = new StringBuilder();
123         for (LedgerAmount amount : amounts) {
124             String amt = amount.toString();
125             if (builder.length() > 0)
126                 builder.append('\n');
127             builder.append(amt);
128         }
129
130         return builder.toString();
131     }
132     public String getAmountsString(int limit) {
133         if ((amounts == null) || amounts.isEmpty())
134             return "";
135
136         int included = 0;
137         StringBuilder builder = new StringBuilder();
138         for (LedgerAmount amount : amounts) {
139             String amt = amount.toString();
140             if (builder.length() > 0)
141                 builder.append('\n');
142             builder.append(amt);
143             included++;
144             if (included == limit)
145                 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 boolean hasSubAccounts() {
163         return hasSubAccounts;
164     }
165     public void setHasSubAccounts(boolean hasSubAccounts) {
166         this.hasSubAccounts = hasSubAccounts;
167     }
168     public boolean isExpanded() {
169         return expanded;
170     }
171     public void setExpanded(boolean expanded) {
172         this.expanded = expanded;
173     }
174     public void toggleExpanded() {
175         expanded = !expanded;
176     }
177     public void removeAmounts() {
178         if (amounts != null)
179             amounts.clear();
180     }
181     public boolean amountsExpanded() { return amountsExpanded; }
182     public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
183     public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }
184
185 }