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.util.Log;
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.RecyclerView;
34 import net.ktnx.mobileledger.R;
35 import net.ktnx.mobileledger.model.Data;
36 import net.ktnx.mobileledger.model.LedgerAccount;
37 import net.ktnx.mobileledger.ui.activity.MainActivity;
38 import net.ktnx.mobileledger.utils.LockHolder;
40 public class AccountSummaryAdapter
41 extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
42 public static final int AMOUNT_LIMIT = 3;
44 AccountSummaryAdapter() { }
46 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
47 try (LockHolder lh = Data.accounts.lockForReading()) {
48 LedgerAccount acc = Data.accounts.get(position);
49 Context ctx = holder.row.getContext();
50 Resources rm = ctx.getResources();
52 holder.row.setTag(acc);
53 holder.row.setVisibility(View.VISIBLE);
54 holder.tvAccountName.setText(acc.getShortName());
55 ConstraintLayout.LayoutParams lp =
56 (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
58 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
59 holder.expanderContainer.setVisibility(
60 acc.hasSubAccounts() ? View.VISIBLE : View.GONE);
61 holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
62 int amounts = acc.getAmountCount();
63 if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
64 holder.tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
65 holder.accountExpanderContainer.setVisibility(View.VISIBLE);
68 holder.tvAccountAmounts.setText(acc.getAmountsString());
69 holder.accountExpanderContainer.setVisibility(View.GONE);
72 holder.row.setTag(R.id.POS, position);
78 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
79 View row = LayoutInflater.from(parent.getContext())
80 .inflate(R.layout.account_summary_row, parent, false);
81 return new LedgerRowHolder(row);
85 public int getItemCount() {
86 return Data.accounts.size();
88 static class LedgerRowHolder extends RecyclerView.ViewHolder {
89 TextView tvAccountName, tvAccountAmounts;
91 View expanderContainer;
93 View accountExpanderContainer;
94 public LedgerRowHolder(@NonNull View itemView) {
96 this.row = itemView.findViewById(R.id.account_summary_row);
97 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
98 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
99 this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
100 this.expander = itemView.findViewById(R.id.account_expander);
101 this.accountExpanderContainer =
102 itemView.findViewById(R.id.account_row_amounts_expander_container);
104 itemView.setOnLongClickListener(this::onItemLongClick);
105 tvAccountName.setOnLongClickListener(this::onItemLongClick);
106 tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
107 expanderContainer.setOnLongClickListener(this::onItemLongClick);
108 expander.setOnLongClickListener(this::onItemLongClick);
109 row.setOnLongClickListener(this::onItemLongClick);
111 private boolean onItemLongClick(View v) {
112 MainActivity activity = (MainActivity) v.getContext();
113 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
117 case R.id.account_summary_row:
120 case R.id.account_row_acc_amounts:
121 case R.id.account_row_amounts_expander_container:
122 row = (View) v.getParent();
124 case R.id.account_row_acc_name:
125 case R.id.account_expander_container:
126 row = (View) v.getParent()
129 case R.id.account_expander:
130 row = (View) v.getParent()
136 String.format("Don't know how to handle long click on id %d", id));
139 LedgerAccount acc = (LedgerAccount) row.getTag();
140 builder.setTitle(acc.getName());
141 builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
145 activity.showAccountTransactions(acc);