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 // FIXME trailer's divider looks bad
88 // perhaps replace the trailer with bottom padding for the last item?
89 holder.vTrailer.setVisibility(View.VISIBLE);
90 holder.row.setVisibility(View.GONE);
97 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
98 View row = LayoutInflater.from(parent.getContext())
99 .inflate(R.layout.account_summary_row, parent, false);
100 return new LedgerRowHolder(row);
104 public int getItemCount() {
105 return Data.accounts.size() + (Data.profile.getValue()
106 .isPostingPermitted() ? 1 : 0);
108 static class LedgerRowHolder extends RecyclerView.ViewHolder {
109 TextView tvAccountName, tvAccountAmounts;
110 ConstraintLayout row;
112 View expanderContainer;
114 View accountExpanderContainer;
115 public LedgerRowHolder(@NonNull View itemView) {
117 this.row = itemView.findViewById(R.id.account_summary_row);
118 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
119 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
120 this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
121 this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
122 this.expander = itemView.findViewById(R.id.account_expander);
123 this.accountExpanderContainer =
124 itemView.findViewById(R.id.account_row_amounts_expander_container);
126 itemView.setOnLongClickListener(this::onItemLongClick);
127 tvAccountName.setOnLongClickListener(this::onItemLongClick);
128 tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
129 expanderContainer.setOnLongClickListener(this::onItemLongClick);
130 expander.setOnLongClickListener(this::onItemLongClick);
131 row.setOnLongClickListener(this::onItemLongClick);
133 private boolean onItemLongClick(View v) {
134 MainActivity activity = (MainActivity) v.getContext();
135 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
139 case R.id.account_summary_row:
142 case R.id.account_row_acc_amounts:
143 case R.id.account_row_amounts_expander_container:
144 row = (View) v.getParent();
146 case R.id.account_row_acc_name:
147 case R.id.account_expander_container:
148 row = (View) v.getParent()
151 case R.id.account_expander:
152 row = (View) v.getParent()
158 String.format("Don't know how to handle long click on id %d", id));
161 LedgerAccount acc = (LedgerAccount) row.getTag();
162 builder.setTitle(acc.getName());
163 builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
167 activity.showAccountTransactions(acc);