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.CheckBox;
28 import android.widget.FrameLayout;
29 import android.widget.ImageView;
30 import android.widget.TextView;
32 import net.ktnx.mobileledger.R;
33 import net.ktnx.mobileledger.model.Data;
34 import net.ktnx.mobileledger.model.LedgerAccount;
35 import net.ktnx.mobileledger.ui.activity.MainActivity;
36 import net.ktnx.mobileledger.utils.LockHolder;
38 import androidx.annotation.NonNull;
39 import androidx.appcompat.app.AlertDialog;
40 import androidx.constraintlayout.widget.ConstraintLayout;
41 import androidx.recyclerview.widget.RecyclerView;
43 public class AccountSummaryAdapter
44 extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
45 public static final int AMOUNT_LIMIT = 3;
46 private boolean selectionActive;
48 AccountSummaryAdapter() {
49 this.selectionActive = false;
52 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
53 try (LockHolder lh = Data.accounts.lockForReading()) {
54 if (position < Data.accounts.size()) {
55 LedgerAccount acc = Data.accounts.get(position);
56 Context ctx = holder.row.getContext();
57 Resources rm = ctx.getResources();
59 holder.row.setTag(acc);
60 holder.row.setVisibility(View.VISIBLE);
61 holder.vTrailer.setVisibility(View.GONE);
62 holder.tvAccountName.setText(acc.getShortName());
63 ConstraintLayout.LayoutParams lp =
64 (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
66 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 2);
67 holder.expanderContainer
68 .setVisibility(acc.hasSubAccounts() ? View.VISIBLE : View.INVISIBLE);
69 holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
70 int amounts = acc.getAmountCount();
71 if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
72 holder.tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
73 holder.accountExpanderContainer.setVisibility(View.VISIBLE);
76 holder.tvAccountAmounts.setText(acc.getAmountsString());
77 holder.accountExpanderContainer.setVisibility(View.GONE);
80 if (acc.isHiddenByStar()) {
81 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
82 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
85 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
86 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
89 holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
90 holder.selectionCb.setChecked(!acc.isHiddenByStarToBe());
92 holder.row.setTag(R.id.POS, position);
95 holder.vTrailer.setVisibility(View.VISIBLE);
96 holder.row.setVisibility(View.GONE);
103 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
104 View row = LayoutInflater.from(parent.getContext())
105 .inflate(R.layout.account_summary_row, parent, false);
106 return new LedgerRowHolder(row);
110 public int getItemCount() {
111 return Data.accounts.size() + 1;
113 public void startSelection() {
114 try (LockHolder lh = Data.accounts.lockForWriting()) {
115 for (int i = 0; i < Data.accounts.size(); i++) {
116 LedgerAccount acc = Data.accounts.get(i);
117 acc.setHiddenByStarToBe(acc.isHiddenByStar());
119 this.selectionActive = true;
121 notifyDataSetChanged();
125 public void stopSelection() {
126 this.selectionActive = false;
127 notifyDataSetChanged();
130 public boolean isSelectionActive() {
131 return selectionActive;
134 public void selectItem(int position) {
135 try (LockHolder lh = Data.accounts.lockForWriting()) {
136 LedgerAccount acc = Data.accounts.get(position);
137 acc.toggleHiddenToBe();
138 toggleChildrenOf(acc, acc.isHiddenByStarToBe(), position);
139 notifyItemChanged(position);
142 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
143 int i = parentPosition + 1;
144 try (LockHolder lh = Data.accounts.lockForWriting()) {
145 for (int j = 0; j < Data.accounts.size(); j++) {
146 LedgerAccount acc = Data.accounts.get(j);
147 if (acc.getName().startsWith(parent.getName() + ":")) {
148 acc.setHiddenByStarToBe(hiddenToBe);
149 notifyItemChanged(i);
150 toggleChildrenOf(acc, hiddenToBe, i);
157 class LedgerRowHolder extends RecyclerView.ViewHolder {
158 CheckBox selectionCb;
159 TextView tvAccountName, tvAccountAmounts;
160 ConstraintLayout row;
162 FrameLayout expanderContainer;
164 FrameLayout accountExpanderContainer;
165 public LedgerRowHolder(@NonNull View itemView) {
167 this.row = itemView.findViewById(R.id.account_summary_row);
168 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
169 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
170 this.selectionCb = itemView.findViewById(R.id.account_row_check);
171 this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
172 this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
173 this.expander = itemView.findViewById(R.id.account_expander);
174 this.accountExpanderContainer =
175 itemView.findViewById(R.id.account_row_amounts_expander_container);
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(this::onItemLongClick);
189 tvAccountName.setOnLongClickListener(this::onItemLongClick);
190 tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
191 expanderContainer.setOnLongClickListener(this::onItemLongClick);
192 expander.setOnLongClickListener(this::onItemLongClick);
194 private boolean onItemLongClick(View v) {
195 MainActivity activity = (MainActivity) v.getContext();
196 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
200 case R.id.account_summary_row:
203 case R.id.account_root:
204 row = v.findViewById(R.id.account_summary_row);
206 case R.id.account_row_acc_name:
207 case R.id.account_row_acc_amounts:
208 case R.id.account_expander_container:
209 row = (View) v.getParent();
211 case R.id.account_expander:
212 row = (View) v.getParent().getParent();
215 Log.e("error", String.format("Don't know how to handle long click on id ", id));
218 LedgerAccount acc = (LedgerAccount) row.findViewById(R.id.account_summary_row).getTag();
219 builder.setTitle(acc.getName());
220 builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
224 activity.showAccountTransactions(acc);