2 * Copyright © 2019 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 if (position < Data.accounts.size()) {
50 LedgerAccount acc = Data.accounts.get(position);
51 Context ctx = holder.row.getContext();
52 Resources rm = ctx.getResources();
54 holder.row.setTag(acc);
55 holder.row.setVisibility(View.VISIBLE);
56 holder.vTrailer.setVisibility(View.GONE);
57 holder.tvAccountName.setText(acc.getShortName());
58 ConstraintLayout.LayoutParams lp =
59 (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
61 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
62 holder.expanderContainer.setVisibility(
63 acc.hasSubAccounts() ? View.VISIBLE : View.GONE);
64 holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
65 int amounts = acc.getAmountCount();
66 if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
67 holder.tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
68 holder.accountExpanderContainer.setVisibility(View.VISIBLE);
71 holder.tvAccountAmounts.setText(acc.getAmountsString());
72 holder.accountExpanderContainer.setVisibility(View.GONE);
75 if (acc.isHiddenByStar()) {
76 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
77 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
80 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
81 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
84 holder.row.setTag(R.id.POS, position);
87 holder.vTrailer.setVisibility(View.VISIBLE);
88 holder.row.setVisibility(View.GONE);
95 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
96 View row = LayoutInflater.from(parent.getContext())
97 .inflate(R.layout.account_summary_row, parent, false);
98 return new LedgerRowHolder(row);
102 public int getItemCount() {
103 return Data.accounts.size() + (Data.profile.getValue()
104 .isPostingPermitted() ? 1 : 0);
106 static class LedgerRowHolder extends RecyclerView.ViewHolder {
107 TextView tvAccountName, tvAccountAmounts;
108 ConstraintLayout row;
110 View expanderContainer;
112 View accountExpanderContainer;
113 public LedgerRowHolder(@NonNull View itemView) {
115 this.row = itemView.findViewById(R.id.account_summary_row);
116 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
117 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
118 this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
119 this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
120 this.expander = itemView.findViewById(R.id.account_expander);
121 this.accountExpanderContainer =
122 itemView.findViewById(R.id.account_row_amounts_expander_container);
124 expanderContainer.addOnLayoutChangeListener(
125 (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
126 int w = right - left;
127 int h = bottom - top;
130 v.setPadding(0, p, 0, p);
132 else v.setPadding(0, 0, 0, 0);
135 itemView.setOnLongClickListener(this::onItemLongClick);
136 tvAccountName.setOnLongClickListener(this::onItemLongClick);
137 tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
138 expanderContainer.setOnLongClickListener(this::onItemLongClick);
139 expander.setOnLongClickListener(this::onItemLongClick);
140 row.setOnLongClickListener(this::onItemLongClick);
142 private boolean onItemLongClick(View v) {
143 MainActivity activity = (MainActivity) v.getContext();
144 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
148 case R.id.account_summary_row:
151 case R.id.account_row_acc_amounts:
152 case R.id.account_row_amounts_expander_container:
153 row = (View) v.getParent();
155 case R.id.account_row_acc_name:
156 case R.id.account_expander_container:
157 row = (View) v.getParent()
160 case R.id.account_expander:
161 row = (View) v.getParent()
167 String.format("Don't know how to handle long click on id %d", id));
170 LedgerAccount acc = (LedgerAccount) row.getTag();
171 builder.setTitle(acc.getName());
172 builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
176 activity.showAccountTransactions(acc);