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 private boolean selectionActive;
46 AccountSummaryAdapter() {
47 this.selectionActive = false;
50 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
51 try (LockHolder lh = Data.accounts.lockForReading()) {
52 if (position < Data.accounts.size()) {
53 LedgerAccount acc = Data.accounts.get(position);
54 Context ctx = holder.row.getContext();
55 Resources rm = ctx.getResources();
57 holder.row.setTag(acc);
58 holder.row.setVisibility(View.VISIBLE);
59 holder.vTrailer.setVisibility(View.GONE);
60 holder.tvAccountName.setText(acc.getShortName());
61 ConstraintLayout.LayoutParams lp =
62 (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
64 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 2);
65 holder.expanderContainer
66 .setVisibility(acc.hasSubAccounts() ? View.VISIBLE : View.INVISIBLE);
67 holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
68 holder.tvAccountAmounts.setText(acc.getAmountsString());
70 if (acc.isHiddenByStar()) {
71 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
72 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
75 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
76 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
79 holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
80 holder.selectionCb.setChecked(!acc.isHiddenByStarToBe());
82 holder.row.setTag(R.id.POS, position);
85 holder.vTrailer.setVisibility(View.VISIBLE);
86 holder.row.setVisibility(View.GONE);
93 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
94 View row = LayoutInflater.from(parent.getContext())
95 .inflate(R.layout.account_summary_row, parent, false);
96 return new LedgerRowHolder(row);
100 public int getItemCount() {
101 return Data.accounts.size() + 1;
103 public void startSelection() {
104 try (LockHolder lh = Data.accounts.lockForWriting()) {
105 for (int i = 0; i < Data.accounts.size(); i++) {
106 LedgerAccount acc = Data.accounts.get(i);
107 acc.setHiddenByStarToBe(acc.isHiddenByStar());
109 this.selectionActive = true;
111 notifyDataSetChanged();
115 public void stopSelection() {
116 this.selectionActive = false;
117 notifyDataSetChanged();
120 public boolean isSelectionActive() {
121 return selectionActive;
124 public void selectItem(int position) {
125 try (LockHolder lh = Data.accounts.lockForWriting()) {
126 LedgerAccount acc = Data.accounts.get(position);
127 acc.toggleHiddenToBe();
128 toggleChildrenOf(acc, acc.isHiddenByStarToBe(), position);
129 notifyItemChanged(position);
132 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
133 int i = parentPosition + 1;
134 try (LockHolder lh = Data.accounts.lockForWriting()) {
135 for (int j = 0; j < Data.accounts.size(); j++) {
136 LedgerAccount acc = Data.accounts.get(j);
137 if (acc.getName().startsWith(parent.getName() + ":")) {
138 acc.setHiddenByStarToBe(hiddenToBe);
139 notifyItemChanged(i);
140 toggleChildrenOf(acc, hiddenToBe, i);
147 class LedgerRowHolder extends RecyclerView.ViewHolder {
148 CheckBox selectionCb;
149 TextView tvAccountName, tvAccountAmounts;
150 ConstraintLayout row;
152 FrameLayout expanderContainer;
154 public LedgerRowHolder(@NonNull View itemView) {
156 this.row = itemView.findViewById(R.id.account_summary_row);
157 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
158 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
159 this.selectionCb = itemView.findViewById(R.id.account_row_check);
160 this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
161 this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
162 this.expander = itemView.findViewById(R.id.account_expander);
164 MainActivity activity = (MainActivity) row.getContext();
166 expanderContainer.addOnLayoutChangeListener(
167 (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
168 int w = right - left;
169 int h = bottom - top;
172 v.setPadding(0, p, 0, p);
174 else v.setPadding(0, 0, 0, 0);
177 itemView.setOnLongClickListener(new View.OnLongClickListener() {
179 public boolean onLongClick(View v) {
180 AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
182 (LedgerAccount) v.findViewById(R.id.account_summary_row).getTag();
183 builder.setTitle(acc.getName());
184 builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
188 activity.showAccountTransactions(acc);