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.FrameLayout;
28 import android.widget.ImageView;
29 import android.widget.TextView;
31 import net.ktnx.mobileledger.R;
32 import net.ktnx.mobileledger.model.Data;
33 import net.ktnx.mobileledger.model.LedgerAccount;
34 import net.ktnx.mobileledger.ui.activity.MainActivity;
35 import net.ktnx.mobileledger.utils.LockHolder;
37 import androidx.annotation.NonNull;
38 import androidx.appcompat.app.AlertDialog;
39 import androidx.constraintlayout.widget.ConstraintLayout;
40 import androidx.recyclerview.widget.RecyclerView;
42 public class AccountSummaryAdapter
43 extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
44 public static final int AMOUNT_LIMIT = 3;
46 AccountSummaryAdapter() { }
48 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
49 try (LockHolder lh = Data.accounts.lockForReading()) {
50 if (position < Data.accounts.size()) {
51 LedgerAccount acc = Data.accounts.get(position);
52 Context ctx = holder.row.getContext();
53 Resources rm = ctx.getResources();
55 holder.row.setTag(acc);
56 holder.row.setVisibility(View.VISIBLE);
57 holder.vTrailer.setVisibility(View.GONE);
58 holder.tvAccountName.setText(acc.getShortName());
59 ConstraintLayout.LayoutParams lp =
60 (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
62 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 2);
63 holder.expanderContainer
64 .setVisibility(acc.hasSubAccounts() ? View.VISIBLE : View.INVISIBLE);
65 holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
66 int amounts = acc.getAmountCount();
67 if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
68 holder.tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
69 holder.accountExpanderContainer.setVisibility(View.VISIBLE);
72 holder.tvAccountAmounts.setText(acc.getAmountsString());
73 holder.accountExpanderContainer.setVisibility(View.GONE);
76 if (acc.isHiddenByStar()) {
77 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
78 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
81 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
82 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
85 holder.row.setTag(R.id.POS, position);
88 holder.vTrailer.setVisibility(View.VISIBLE);
89 holder.row.setVisibility(View.GONE);
96 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
97 View row = LayoutInflater.from(parent.getContext())
98 .inflate(R.layout.account_summary_row, parent, false);
99 return new LedgerRowHolder(row);
103 public int getItemCount() {
104 return Data.accounts.size();
106 public void selectItem(int position) {
107 try (LockHolder lh = Data.accounts.lockForWriting()) {
108 LedgerAccount acc = Data.accounts.get(position);
109 acc.toggleHiddenToBe();
110 toggleChildrenOf(acc, acc.isHiddenByStarToBe(), position);
111 notifyItemChanged(position);
114 private void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
115 int i = parentPosition + 1;
116 try (LockHolder lh = Data.accounts.lockForWriting()) {
117 for (int j = 0; j < Data.accounts.size(); j++) {
118 LedgerAccount acc = Data.accounts.get(j);
119 if (acc.getName().startsWith(parent.getName() + ":")) {
120 acc.setHiddenByStarToBe(hiddenToBe);
121 notifyItemChanged(i);
122 toggleChildrenOf(acc, hiddenToBe, i);
129 static class LedgerRowHolder extends RecyclerView.ViewHolder {
130 TextView tvAccountName, tvAccountAmounts;
131 ConstraintLayout row;
133 FrameLayout expanderContainer;
135 FrameLayout accountExpanderContainer;
136 public LedgerRowHolder(@NonNull View itemView) {
138 this.row = itemView.findViewById(R.id.account_summary_row);
139 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
140 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
141 this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
142 this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
143 this.expander = itemView.findViewById(R.id.account_expander);
144 this.accountExpanderContainer =
145 itemView.findViewById(R.id.account_row_amounts_expander_container);
147 expanderContainer.addOnLayoutChangeListener(
148 (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
149 int w = right - left;
150 int h = bottom - top;
153 v.setPadding(0, p, 0, p);
155 else v.setPadding(0, 0, 0, 0);
158 itemView.setOnLongClickListener(this::onItemLongClick);
159 tvAccountName.setOnLongClickListener(this::onItemLongClick);
160 tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
161 expanderContainer.setOnLongClickListener(this::onItemLongClick);
162 expander.setOnLongClickListener(this::onItemLongClick);
164 private boolean onItemLongClick(View v) {
165 MainActivity activity = (MainActivity) v.getContext();
166 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
170 case R.id.account_summary_row:
173 case R.id.account_root:
174 row = v.findViewById(R.id.account_summary_row);
176 case R.id.account_row_acc_name:
177 case R.id.account_row_acc_amounts:
178 case R.id.account_expander_container:
179 row = (View) v.getParent();
181 case R.id.account_expander:
182 row = (View) v.getParent().getParent();
185 Log.e("error", String.format("Don't know how to handle long click on id ", id));
188 LedgerAccount acc = (LedgerAccount) row.findViewById(R.id.account_summary_row).getTag();
189 builder.setTitle(acc.getName());
190 builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
194 activity.showAccountTransactions(acc);