]> git.ktnx.net Git - mobile-ledger.git/blobdiff - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
last update text also includes transaction count
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryAdapter.java
index 45a966676fde43b833f32fc41633128400285a23..cc6cff1878a336620ff0df206abf2fc82a1d0d34 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright © 2019 Damyan Ivanov.
+ * 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
@@ -19,192 +19,294 @@ package net.ktnx.mobileledger.ui.account_summary;
 
 import android.content.Context;
 import android.content.res.Resources;
-import android.graphics.Typeface;
+import android.text.TextUtils;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
-import android.widget.CheckBox;
-import android.widget.FrameLayout;
 import android.widget.ImageView;
 import android.widget.TextView;
 
+import androidx.annotation.NonNull;
+import androidx.appcompat.app.AlertDialog;
+import androidx.constraintlayout.widget.ConstraintLayout;
+import androidx.recyclerview.widget.AsyncListDiffer;
+import androidx.recyclerview.widget.DiffUtil;
+import androidx.recyclerview.widget.RecyclerView;
+
 import net.ktnx.mobileledger.R;
+import net.ktnx.mobileledger.async.DbOpQueue;
+import net.ktnx.mobileledger.model.AccountListItem;
 import net.ktnx.mobileledger.model.Data;
 import net.ktnx.mobileledger.model.LedgerAccount;
+import net.ktnx.mobileledger.model.MobileLedgerProfile;
+import net.ktnx.mobileledger.ui.MainModel;
 import net.ktnx.mobileledger.ui.activity.MainActivity;
-import net.ktnx.mobileledger.utils.LockHolder;
+import net.ktnx.mobileledger.utils.Locker;
 
-import androidx.annotation.NonNull;
-import androidx.appcompat.app.AlertDialog;
-import androidx.constraintlayout.widget.ConstraintLayout;
-import androidx.recyclerview.widget.RecyclerView;
+import org.jetbrains.annotations.NotNull;
+
+import java.util.List;
+import java.util.Locale;
+import java.util.Observer;
+
+import static net.ktnx.mobileledger.utils.Logger.debug;
 
 public class AccountSummaryAdapter
         extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
     public static final int AMOUNT_LIMIT = 3;
-    private boolean selectionActive;
-
-    AccountSummaryAdapter() {
-        this.selectionActive = false;
-    }
-
-    public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
-        try (LockHolder lh = Data.accounts.lockForReading()) {
-            if (position < Data.accounts.size()) {
-                LedgerAccount acc = Data.accounts.get(position);
-                Context ctx = holder.row.getContext();
-                Resources rm = ctx.getResources();
-
-                holder.row.setTag(acc);
-                holder.row.setVisibility(View.VISIBLE);
-                holder.vTrailer.setVisibility(View.GONE);
-                holder.tvAccountName.setText(acc.getShortName());
-                ConstraintLayout.LayoutParams lp =
-                        (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
-                lp.setMarginStart(
-                        acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 2);
-                holder.expanderContainer
-                        .setVisibility(acc.hasSubAccounts() ? View.VISIBLE : View.INVISIBLE);
-                holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
-                int amounts = acc.getAmountCount();
-                if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
-                    holder.tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
-                    holder.accountExpanderContainer.setVisibility(View.VISIBLE);
-                }
-                else {
-                    holder.tvAccountAmounts.setText(acc.getAmountsString());
-                    holder.accountExpanderContainer.setVisibility(View.GONE);
-                }
+    private final AsyncListDiffer<AccountListItem> listDiffer;
+    private final MainModel model;
+    AccountSummaryAdapter(MainModel model) {
+        this.model = model;
 
-                if (acc.isHiddenByStar()) {
-                    holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
-                    holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
-                }
-                else {
-                    holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
-                    holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
+        listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<AccountListItem>() {
+            @Override
+            public boolean areItemsTheSame(@NotNull AccountListItem oldItem,
+                                           @NotNull AccountListItem newItem) {
+                final AccountListItem.Type oldType = oldItem.getType();
+                final AccountListItem.Type newType = newItem.getType();
+                if (oldType == AccountListItem.Type.HEADER) {
+                    return newType == AccountListItem.Type.HEADER;
                 }
+                if (oldType != newType)
+                    return false;
 
-                holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
-                holder.selectionCb.setChecked(!acc.isHiddenByStarToBe());
-
-                holder.row.setTag(R.id.POS, position);
+                return TextUtils.equals(oldItem.getAccount()
+                                               .getName(), newItem.getAccount()
+                                                                  .getName());
             }
-            else {
-                holder.vTrailer.setVisibility(View.VISIBLE);
-                holder.row.setVisibility(View.GONE);
+            @Override
+            public boolean areContentsTheSame(@NotNull AccountListItem oldItem,
+                                              @NotNull AccountListItem newItem) {
+                if (oldItem.getType()
+                           .equals(AccountListItem.Type.HEADER))
+                    return true;
+                return oldItem.getAccount()
+                              .equals(newItem.getAccount());
             }
-        }
+        });
+    }
+
+    public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
+        holder.bindToAccount(listDiffer.getCurrentList()
+                                       .get(position));
     }
 
     @NonNull
     @Override
     public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
         View row = LayoutInflater.from(parent.getContext())
-                .inflate(R.layout.account_summary_row, parent, false);
+                                 .inflate(R.layout.account_summary_row, parent, false);
         return new LedgerRowHolder(row);
     }
 
     @Override
     public int getItemCount() {
-        return Data.accounts.size() + 1;
+        return listDiffer.getCurrentList()
+                         .size();
     }
