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.
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.
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/>.
18 package net.ktnx.mobileledger.model;
20 import java.util.ArrayList;
21 import java.util.List;
22 import java.util.regex.Matcher;
23 import java.util.regex.Pattern;
25 import androidx.annotation.NonNull;
27 public class LedgerAccount {
28 static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
30 private String shortName;
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;
39 public LedgerAccount(String name) {
44 public LedgerAccount(String name, float amount) {
46 this.hiddenByStar = false;
48 this.amounts = new ArrayList<LedgerAmount>();
49 this.addAmount(amount);
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;
57 if (level == 0) return true;
59 return isVisible(Data.accounts.get());
61 public boolean isVisible(ArrayList<LedgerAccount> list) {
62 for (LedgerAccount acc : list) {
63 if (acc.isParentOf(this)) {
64 if (!acc.isExpanded()) return false;
69 public boolean isParentOf(LedgerAccount potentialChild) {
70 return potentialChild.getName().startsWith(name + ":");
72 public boolean isHiddenByStar() {
75 public void setHiddenByStar(boolean hiddenByStar) {
76 this.hiddenByStar = hiddenByStar;
78 private void stripName() {
81 StringBuilder parentBuilder = new StringBuilder();
83 Matcher m = reHigherAccount.matcher(shortName);
86 parentBuilder.append(m.group(0));
87 shortName = m.replaceFirst("");
91 if (parentBuilder.length() > 0)
92 parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
93 else parentName = null;
95 public String getName() {
98 public void setName(String name) {
102 public void addAmount(float amount, String currency) {
103 if (amounts == null) amounts = new ArrayList<>();
104 amounts.add(new LedgerAmount(amount, currency));
106 public void addAmount(float amount) {
107 this.addAmount(amount, null);
110 public String getAmountsString() {
111 if ((amounts == null) || amounts.isEmpty()) return "";
113 StringBuilder builder = new StringBuilder();
114 for (LedgerAmount amount : amounts) {
115 String amt = amount.toString();
116 if (builder.length() > 0) builder.append('\n');
120 return builder.toString();
123 public int getLevel() {
128 public String getShortName() {
132 public String getParentName() {
135 public void togglehidden() {
136 hiddenByStar = !hiddenByStar;
139 public boolean isHiddenByStarToBe() {
140 return hiddenByStarToBe;
142 public void setHiddenByStarToBe(boolean hiddenByStarToBe) {
143 this.hiddenByStarToBe = hiddenByStarToBe;
145 public void toggleHiddenToBe() {
146 setHiddenByStarToBe(!hiddenByStarToBe);
148 public boolean hasSubAccounts() {
149 return hasSubAccounts;
151 public void setHasSubAccounts(boolean hasSubAccounts) {
152 this.hasSubAccounts = hasSubAccounts;
154 public boolean isExpanded() {
157 public void setExpanded(boolean expanded) {
158 this.expanded = expanded;
160 public void toggleExpanded() {
161 expanded = !expanded;
163 public void removeAmounts() {
164 if (amounts != null) amounts.clear();