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.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.CheckBox;
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;
45 private boolean selectionActive;
47 AccountSummaryAdapter() {
48 this.selectionActive = false;
51 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
52 try (LockHolder lh = Data.accounts.lockForReading()) {
53 if (position < Data.accounts.size()) {
54 LedgerAccount acc = Data.accounts.get(position);
55 Context ctx = holder.row.getContext();
56 Resources rm = ctx.getResources();
58 holder.row.setTag(acc);
59 holder.row.setVisibility(View.VISIBLE);
60 holder.vTrailer.setVisibility(View.GONE);
61 holder.tvAccountName.setText(acc.getShortName());
62 ConstraintLayout.LayoutParams lp =
63 (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
65 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 2);
66 holder.expanderContainer
67 .setVisibility(acc.hasSubAccounts() ? View.VISIBLE : View.INVISIBLE);
68 holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
69 int amounts = acc.getAmountCount();
70 if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
71 holder.tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
72 holder.accountExpanderContainer.setVisibility(View.VISIBLE);
75 holder.tvAccountAmounts.setText(acc.getAmountsString());
76 holder.accountExpanderContainer.setVisibility(View.GONE);
79 if (acc.isHiddenByStar()) {
80 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
81 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
84 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
85 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
88 holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
89 holder.selectionCb.setChecked(!acc.isHiddenByStarToBe());
91 holder.row.setTag(R.id.POS, position);
94 holder.vTrailer.setVisibility(View.VISIBLE);
95 holder.row.setVisibility(View.GONE);
102 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
103 View row = LayoutInflater.from(parent.getContext())
104 .inflate(R.layout.account_summary_row, parent, false);
105 return new LedgerRowHolder(row);
109 public int getItemCount() {
110 return Data.accounts.size() + 1;
112 public void startSelection() {
113 try (LockHolder lh = Data.accounts.lockForWriting()) {
114 for (int i = 0; i < Data.accounts.size(); i++) {
115 LedgerAccount acc = Data.accounts.get(i);
116 acc.setHiddenByStarToBe(acc.isHiddenByStar());
118 this.selectionActive = true;
120 notifyDataSetChanged();
124 public void stopSelection() {
125 this.selectionActive = false;
126 notifyDataSetChanged();
129 public boolean isSelectionActive() {
130 return selectionActive;
133 public void selectItem(int position) {
134 try (LockHolder lh = Data.accounts.lockForWriting()) {
135 LedgerAccount acc = Data.accounts.get(position);
136 acc.toggleHiddenToBe();
137 toggleChildrenOf(acc, acc.isHiddenByStarToBe(), position);
138 notifyItemChanged(position);
141 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
142 int i = parentPosition + 1;
143 try (LockHolder lh = Data.accounts.lockForWriting()) {
144 for (int j = 0; j < Data.accounts.size(); j++) {
145 LedgerAccount acc = Data.accounts.get(j);
146 if (acc.getName().startsWith(parent.getName() + ":")) {
147 acc.setHiddenByStarToBe(hiddenToBe);
148 notifyItemChanged(i);
149 toggleChildrenOf(acc, hiddenToBe, i);
156 class LedgerRowHolder extends RecyclerView.ViewHolder {
157 CheckBox selectionCb;
158 TextView tvAccountName, tvAccountAmounts;
159 ConstraintLayout row;
161 FrameLayout expanderContainer;
163 FrameLayout accountExpanderContainer;
164 public LedgerRowHolder(@NonNull View itemView) {
166 this.row = itemView.findViewById(R.id.account_summary_row);
167 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
168 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
169 this.selectionCb = itemView.findViewById(R.id.account_row_check);
170 this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
171 this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
172 this.expander = itemView.findViewById(R.id.account_expander);
173 this.accountExpanderContainer = itemView.findViewById(R.id.account_row_amounts_expander_container);
175 MainActivity activity = (MainActivity) row.getContext();
177 expanderContainer.addOnLayoutChangeListener(
178 (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
179 int w = right - left;
180 int h = bottom - top;
183 v.setPadding(0, p, 0, p);
185 else v.setPadding(0, 0, 0, 0);
188 itemView.setOnLongClickListener(new View.OnLongClickListener() {
190 public boolean onLongClick(View v) {
191 AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
193 (LedgerAccount) v.findViewById(R.id.account_summary_row).getTag();
194 builder.setTitle(acc.getName());
195 builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
199 activity.showAccountTransactions(acc);