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;
35 import java.util.List;
37 import androidx.annotation.NonNull;
38 import androidx.constraintlayout.widget.ConstraintLayout;
39 import androidx.recyclerview.widget.RecyclerView;
41 public class AccountSummaryAdapter
42 extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
43 private boolean selectionActive;
45 AccountSummaryAdapter() {
46 this.selectionActive = false;
49 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
50 List<LedgerAccount> accounts = Data.accounts.get();
51 if (position < accounts.size()) {
52 LedgerAccount acc = accounts.get(position);
53 Context ctx = holder.row.getContext();
54 Resources rm = ctx.getResources();
56 holder.row.setTag(acc);
57 holder.row.setVisibility(View.VISIBLE);
58 holder.vTrailer.setVisibility(View.GONE);
59 holder.tvAccountName.setText(acc.getShortName());
60 ConstraintLayout.LayoutParams lp =
61 (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
63 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 2);
64 holder.expanderContainer
65 .setVisibility(acc.hasSubAccounts() ? View.VISIBLE : View.INVISIBLE);
66 holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
67 holder.tvAccountAmounts.setText(acc.getAmountsString());
69 if (acc.isHiddenByStar()) {
70 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
71 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
74 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
75 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
78 holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
79 holder.selectionCb.setChecked(!acc.isHiddenByStarToBe());
81 holder.row.setTag(R.id.POS, position);
84 holder.vTrailer.setVisibility(View.VISIBLE);
85 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.get().size() + 1;
101 public void startSelection() {
102 for (LedgerAccount acc : Data.accounts.get()) acc.setHiddenByStarToBe(acc.isHiddenByStar());
103 this.selectionActive = true;
104 notifyDataSetChanged();
107 public void stopSelection() {
108 this.selectionActive = false;
109 notifyDataSetChanged();
112 public boolean isSelectionActive() {
113 return selectionActive;
116 public void selectItem(int position) {
117 LedgerAccount acc = Data.accounts.get().get(position);
118 acc.toggleHiddenToBe();
119 toggleChildrenOf(acc, acc.isHiddenByStarToBe(), position);
120 notifyItemChanged(position);
122 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
123 int i = parentPosition + 1;
124 for (LedgerAccount acc : Data.accounts.get()) {
125 if (acc.getName().startsWith(parent.getName() + ":")) {
126 acc.setHiddenByStarToBe(hiddenToBe);
127 notifyItemChanged(i);
128 toggleChildrenOf(acc, hiddenToBe, i);
134 class LedgerRowHolder extends RecyclerView.ViewHolder {
135 CheckBox selectionCb;
136 TextView tvAccountName, tvAccountAmounts;
137 ConstraintLayout row;
139 FrameLayout expanderContainer;
141 public LedgerRowHolder(@NonNull View itemView) {
143 this.row = itemView.findViewById(R.id.account_summary_row);
144 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
145 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
146 this.selectionCb = itemView.findViewById(R.id.account_row_check);
147 this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
148 this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
149 this.expander = itemView.findViewById(R.id.account_expander);
151 expanderContainer.addOnLayoutChangeListener(
152 (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
153 int w = right - left;
154 int h = bottom - top;
157 v.setPadding(0, p, 0, p);
159 else v.setPadding(0, 0, 0, 0);