X-Git-Url: https://git.ktnx.net/?a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fmodel%2FAccountListItem.java;h=dfd3d982011a105191f5e801bea2fcc5e276c4d6;hb=8098a8b37a4331b9faf6cf50a51a0d7aa9677421;hp=5fd951715eeaf609cf46358f909b18c9f1a052f9;hpb=2d85826653a8ba3e619afc83c5c91216a7fdb0b6;p=mobile-ledger-staging.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 5fd95171..dfd3d982 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 © 2020 Damyan Ivanov. + * Copyright © 2021 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,29 +18,60 @@ package net.ktnx.mobileledger.model; import androidx.annotation.NonNull; +import androidx.lifecycle.LiveData; import org.jetbrains.annotations.NotNull; -public class AccountListItem { - private final Type type; - private LedgerAccount account; - public AccountListItem(@NotNull LedgerAccount account) { - this.type = Type.ACCOUNT; - this.account = account; - } - public AccountListItem() { - this.type = Type.HEADER; - } +public abstract class AccountListItem { + private AccountListItem() {} + public abstract boolean sameContent(AccountListItem other); @NonNull public Type getType() { - return type; + if (this instanceof Account) + return Type.ACCOUNT; + else if (this instanceof Header) + return Type.HEADER; + else + throw new RuntimeException("Unsupported sub-class " + this); } @NotNull public LedgerAccount getAccount() { - if (type != Type.ACCOUNT) - throw new IllegalStateException( - String.format("Item type is not %s, but %s", Type.ACCOUNT, type)); - return account; + if (this instanceof Account) + return ((Account) this).account; + + throw new IllegalStateException(String.format("Item type is not Account, but %s", this)); } public enum Type {ACCOUNT, HEADER} + + public static class Account extends AccountListItem { + private final LedgerAccount account; + 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()); + } + } + + public static class Header extends AccountListItem { + 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; + } + } }