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.utils.LockHolder;
36 import androidx.annotation.NonNull;
37 import androidx.constraintlayout.widget.ConstraintLayout;
38 import androidx.recyclerview.widget.RecyclerView;
40 public class AccountSummaryAdapter
41 extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
42 private boolean selectionActive;
44 AccountSummaryAdapter() {
45 this.selectionActive = false;
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 holder.tvAccountAmounts.setText(acc.getAmountsString());
68 if (acc.isHiddenByStar()) {
69 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
70 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
73 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
74 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
77 holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
78 holder.selectionCb.setChecked(!acc.isHiddenByStarToBe());
80 holder.row.setTag(R.id.POS, position);
83 holder.vTrailer.setVisibility(View.VISIBLE);
84 holder.row.setVisibility(View.GONE);
91 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
92 View row = LayoutInflater.from(parent.getContext())
93 .inflate(R.layout.account_summary_row, parent, false);
94 return new LedgerRowHolder(row);
98 public int getItemCount() {
99 return Data.accounts.size() + 1;
101 public void startSelection() {
102 try (LockHolder lh = Data.accounts.lockForWriting()) {
103 for (int i = 0; i < Data.accounts.size(); i++) {
104 LedgerAccount acc = Data.accounts.get(i);
105 acc.setHiddenByStarToBe(acc.isHiddenByStar());
107 this.selectionActive = true;
109 notifyDataSetChanged();
113 public void stopSelection() {
114 this.selectionActive = false;
115 notifyDataSetChanged();
118 public boolean isSelectionActive() {
119 return selectionActive;
122 public void selectItem(int position) {
123 try (LockHolder lh = Data.accounts.lockForWriting()) {
124 LedgerAccount acc = Data.accounts.get(position);
125 acc.toggleHiddenToBe();
126 toggleChildrenOf(acc, acc.isHiddenByStarToBe(), position);
127 notifyItemChanged(position);
130 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
131 int i = parentPosition + 1;
132 try (LockHolder lh = Data.accounts.lockForWriting()) {
133 for (int j = 0; j < Data.accounts.size(); j++) {
134 LedgerAccount acc = Data.accounts.get(j);
135 if (acc.getName().startsWith(parent.getName() + ":")) {
136 acc.setHiddenByStarToBe(hiddenToBe);
137 notifyItemChanged(i);
138 toggleChildrenOf(acc, hiddenToBe, i);
145 class LedgerRowHolder extends RecyclerView.ViewHolder {
146 CheckBox selectionCb;
147 TextView tvAccountName, tvAccountAmounts;
148 ConstraintLayout row;
150 FrameLayout expanderContainer;
152 public LedgerRowHolder(@NonNull View itemView) {
154 this.row = itemView.findViewById(R.id.account_summary_row);
155 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
156 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
157 this.selectionCb = itemView.findViewById(R.id.account_row_check);
158 this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
159 this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
160 this.expander = itemView.findViewById(R.id.account_expander);
162 expanderContainer.addOnLayoutChangeListener(
163 (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
164 int w = right - left;
165 int h = bottom - top;
168 v.setPadding(0, p, 0, p);
170 else v.setPadding(0, 0, 0, 0);