X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FLedgerAccount.java;h=7fb38649a1e46571515917e59fcbabf4a2811950;hb=bb789332571609eeb1bef6e39b7ad359227d1045;hp=4cf08f9ebf01b114ffd47a5a181ab6ebafafcc77;hpb=6b740c280c79b0170321f533747cdbfc3e179a29;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 4cf08f9e..7fb38649 100644
--- a/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
+++ b/app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
@@ -1,107 +1,155 @@
/*
- * Copyright © 2018 Damyan Ivanov.
- * This file is part of Mobile-Ledger.
- * Mobile-Ledger is free software: you can distribute it and/or modify it
+ * Copyright © 2020 Damyan Ivanov.
+ * 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 androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;
-import java.util.regex.Matcher;
import java.util.regex.Pattern;
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 final LedgerAccount parent;
+ private boolean expanded;
private List amounts;
- static Pattern higher_account = Pattern.compile("^[^:]+:");
-
- public LedgerAccount(String name) {
+ private boolean hasSubAccounts;
+ private boolean amountsExpanded;
+ private final WeakReference profileWeakReference;
+
+ public LedgerAccount(MobileLedgerProfile profile, String name, @Nullable LedgerAccount parent) {
+ this.profileWeakReference = new WeakReference<>(profile);
+ this.parent = parent;
+ if (parent != null && !name.startsWith(parent.getName() + ":"))
+ throw new IllegalStateException(
+ String.format("Account name '%s' doesn't match parent account '%s'", name,
+ parent.getName()));
this.setName(name);
- hidden = false;
}
-
- public boolean isHidden() {
- return hidden;
+ @Nullable
+ public static String extractParentName(@NonNull String accName) {
+ int colonPos = accName.lastIndexOf(':');
+ if (colonPos < 0)
+ return null; // no parent account -- this is a top-level account
+ else
+ return accName.substring(0, colonPos);
}
-
- public void setHidden(boolean hidden) {
- this.hidden = hidden;
+ public @Nullable
+ MobileLedgerProfile getProfile() {
+ return profileWeakReference.get();
}
-
- public LedgerAccount(String name, float amount) {
- this.setName(name);
- this.hidden = false;
- 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();
+ if (!(obj instanceof LedgerAccount))
+ return false;
+
+ LedgerAccount acc = (LedgerAccount) obj;
+ if (!name.equals(acc.name))
+ return false;
+
+ if (!getAmountsString().equals(acc.getAmountsString()))
+ return false;
+
+ return expanded == acc.expanded && amountsExpanded == acc.amountsExpanded;
}
+ // an account is visible if:
+ // - it has an expanded visible parent or is a top account
+ public boolean isVisible() {
+ if (parent == null)
+ return true;
+ return (parent.isExpanded() && parent.isVisible());
+ }
+ public boolean isParentOf(LedgerAccount potentialChild) {
+ return potentialChild.getName()
+ .startsWith(name + ":");
+ }
private void stripName() {
- level = 0;
- shortName = name;
- StringBuilder parentBuilder = new StringBuilder();
- while (true) {
- Matcher m = higher_account.matcher(shortName);
- if (m.find()) {
- level++;
- parentBuilder.append(m.group(0));
- shortName = m.replaceFirst("");
- }
- else break;
+ if (parent == null) {
+ level = 0;
+ shortName = name;
+ }
+ else {
+ level = parent.level + 1;
+ shortName = name.substring(parent.getName()
+ .length() + 1);
}
- if (parentBuilder.length() > 0)
- parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
- else parentName = null;
}
-
public String getName() {
return name;
}
-
- public void addAmount(float amount, String currency) {
- if (amounts == null ) amounts = new ArrayList<>();
+ public void setName(String name) {
+ this.name = name;
+ stripName();
+ }
+ public void addAmount(float amount, @NonNull String currency) {
+ if (amounts == null)
+ amounts = new ArrayList<>();
amounts.add(new LedgerAmount(amount, currency));
}
public void addAmount(float amount) {
- this.addAmount(amount, null);
+ this.addAmount(amount, "");
}
-
+ public int getAmountCount() { return (amounts != null) ? amounts.size() : 0; }
public String getAmountsString() {
- if ((amounts == null) || amounts.isEmpty()) return "";
+ 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');
+ if (builder.length() > 0)
+ builder.append('\n');
builder.append(amt);
}
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;
}
@@ -112,19 +160,36 @@ public class LedgerAccount {
}
public String getParentName() {
- return parentName;
+ return (parent == null) ? null : parent.getName();
}
- public void togglehidden() {
- hidden = !hidden;
+ public boolean hasSubAccounts() {
+ return hasSubAccounts;
}
-
- public boolean isHiddenToBe() {
- return hiddenToBe;
+ public void setHasSubAccounts(boolean hasSubAccounts) {
+ this.hasSubAccounts = hasSubAccounts;
+ }
+ public boolean isExpanded() {
+ return expanded;
}
- public void setHiddenToBe(boolean hiddenToBe) {
- this.hiddenToBe = hiddenToBe;
+ 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; }
+
+ public void propagateAmountsTo(LedgerAccount acc) {
+ for (LedgerAmount a : amounts)
+ a.propagateToAccount(acc);
}
- public void toggleHiddenToBe() {
- setHiddenToBe(!hiddenToBe);
+ public List getAmounts() {
+ return amounts;
}
}