X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FLedgerAccount.java;h=256be6f082f7ee936f94943efde6782cf82386c3;hb=46e3f8b3ad1ff29ded16d506ef9ac9ce23bf3c71;hp=532642acddaf238e4393498539080ba966d33071;hpb=bde37d0aa472d31606b53491240c79af3374f09b;p=mobile-ledger.git
diff --git a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
index 532642ac..256be6f0 100644
--- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
+++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
@@ -1,64 +1,93 @@
/*
* Copyright © 2019 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * This file is part of MoLe.
+ * MoLe is free software: you can distribute it and/or modify it
* under the term of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your opinion), any later version.
*
- * Mobile-Ledger is distributed in the hope that it will be useful,
+ * MoLe is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License terms for details.
*
* You should have received a copy of the GNU General Public License
- * along with Mobile-Ledger. If not, see .
+ * along with MoLe. If not, see .
*/
package net.ktnx.mobileledger.model;
-import android.support.annotation.NonNull;
-
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
public class LedgerAccount {
+ static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
private String name;
private String shortName;
private int level;
private String parentName;
- private boolean hidden;
- private boolean hiddenToBe;
+ private boolean hiddenByStar;
+ private boolean hiddenByStarToBe;
+ private boolean expanded;
private List amounts;
- static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
+ private boolean hasSubAccounts;
+ private boolean amountsExpanded;
public LedgerAccount(String name) {
this.setName(name);
- hidden = false;
- }
-
- public boolean isHidden() {
- return hidden;
- }
-
- public void setHidden(boolean hidden) {
- this.hidden = hidden;
+ hiddenByStar = false;
}
public LedgerAccount(String name, float amount) {
this.setName(name);
- this.hidden = false;
+ this.hiddenByStar = false;
+ this.expanded = true;
this.amounts = new ArrayList();
this.addAmount(amount);
}
+ @Override
+ public int hashCode() {
+ return name.hashCode();
+ }
+ @Override
+ public boolean equals(@Nullable Object obj) {
+ if (obj == null) return false;
- public void setName(String name) {
- this.name = name;
- stripName();
+ return obj.getClass().equals(this.getClass()) &&
+ name.equals(((LedgerAccount) obj).getName());
}
+ // an account is visible if:
+ // - it is starred (not hidden by a star)
+ // - and it has an expanded parent or is a top account
+ public boolean isVisible() {
+ if (hiddenByStar) return false;
+
+ if (level == 0) return true;
+ return isVisible(Data.accounts);
+ }
+ public boolean isVisible(List list) {
+ for (LedgerAccount acc : list) {
+ if (acc.isParentOf(this)) {
+ if (!acc.isExpanded()) return false;
+ }
+ }
+ return true;
+ }
+ public boolean isParentOf(LedgerAccount potentialChild) {
+ return potentialChild.getName().startsWith(name + ":");
+ }
+ public boolean isHiddenByStar() {
+ return hiddenByStar;
+ }
+ public void setHiddenByStar(boolean hiddenByStar) {
+ this.hiddenByStar = hiddenByStar;
+ }
private void stripName() {
level = 0;
shortName = name;
@@ -76,24 +105,26 @@ public class LedgerAccount {
parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
else parentName = null;
}
-
public String getName() {
return name;
}
-
+ public void setName(String name) {
+ this.name = name;
+ stripName();
+ }
public void addAmount(float amount, String currency) {
- if (amounts == null ) amounts = new ArrayList<>();
+ if (amounts == null) amounts = new ArrayList<>();
amounts.add(new LedgerAmount(amount, currency));
}
public void addAmount(float amount) {
this.addAmount(amount, null);
}
-
+ public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
public String getAmountsString() {
if ((amounts == null) || amounts.isEmpty()) return "";
StringBuilder builder = new StringBuilder();
- for( LedgerAmount amount : amounts ) {
+ for (LedgerAmount amount : amounts) {
String amt = amount.toString();
if (builder.length() > 0) builder.append('\n');
builder.append(amt);
@@ -101,7 +132,21 @@ public class LedgerAccount {
return builder.toString();
}
+ public String getAmountsString(int limit) {
+ if ((amounts == null) || amounts.isEmpty()) return "";
+ int included = 0;
+ StringBuilder builder = new StringBuilder();
+ for (LedgerAmount amount : amounts) {
+ String amt = amount.toString();
+ if (builder.length() > 0) builder.append('\n');
+ builder.append(amt);
+ included++;
+ if (included == limit) break;
+ }
+
+ return builder.toString();
+ }
public int getLevel() {
return level;
}
@@ -115,16 +160,38 @@ public class LedgerAccount {
return parentName;
}
public void togglehidden() {
- hidden = !hidden;
+ hiddenByStar = !hiddenByStar;
}
- public boolean isHiddenToBe() {
- return hiddenToBe;
+ public boolean isHiddenByStarToBe() {
+ return hiddenByStarToBe;
}
- public void setHiddenToBe(boolean hiddenToBe) {
- this.hiddenToBe = hiddenToBe;
+ public void setHiddenByStarToBe(boolean hiddenByStarToBe) {
+ this.hiddenByStarToBe = hiddenByStarToBe;
}
public void toggleHiddenToBe() {
- setHiddenToBe(!hiddenToBe);
+ setHiddenByStarToBe(!hiddenByStarToBe);
+ }
+ public boolean hasSubAccounts() {
+ return hasSubAccounts;
+ }
+ public void setHasSubAccounts(boolean hasSubAccounts) {
+ this.hasSubAccounts = hasSubAccounts;
}
+ public boolean isExpanded() {
+ return expanded;
+ }
+ public void setExpanded(boolean expanded) {
+ this.expanded = expanded;
+ }
+ public void toggleExpanded() {
+ expanded = !expanded;
+ }
+ public void removeAmounts() {
+ if (amounts != null) amounts.clear();
+ }
+ public boolean amountsExpanded() { return amountsExpanded; }
+ public void setAmountsExpanded(boolean flag) { amountsExpanded = flag; }
+ public void toggleAmountsExpanded() { amountsExpanded = !amountsExpanded; }
+
}