2 * Copyright © 2020 Damyan Ivanov.
3 * This file is part of MoLe.
4 * MoLe is free software: you can distribute it and/or modify it
5 * under the term of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your opinion), any later version.
9 * MoLe is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License terms for details.
14 * You should have received a copy of the GNU General Public License
15 * along with MoLe. If not, see <https://www.gnu.org/licenses/>.
18 package net.ktnx.mobileledger.ui.account_summary;
20 import android.content.Context;
21 import android.content.res.Resources;
22 import android.text.TextUtils;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ImageView;
27 import android.widget.TextView;
29 import androidx.annotation.NonNull;
30 import androidx.appcompat.app.AlertDialog;
31 import androidx.constraintlayout.widget.ConstraintLayout;
32 import androidx.recyclerview.widget.AsyncListDiffer;
33 import androidx.recyclerview.widget.DiffUtil;
34 import androidx.recyclerview.widget.RecyclerView;
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.async.DbOpQueue;
38 import net.ktnx.mobileledger.model.LedgerAccount;
39 import net.ktnx.mobileledger.model.MobileLedgerProfile;
40 import net.ktnx.mobileledger.ui.MainModel;
41 import net.ktnx.mobileledger.ui.activity.MainActivity;
42 import net.ktnx.mobileledger.utils.Locker;
43 import net.ktnx.mobileledger.utils.Logger;
45 import org.jetbrains.annotations.NotNull;
47 import java.util.List;
48 import java.util.Locale;
50 import static net.ktnx.mobileledger.utils.Logger.debug;
52 public class AccountSummaryAdapter
53 extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
54 public static final int AMOUNT_LIMIT = 3;
55 private final AsyncListDiffer<LedgerAccount> listDiffer;
56 private final MainModel model;
57 AccountSummaryAdapter(MainModel model) {
60 listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<LedgerAccount>() {
62 public boolean areItemsTheSame(@NotNull LedgerAccount oldItem,
63 @NotNull LedgerAccount newItem) {
64 return TextUtils.equals(oldItem.getName(), newItem.getName());
67 public boolean areContentsTheSame(@NotNull LedgerAccount oldItem,
68 @NotNull LedgerAccount newItem) {
69 return oldItem.equals(newItem);
74 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
75 holder.bindToAccount(listDiffer.getCurrentList()
81 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
82 View row = LayoutInflater.from(parent.getContext())
83 .inflate(R.layout.account_summary_row, parent, false);
84 return new LedgerRowHolder(row);
88 public int getItemCount() {
89 return listDiffer.getCurrentList()
92 public void setAccounts(List<LedgerAccount> newList) {
93 listDiffer.submitList(newList);
95 class LedgerRowHolder extends RecyclerView.ViewHolder {
96 final TextView tvAccountName, tvAccountAmounts;
97 final ConstraintLayout row;
98 final View expanderContainer;
99 final ImageView expander;
100 final View accountExpanderContainer;
101 LedgerAccount mAccount;
102 public LedgerRowHolder(@NonNull View itemView) {
105 row = itemView.findViewById(R.id.account_summary_row);
106 tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
107 tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
108 expanderContainer = itemView.findViewById(R.id.account_expander_container);
109 expander = itemView.findViewById(R.id.account_expander);
110 accountExpanderContainer =
111 itemView.findViewById(R.id.account_row_amounts_expander_container);
113 itemView.setOnLongClickListener(this::onItemLongClick);
114 tvAccountName.setOnLongClickListener(this::onItemLongClick);
115 tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
116 expanderContainer.setOnLongClickListener(this::onItemLongClick);
117 expander.setOnLongClickListener(this::onItemLongClick);
118 row.setOnLongClickListener(this::onItemLongClick);
120 tvAccountName.setOnClickListener(v -> toggleAccountExpanded());
121 expanderContainer.setOnClickListener(v -> toggleAccountExpanded());
122 expander.setOnClickListener(v -> toggleAccountExpanded());
123 tvAccountAmounts.setOnClickListener(v -> toggleAmountsExpanded());
125 private void toggleAccountExpanded() {
126 if (!mAccount.hasSubAccounts())
128 debug("accounts", "Account expander clicked");
130 // make sure we use the same object as the one in the allAccounts list
131 MobileLedgerProfile profile = mAccount.getProfile();
132 if (profile == null) {
135 try (Locker ignored = model.lockAccountsForWriting()) {
136 LedgerAccount realAccount = model.locateAccount(mAccount.getName());
137 if (realAccount == null)
140 mAccount = realAccount;
141 mAccount.toggleExpanded();
143 expanderContainer.animate()
144 .rotation(mAccount.isExpanded() ? 0 : 180);
145 model.updateDisplayedAccounts();
147 DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
148 new Object[]{mAccount.isExpanded(), mAccount.getName(), profile.getUuid()
152 private void toggleAmountsExpanded() {
153 if (mAccount.getAmountCount() <= AMOUNT_LIMIT)
156 mAccount.toggleAmountsExpanded();
157 if (mAccount.amountsExpanded()) {
158 tvAccountAmounts.setText(mAccount.getAmountsString());
159 accountExpanderContainer.setVisibility(View.GONE);
162 tvAccountAmounts.setText(mAccount.getAmountsString(AMOUNT_LIMIT));
163 accountExpanderContainer.setVisibility(View.VISIBLE);
166 MobileLedgerProfile profile = mAccount.getProfile();
170 DbOpQueue.add("update accounts set amounts_expanded=? where name=? and profile=?",
171 new Object[]{mAccount.amountsExpanded(), mAccount.getName(), profile.getUuid()
175 private boolean onItemLongClick(View v) {
176 MainActivity activity = (MainActivity) v.getContext();
177 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
178 final String accountName = mAccount.getName();
179 builder.setTitle(accountName);
180 builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
181 if (which == 0) {// show transactions
182 activity.showAccountTransactions(accountName);
185 throw new RuntimeException(String.format("Unknown menu item id (%d)", which));
192 public void bindToAccount(LedgerAccount acc) {
193 Logger.debug("accounts", String.format(Locale.US, "Binding to '%s'", acc.getName()));
194 Context ctx = row.getContext();
195 Resources rm = ctx.getResources();
200 tvAccountName.setText(acc.getShortName());
202 ConstraintLayout.LayoutParams lp =
203 (ConstraintLayout.LayoutParams) tvAccountName.getLayoutParams();
205 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
207 if (acc.hasSubAccounts()) {
208 expanderContainer.setVisibility(View.VISIBLE);
209 expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
212 expanderContainer.setVisibility(View.GONE);
215 int amounts = acc.getAmountCount();
216 if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
217 tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
218 accountExpanderContainer.setVisibility(View.VISIBLE);
221 tvAccountAmounts.setText(acc.getAmountsString());
222 accountExpanderContainer.setVisibility(View.GONE);