X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FAccountListItem.java;h=807e93d348b1e182d81090d94fc097b32cd4be8f;hb=HEAD;hp=a96a9a3e2f75c4e9da6bc300e760e47879a860a9;hpb=acdedb28266ec0bde0a3b6775837a70455c339fe;p=mobile-ledger.git diff --git a/app/src/main/java/net/ktnx/mobileledger/model/AccountListItem.java b/app/src/main/java/net/ktnx/mobileledger/model/AccountListItem.java index a96a9a3e..807e93d3 100644 --- a/app/src/main/java/net/ktnx/mobileledger/model/AccountListItem.java +++ b/app/src/main/java/net/ktnx/mobileledger/model/AccountListItem.java @@ -1,5 +1,5 @@ /* - * Copyright © 2021 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 @@ -18,11 +18,13 @@ package net.ktnx.mobileledger.model; import androidx.annotation.NonNull; +import androidx.lifecycle.LiveData; import org.jetbrains.annotations.NotNull; -public class AccountListItem { +public abstract class AccountListItem { private AccountListItem() {} + public abstract boolean sameContent(AccountListItem other); @NonNull public Type getType() { if (this instanceof Account) @@ -32,12 +34,19 @@ public class AccountListItem { else throw new RuntimeException("Unsupported sub-class " + this); } - @NotNull - public LedgerAccount getAccount() { - if (this instanceof Account) - return ((Account) this).account; - - throw new IllegalStateException(String.format("Item type is not Account, but %s", this)); + public boolean isAccount() { + return this instanceof Account; + } + public Account toAccount() { + assert isAccount(); + return ((Account) this); + } + public boolean isHeader() { + return this instanceof Header; + } + public Header toHeader() { + assert isHeader(); + return ((Header) this); } public enum Type {ACCOUNT, HEADER} @@ -46,10 +55,37 @@ public class AccountListItem { public Account(@NotNull LedgerAccount account) { this.account = account; } + @Override + public boolean sameContent(AccountListItem other) { + if (!(other instanceof Account)) + return false; + return ((Account) other).account.hasSubAccounts() == account.hasSubAccounts() && + ((Account) other).account.amountsExpanded() == account.amountsExpanded() && + ((Account) other).account.isExpanded() == account.isExpanded() && + ((Account) other).account.getLevel() == account.getLevel() && + ((Account) other).account.getAmountsString() + .equals(account.getAmountsString()); + } + @NotNull + public LedgerAccount getAccount() { + return account; + } + public boolean allAmountsAreZero() { + return account.allAmountsAreZero(); + } } public static class Header extends AccountListItem { - public Header() { + private final LiveData text; + public Header(@NonNull LiveData text) { + this.text = text; + } + public LiveData getText() { + return text; + } + @Override + public boolean sameContent(AccountListItem other) { + return true; } } }