]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
3778552503f3c833915779908432167bc4420f4d
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryAdapter.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.account_summary;
19
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;
30
31 import net.ktnx.mobileledger.R;
32 import net.ktnx.mobileledger.model.Data;
33 import net.ktnx.mobileledger.model.LedgerAccount;
34
35 import androidx.annotation.NonNull;
36 import androidx.constraintlayout.widget.ConstraintLayout;
37 import androidx.recyclerview.widget.RecyclerView;
38
39 public class AccountSummaryAdapter
40         extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
41     private boolean selectionActive;
42
43     AccountSummaryAdapter() {
44         this.selectionActive = false;
45     }
46
47     public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
48         if (position < Data.accounts.size()) {
49             LedgerAccount acc = Data.accounts.get(position);
50             Context ctx = holder.row.getContext();
51             Resources rm = ctx.getResources();
52
53             holder.row.setTag(acc);
54             holder.row.setVisibility(View.VISIBLE);
55             holder.vTrailer.setVisibility(View.GONE);
56             holder.tvAccountName.setText(acc.getShortName());
57             ConstraintLayout.LayoutParams lp =
58                     (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
59             lp.setMarginStart(
60                     acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 2);
61             holder.expanderContainer
62                     .setVisibility(acc.hasSubAccounts() ? View.VISIBLE : View.INVISIBLE);
63             holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
64             holder.tvAccountAmounts.setText(acc.getAmountsString());
65
66             if (acc.isHiddenByStar()) {
67                 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
68                 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
69             }
70             else {
71                 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
72                 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
73             }
74
75             holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
76             holder.selectionCb.setChecked(!acc.isHiddenByStarToBe());
77
78             holder.row.setTag(R.id.POS, position);
79         }
80         else {
81             holder.vTrailer.setVisibility(View.VISIBLE);
82             holder.row.setVisibility(View.GONE);
83         }
84     }
85
86     @NonNull
87     @Override
88     public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
89         View row = LayoutInflater.from(parent.getContext())
90                 .inflate(R.layout.account_summary_row, parent, false);
91         return new LedgerRowHolder(row);
92     }
93
94     @Override
95     public int getItemCount() {
96         return Data.accounts.size() + 1;
97     }
98     public void startSelection() {
99         for (int i = 0; i < Data.accounts.size(); i++ ) {
100             LedgerAccount acc = Data.accounts.get(i);
101             acc.setHiddenByStarToBe(acc.isHiddenByStar());
102         }
103         this.selectionActive = true;
104         notifyDataSetChanged();
105     }
106
107     public void stopSelection() {
108         this.selectionActive = false;
109         notifyDataSetChanged();
110     }
111
112     public boolean isSelectionActive() {
113         return selectionActive;
114     }
115
116     public void selectItem(int position) {
117         LedgerAccount acc = Data.accounts.get(position);
118         acc.toggleHiddenToBe();
119         toggleChildrenOf(acc, acc.isHiddenByStarToBe(), position);
120         notifyItemChanged(position);
121     }
122     void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
123         int i = parentPosition + 1;
124         for (int j = 0; j < Data.accounts.size(); j++) {
125             LedgerAccount acc = Data.accounts.get(j);
126             if (acc.getName().startsWith(parent.getName() + ":")) {
127                 acc.setHiddenByStarToBe(hiddenToBe);
128                 notifyItemChanged(i);
129                 toggleChildrenOf(acc, hiddenToBe, i);
130                 i++;
131             }
132         }
133     }
134
135     class LedgerRowHolder extends RecyclerView.ViewHolder {
136         CheckBox selectionCb;
137         TextView tvAccountName, tvAccountAmounts;
138         ConstraintLayout row;
139         View vTrailer;
140         FrameLayout expanderContainer;
141         ImageView expander;
142         public LedgerRowHolder(@NonNull View itemView) {
143             super(itemView);
144             this.row = itemView.findViewById(R.id.account_summary_row);
145             this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
146             this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
147             this.selectionCb = itemView.findViewById(R.id.account_row_check);
148             this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
149             this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
150             this.expander = itemView.findViewById(R.id.account_expander);
151
152             expanderContainer.addOnLayoutChangeListener(
153                     (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
154                         int w = right - left;
155                         int h = bottom - top;
156                         if (h > w) {
157                             int p = (h - w) / 2;
158                             v.setPadding(0, p, 0, p);
159                         }
160                         else v.setPadding(0, 0, 0, 0);
161                     });
162         }
163     }
164 }