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.activity.MainActivity;
41 import net.ktnx.mobileledger.utils.Locker;
43 import org.jetbrains.annotations.NotNull;
45 import java.util.List;
47 import static net.ktnx.mobileledger.utils.Logger.debug;
49 public class AccountSummaryAdapter
50 extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
51 public static final int AMOUNT_LIMIT = 3;
52 private AsyncListDiffer<LedgerAccount> listDiffer;
53 AccountSummaryAdapter() {
54 listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<LedgerAccount>() {
56 public boolean areItemsTheSame(@NotNull LedgerAccount oldItem,
57 @NotNull LedgerAccount newItem) {
58 return TextUtils.equals(oldItem.getName(), newItem.getName());
61 public boolean areContentsTheSame(@NotNull LedgerAccount oldItem,
62 @NotNull LedgerAccount newItem) {
63 return oldItem.equals(newItem);
68 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
69 holder.bindToAccount(listDiffer.getCurrentList()
75 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
76 View row = LayoutInflater.from(parent.getContext())
77 .inflate(R.layout.account_summary_row, parent, false);
78 return new LedgerRowHolder(row);
82 public int getItemCount() {
83 return listDiffer.getCurrentList()
86 public void setAccounts(List<LedgerAccount> newList) {
87 listDiffer.submitList(newList);
89 static class LedgerRowHolder extends RecyclerView.ViewHolder {
90 TextView tvAccountName, tvAccountAmounts;
92 View expanderContainer;
94 View accountExpanderContainer;
95 LedgerAccount mAccount;
96 public LedgerRowHolder(@NonNull View itemView) {
99 row = itemView.findViewById(R.id.account_summary_row);
100 tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
101 tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
102 expanderContainer = itemView.findViewById(R.id.account_expander_container);
103 expander = itemView.findViewById(R.id.account_expander);
104 accountExpanderContainer =
105 itemView.findViewById(R.id.account_row_amounts_expander_container);
107 itemView.setOnLongClickListener(this::onItemLongClick);
108 tvAccountName.setOnLongClickListener(this::onItemLongClick);
109 tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
110 expanderContainer.setOnLongClickListener(this::onItemLongClick);
111 expander.setOnLongClickListener(this::onItemLongClick);
112 row.setOnLongClickListener(this::onItemLongClick);
114 tvAccountName.setOnClickListener(v -> toggleAccountExpanded());
115 expanderContainer.setOnClickListener(v -> toggleAccountExpanded());
116 expander.setOnClickListener(v -> toggleAccountExpanded());
117 tvAccountAmounts.setOnClickListener(v -> toggleAmountsExpanded());
119 private void toggleAccountExpanded() {
120 if (!mAccount.hasSubAccounts())
122 debug("accounts", "Account expander clicked");
124 // make sure we use the same object as the one in the allAccounts list
125 MobileLedgerProfile profile = mAccount.getProfile();
126 if (profile == null) {
129 try (Locker ignored = profile.lockAccountsForWriting()) {
130 LedgerAccount realAccount = profile.locateAccount(mAccount.getName());
131 if (realAccount == null)
134 mAccount = realAccount;
135 mAccount.toggleExpanded();
137 expanderContainer.animate()
138 .rotation(mAccount.isExpanded() ? 0 : 180);
139 profile.updateDisplayedAccounts();
141 DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
142 new Object[]{mAccount.isExpanded(), mAccount.getName(), profile.getUuid()
146 private void toggleAmountsExpanded() {
147 if (mAccount.getAmountCount() <= AMOUNT_LIMIT)
150 mAccount.toggleAmountsExpanded();
151 if (mAccount.amountsExpanded()) {
152 tvAccountAmounts.setText(mAccount.getAmountsString());
153 accountExpanderContainer.setVisibility(View.GONE);
156 tvAccountAmounts.setText(mAccount.getAmountsString(AMOUNT_LIMIT));
157 accountExpanderContainer.setVisibility(View.VISIBLE);
160 MobileLedgerProfile profile = mAccount.getProfile();
164 DbOpQueue.add("update accounts set amounts_expanded=? where name=? and profile=?",
165 new Object[]{mAccount.amountsExpanded(), mAccount.getName(), profile.getUuid()
169 private boolean onItemLongClick(View v) {
170 MainActivity activity = (MainActivity) v.getContext();
171 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
172 final String accountName = mAccount.getName();
173 builder.setTitle(accountName);
174 builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
178 activity.showAccountTransactions(accountName);
181 throw new RuntimeException(
182 String.format("Unknown menu item id (%d)", which));
189 public void bindToAccount(LedgerAccount acc) {
190 Context ctx = row.getContext();
191 Resources rm = ctx.getResources();
196 tvAccountName.setText(acc.getShortName());
198 ConstraintLayout.LayoutParams lp =
199 (ConstraintLayout.LayoutParams) tvAccountName.getLayoutParams();
201 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
203 if (acc.hasSubAccounts()) {
204 expanderContainer.setVisibility(View.VISIBLE);
205 expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
208 expanderContainer.setVisibility(View.GONE);
211 int amounts = acc.getAmountCount();
212 if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
213 tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
214 accountExpanderContainer.setVisibility(View.VISIBLE);
217 tvAccountAmounts.setText(acc.getAmountsString());
218 accountExpanderContainer.setVisibility(View.GONE);