]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/model/LedgerAccount.java
more pronounced day/month delimiters in the transaction list
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / model / LedgerAccount.java
index 6f3b21ac236728f8bfb46569b4a5f80c47455033..c2e62772f5990ef911854c838218edde70e50f82 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019 Damyan Ivanov.
+ * Copyright © 2024 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
 
 package net.ktnx.mobileledger.model;
 
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
+import net.ktnx.mobileledger.db.Account;
+import net.ktnx.mobileledger.db.AccountValue;
+import net.ktnx.mobileledger.db.AccountWithAmounts;
+
 import java.util.ArrayList;
 import java.util.List;
-import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-import androidx.annotation.NonNull;
-
 public class LedgerAccount {
+    private static final char ACCOUNT_DELIMITER = ':';
+    static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
+    private final LedgerAccount parent;
+    private long dbId;
+    private long profileId;
     private String name;
     private String shortName;
     private int level;
-    private String parentName;
-    private boolean hidden;
-    private boolean hiddenToBe;
+    private boolean expanded;
     private List<LedgerAmount> amounts;
-    static Pattern reHigherAccount = Pattern.compile("^[^:]+:");
-
-    public LedgerAccount(String name) {
+    private boolean hasSubAccounts;
+    private boolean amountsExpanded;
+
+    public LedgerAccount(String name, @Nullable LedgerAccount parent) {
+        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(ACCOUNT_DELIMITER);
+        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 static boolean isParentOf(@NonNull String possibleParent, @NonNull String accountName) {
+        return accountName.startsWith(possibleParent + ':');
     }
+    @NonNull
+    static public LedgerAccount fromDBO(AccountWithAmounts in, LedgerAccount parent) {
+        LedgerAccount res = new LedgerAccount(in.account.getName(), parent);
+        res.dbId = in.account.getId();
+        res.profileId = in.account.getProfileId();
+        res.setName(in.account.getName());
+        res.setExpanded(in.account.isExpanded());
+        res.setAmountsExpanded(in.account.isAmountsExpanded());
+
+        res.amounts = new ArrayList<>();
+        for (AccountValue val : in.amounts) {
+            res.amounts.add(new LedgerAmount(val.getValue(), val.getCurrency()));
+        }
 
-    public LedgerAccount(String name, float amount) {
-        this.setName(name);
-        this.hidden = false;
-        this.amounts = new ArrayList<LedgerAmount>();
-        this.addAmount(amount);
+        return res;
+    }
+    public static int determineLevel(String accName) {
+        int level = 0;
+        int delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER);
+        while (delimiterPosition >= 0) {
+            level++;
+            delimiterPosition = accName.indexOf(ACCOUNT_DELIMITER, delimiterPosition + 1);
+        }
+        return level;
     }
+    @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 = reHigherAccount.matcher(shortName);
-            if (m.find()) {
-                level++;
-                parentBuilder.append(m.group(0));
-                shortName = m.replaceFirst("");
-            }
-            else break;
-        }
-        if (parentBuilder.length() > 0)
-            parentName = parentBuilder.substring(0, parentBuilder.length() - 1);
-        else parentName = null;
+        String[] split = name.split(":");
+        shortName = split[split.length - 1];
+        level = split.length - 1;
     }
-
     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;
     }
-
     @NonNull
     public String getShortName() {
         return shortName;
     }
-
     public String getParentName() {
-        return parentName;
+        return (parent == null) ? null : parent.getName();
     }
-    public void togglehidden() {
-        hidden = !hidden;
+    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;}
+    public void propagateAmountsTo(LedgerAccount acc) {
+        for (LedgerAmount a : amounts)
+            a.propagateToAccount(acc);
+    }
+    public boolean allAmountsAreZero() {
+        for (LedgerAmount a : amounts) {
+            if (a.getAmount() != 0)
+                return false;
+        }
 
-    public boolean isHiddenToBe() {
-        return hiddenToBe;
+        return true;
     }
-    public void setHiddenToBe(boolean hiddenToBe) {
-        this.hiddenToBe = hiddenToBe;
+    public List<LedgerAmount> getAmounts() {
+        return amounts;
+    }
+    @NonNull
+    public Account toDBO() {
+        Account dbo = new Account();
+        dbo.setName(name);
+        dbo.setNameUpper(name.toUpperCase());
+        dbo.setParentName(extractParentName(name));
+        dbo.setLevel(level);
+        dbo.setId(dbId);
+        dbo.setProfileId(profileId);
+        dbo.setExpanded(expanded);
+        dbo.setAmountsExpanded(amountsExpanded);
+
+        return dbo;
+    }
+    @NonNull
+    public AccountWithAmounts toDBOWithAmounts() {
+        AccountWithAmounts dbo = new AccountWithAmounts();
+        dbo.account = toDBO();
+
+        dbo.amounts = new ArrayList<>();
+        for (LedgerAmount amt : getAmounts()) {
+            AccountValue val = new AccountValue();
+            val.setCurrency(amt.getCurrency());
+            val.setValue(amt.getAmount());
+            dbo.amounts.add(val);
+        }
+
+        return dbo;
     }
-    public void toggleHiddenToBe() {
-        setHiddenToBe(!hiddenToBe);
+    public long getId() {
+        return dbId;
     }
 }