-    public void startSelection() {
-        try (LockHolder lh = Data.accounts.lockForWriting()) {
-            for (int i = 0; i < Data.accounts.size(); i++) {
-                LedgerAccount acc = Data.accounts.get(i);
-                acc.setHiddenByStarToBe(acc.isHiddenByStar());
-            }
-            this.selectionActive = true;
-            lh.downgrade();
-            notifyDataSetChanged();
-        }
+    public void setAccounts(List<AccountListItem> newList) {
+        listDiffer.submitList(newList);
     }
+    class LedgerRowHolder extends RecyclerView.ViewHolder {
+        private final TextView tvAccountName, tvAccountAmounts;
+        private final ConstraintLayout row;
+        private final View expanderContainer;
+        private final View amountExpanderContainer;
+        private final View lLastUpdate;
+        private final TextView tvLastUpdate;
+        private final View vAccountNameLayout;
+        LedgerAccount mAccount;
+        private AccountListItem.Type lastType;
+        private Observer lastUpdateObserver;
+        public LedgerRowHolder(@NonNull View itemView) {
+            super(itemView);
 
-    public void stopSelection() {
-        this.selectionActive = false;
-        notifyDataSetChanged();
-    }
+            row = itemView.findViewById(R.id.account_summary_row);
+            vAccountNameLayout = itemView.findViewById(R.id.account_name_layout);
+            tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
+            tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
+            expanderContainer = itemView.findViewById(R.id.account_expander_container);
+            ImageView expander = itemView.findViewById(R.id.account_expander);
+            amountExpanderContainer =
+                    itemView.findViewById(R.id.account_row_amounts_expander_container);
+            lLastUpdate = itemView.findViewById(R.id.last_update_container);
+            tvLastUpdate = itemView.findViewById(R.id.last_update_text);
 
-    public boolean isSelectionActive() {
-        return selectionActive;
-    }
+            itemView.setOnLongClickListener(this::onItemLongClick);
+            tvAccountName.setOnLongClickListener(this::onItemLongClick);
+            tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
+            expanderContainer.setOnLongClickListener(this::onItemLongClick);
+            expander.setOnLongClickListener(this::onItemLongClick);
+            row.setOnLongClickListener(this::onItemLongClick);
+
+            tvAccountName.setOnClickListener(v -> toggleAccountExpanded());
+            expanderContainer.setOnClickListener(v -> toggleAccountExpanded());
+            expander.setOnClickListener(v -> toggleAccountExpanded());
+            tvAccountAmounts.setOnClickListener(v -> toggleAmountsExpanded());
 
-    public void selectItem(int position) {
-        try (LockHolder lh = Data.accounts.lockForWriting()) {
-            LedgerAccount acc = Data.accounts.get(position);
-            acc.toggleHiddenToBe();
-            toggleChildrenOf(acc, acc.isHiddenByStarToBe(), position);
-            notifyItemChanged(position);
         }
-    }
-    void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
-        int i = parentPosition + 1;
-        try (LockHolder lh = Data.accounts.lockForWriting()) {
-            for (int j = 0; j < Data.accounts.size(); j++) {
-                LedgerAccount acc = Data.accounts.get(j);
-                if (acc.getName().startsWith(parent.getName() + ":")) {
-                    acc.setHiddenByStarToBe(hiddenToBe);
-                    notifyItemChanged(i);
-                    toggleChildrenOf(acc, hiddenToBe, i);
-                    i++;
-                }
+        private void toggleAccountExpanded() {
+            if (!mAccount.hasSubAccounts())
+                return;
+            debug("accounts", "Account expander clicked");
+
+            // make sure we use the same object as the one in the allAccounts list
+            MobileLedgerProfile profile = mAccount.getProfile();
+            if (profile == null) {
+                return;
             }
-        }
-    }
+            try (Locker ignored = model.lockAccountsForWriting()) {
+                LedgerAccount realAccount = model.locateAccount(mAccount.getName());
+                if (realAccount == null)
+                    return;
 
-    class LedgerRowHolder extends RecyclerView.ViewHolder {
-        CheckBox selectionCb;
-        TextView tvAccountName, tvAccountAmounts;
-        ConstraintLayout row;
-        View vTrailer;
-        FrameLayout expanderContainer;
-        ImageView expander;
-        FrameLayout accountExpanderContainer;
-        public LedgerRowHolder(@NonNull View itemView) {
-            super(itemView);
-            this.row = itemView.findViewById(R.id.account_summary_row);
-            this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
-            this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
-            this.selectionCb = itemView.findViewById(R.id.account_row_check);
-            this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
-            this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
-            this.expander = itemView.findViewById(R.id.account_expander);
-            this.accountExpanderContainer = itemView.findViewById(R.id.account_row_amounts_expander_container);
-
-            MainActivity activity = (MainActivity) row.getContext();
-
-            expanderContainer.addOnLayoutChangeListener(
-                    (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
-                        int w = right - left;
-                        int h = bottom - top;
-                        if (h > w) {
-                            int p = (h - w) / 2;
-                            v.setPadding(0, p, 0, p);
-                        }
-                        else v.setPadding(0, 0, 0, 0);
+                mAccount = realAccount;
+                mAccount.toggleExpanded();
+            }
+            expanderContainer.animate()
+                             .rotation(mAccount.isExpanded() ? 0 : 180);
+            model.updateDisplayedAccounts();
+
+            DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
+                    new Object[]{mAccount.isExpanded(), mAccount.getName(), profile.getUuid()
                     });
 
-            itemView.setOnLongClickListener(new View.OnLongClickListener() {
-                @Override
-                public boolean onLongClick(View v) {
-                    AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
-                    LedgerAccount acc =
-                            (LedgerAccount) v.findViewById(R.id.account_summary_row).getTag();
-                    builder.setTitle(acc.getName());
-                    builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
-                        switch(which) {
-                            case 0:
-                                // show transactions
-                                activity.showAccountTransactions(acc);
-                                break;
-                        }
-                        dialog.dismiss();
+        }
+        private void toggleAmountsExpanded() {
+            if (mAccount.getAmountCount() <= AMOUNT_LIMIT)
+                return;
+
+            mAccount.toggleAmountsExpanded();
+            if (mAccount.amountsExpanded()) {
+                tvAccountAmounts.setText(mAccount.getAmountsString());
+                amountExpanderContainer.setVisibility(View.GONE);
+            }
+            else {
+                tvAccountAmounts.setText(mAccount.getAmountsString(AMOUNT_LIMIT));
+                amountExpanderContainer.setVisibility(View.VISIBLE);
+            }
+
+            MobileLedgerProfile profile = mAccount.getProfile();
+            if (profile == null)
+                return;
+
+            DbOpQueue.add("update accounts set amounts_expanded=? where name=? and profile=?",
+                    new Object[]{mAccount.amountsExpanded(), mAccount.getName(), profile.getUuid()
                     });
-                    builder.show();
-                    return true;
+
+        }
+        private boolean onItemLongClick(View v) {
+            MainActivity activity = (MainActivity) v.getContext();
+            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
+            final String accountName = mAccount.getName();
+            builder.setTitle(accountName);
+            builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
+                if (which == 0) {// show transactions
+                    activity.showAccountTransactions(accountName);
+                }
+                else {
+                    throw new RuntimeException(String.format("Unknown menu item id (%d)", which));
                 }
+                dialog.dismiss();
             });
+            builder.show();
+            return true;
+        }
+        public void bindToAccount(AccountListItem item) {
+            final AccountListItem.Type newType = item.getType();
+            setType(newType);
+
+            switch (newType) {
+                case ACCOUNT:
+                    LedgerAccount acc = item.getAccount();
+
+                    debug("accounts", String.format(Locale.US, "Binding to '%s'", acc.getName()));
+                    Context ctx = row.getContext();
+                    Resources rm = ctx.getResources();
+                    mAccount = acc;
+
+                    row.setTag(acc);
+
+                    tvAccountName.setText(acc.getShortName());
+
+                    ConstraintLayout.LayoutParams lp =
+                            (ConstraintLayout.LayoutParams) tvAccountName.getLayoutParams();
+                    lp.setMarginStart(
+                            acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) /
+                            3);
+
+                    if (acc.hasSubAccounts()) {
+                        expanderContainer.setVisibility(View.VISIBLE);
+                        expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
+                    }
+                    else {
+                        expanderContainer.setVisibility(View.GONE);
+                    }
+
+                    int amounts = acc.getAmountCount();
+                    if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
+                        tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
+                        amountExpanderContainer.setVisibility(View.VISIBLE);
+                    }
+                    else {
+                        tvAccountAmounts.setText(acc.getAmountsString());
+                        amountExpanderContainer.setVisibility(View.GONE);
+                    }
+
+                    break;
+                case HEADER:
+                    setLastUpdateText(Data.lastUpdateText.get());
+                    break;
+                default:
+                    throw new IllegalStateException("Unexpected value: " + newType);
+            }
+
+        }
+        void setLastUpdateText(String text) {
+            tvLastUpdate.setText(text);
+        }
+        private void initLastUpdateObserver() {
+            if (lastUpdateObserver != null)
+                return;
+
+            lastUpdateObserver = (o, arg) -> setLastUpdateText(Data.lastUpdateText.get());
+
+            Data.lastUpdateText.addObserver(lastUpdateObserver);
+        }
+        private void dropLastUpdateObserver() {
+            if (lastUpdateObserver == null)
+                return;
+
+            Data.lastUpdateText.deleteObserver(lastUpdateObserver);
+            lastUpdateObserver = null;
+        }
+        private void setType(AccountListItem.Type newType) {
+            if (newType == lastType)
+                return;
+
+            switch (newType) {
+                case ACCOUNT:
+                    row.setLongClickable(true);
+                    amountExpanderContainer.setVisibility(View.VISIBLE);
+                    vAccountNameLayout.setVisibility(View.VISIBLE);
+                    tvAccountAmounts.setVisibility(View.VISIBLE);
+                    lLastUpdate.setVisibility(View.GONE);
+                    dropLastUpdateObserver();
+                    break;
+                case HEADER:
+                    row.setLongClickable(false);
+                    tvAccountAmounts.setVisibility(View.GONE);
+                    amountExpanderContainer.setVisibility(View.GONE);
+                    vAccountNameLayout.setVisibility(View.GONE);
+                    lLastUpdate.setVisibility(View.VISIBLE);
+                    initLastUpdateObserver();
+                    break;
+                default:
+                    throw new IllegalStateException("Unexpected value: " + newType);
+            }
+
+            lastType = newType;
         }
     }
 }