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.graphics.Typeface;
23 import android.util.Log;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ImageView;
28 import android.widget.TextView;
30 import androidx.annotation.NonNull;
31 import androidx.appcompat.app.AlertDialog;
32 import androidx.constraintlayout.widget.ConstraintLayout;
33 import androidx.recyclerview.widget.RecyclerView;
35 import net.ktnx.mobileledger.R;
36 import net.ktnx.mobileledger.model.Data;
37 import net.ktnx.mobileledger.model.LedgerAccount;
38 import net.ktnx.mobileledger.ui.activity.MainActivity;
39 import net.ktnx.mobileledger.utils.LockHolder;
41 public class AccountSummaryAdapter
42 extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
43 public static final int AMOUNT_LIMIT = 3;
45 AccountSummaryAdapter() { }
47 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
48 try (LockHolder lh = Data.accounts.lockForReading()) {
49 LedgerAccount acc = Data.accounts.get(position);
50 Context ctx = holder.row.getContext();
51 Resources rm = ctx.getResources();
53 holder.row.setTag(acc);
54 holder.row.setVisibility(View.VISIBLE);
55 holder.tvAccountName.setText(acc.getShortName());
56 ConstraintLayout.LayoutParams lp =
57 (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
59 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
60 holder.expanderContainer.setVisibility(
61 acc.hasSubAccounts() ? View.VISIBLE : View.GONE);
62 holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
63 int amounts = acc.getAmountCount();
64 if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
65 holder.tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
66 holder.accountExpanderContainer.setVisibility(View.VISIBLE);
69 holder.tvAccountAmounts.setText(acc.getAmountsString());
70 holder.accountExpanderContainer.setVisibility(View.GONE);
73 if (acc.isHiddenByStar()) {
74 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
75 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
78 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
79 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
82 holder.row.setTag(R.id.POS, position);
88 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
89 View row = LayoutInflater.from(parent.getContext())
90 .inflate(R.layout.account_summary_row, parent, false);
91 return new LedgerRowHolder(row);
95 public int getItemCount() {
96 return Data.accounts.size();
98 static class LedgerRowHolder extends RecyclerView.ViewHolder {
99 TextView tvAccountName, tvAccountAmounts;
100 ConstraintLayout row;
101 View expanderContainer;
103 View accountExpanderContainer;
104 public LedgerRowHolder(@NonNull View itemView) {
106 this.row = itemView.findViewById(R.id.account_summary_row);
107 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
108 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
109 this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
110 this.expander = itemView.findViewById(R.id.account_expander);
111 this.accountExpanderContainer =
112 itemView.findViewById(R.id.account_row_amounts_expander_container);
114 itemView.setOnLongClickListener(this::onItemLongClick);
115 tvAccountName.setOnLongClickListener(this::onItemLongClick);
116 tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
117 expanderContainer.setOnLongClickListener(this::onItemLongClick);
118 expander.setOnLongClickListener(this::onItemLongClick);
119 row.setOnLongClickListener(this::onItemLongClick);
121 private boolean onItemLongClick(View v) {
122 MainActivity activity = (MainActivity) v.getContext();
123 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
127 case R.id.account_summary_row:
130 case R.id.account_row_acc_amounts:
131 case R.id.account_row_amounts_expander_container:
132 row = (View) v.getParent();
134 case R.id.account_row_acc_name:
135 case R.id.account_expander_container:
136 row = (View) v.getParent()
139 case R.id.account_expander:
140 row = (View) v.getParent()
146 String.format("Don't know how to handle long click on id %d", id));
149 LedgerAccount acc = (LedgerAccount) row.getTag();
150 builder.setTitle(acc.getName());
151 builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
155 activity.showAccountTransactions(acc);