]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
use Colors.* for run-time color control
[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.support.annotation.NonNull;
24 import android.support.v7.widget.RecyclerView;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.widget.CheckBox;
29 import android.widget.LinearLayout;
30 import android.widget.TextView;
31
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.utils.Colors;
36
37 import java.util.List;
38
39 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
40     private boolean selectionActive;
41
42     AccountSummaryAdapter() {
43         this.selectionActive = false;
44     }
45
46     public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
47         List<LedgerAccount> accounts = Data.accounts.get();
48         if (position < accounts.size()) {
49             LedgerAccount acc = accounts.get(position);
50             Context ctx = holder.row.getContext();
51             Resources rm = ctx.getResources();
52
53             holder.row.setVisibility(View.VISIBLE);
54             holder.vTrailer.setVisibility(View.GONE);
55             holder.tvAccountName.setText(acc.getShortName());
56             holder.tvAccountName.setPadding(
57                     acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin) /
58                     2, 0, 0, 0);
59             holder.tvAccountAmounts.setText(acc.getAmountsString());
60
61             if (acc.isHidden()) {
62                 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
63                 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
64             }
65             else {
66                 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
67                 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
68             }
69
70             if (position % 2 == 0) {
71                 holder.row.setBackgroundColor(Colors.tableRowDarkBG);
72             }
73             else {
74                 holder.row.setBackgroundColor(Colors.tableRowLightBG);
75             }
76
77             holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
78             holder.selectionCb.setChecked(!acc.isHiddenToBe());
79
80             holder.row.setTag(R.id.POS, position);
81         }
82         else {
83             holder.vTrailer.setVisibility(View.VISIBLE);
84             holder.row.setVisibility(View.GONE);
85         }
86     }
87
88     @NonNull
89     @Override
90     public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
91         View row = LayoutInflater.from(parent.getContext())
92                 .inflate(R.layout.account_summary_row, parent, false);
93         return new LedgerRowHolder(row);
94     }
95
96     @Override
97     public int getItemCount() {
98         return Data.accounts.get().size() + 1;
99     }
100     public void startSelection() {
101         for (LedgerAccount acc : Data.accounts.get()) acc.setHiddenToBe(acc.isHidden());
102         this.selectionActive = true;
103         notifyDataSetChanged();
104     }
105
106     public void stopSelection() {
107         this.selectionActive = false;
108         notifyDataSetChanged();
109     }
110
111     public boolean isSelectionActive() {
112         return selectionActive;
113     }
114
115     public void selectItem(int position) {
116         LedgerAccount acc = Data.accounts.get().get(position);
117         acc.toggleHiddenToBe();
118         toggleChildrenOf(acc, acc.isHiddenToBe(), position);
119         notifyItemChanged(position);
120     }
121     void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
122         int i = parentPosition + 1;
123         for (LedgerAccount acc : Data.accounts.get()) {
124             if (acc.getName().startsWith(parent.getName() + ":")) {
125                 acc.setHiddenToBe(hiddenToBe);
126                 notifyItemChanged(i);
127                 toggleChildrenOf(acc, hiddenToBe, i);
128                 i++;
129             }
130         }
131     }
132
133     class LedgerRowHolder extends RecyclerView.ViewHolder {
134         CheckBox selectionCb;
135         TextView tvAccountName, tvAccountAmounts;
136         LinearLayout row;
137         View vTrailer;
138         public LedgerRowHolder(@NonNull View itemView) {
139             super(itemView);
140             this.row = itemView.findViewById(R.id.account_summary_row);
141             this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
142             this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
143             this.selectionCb = itemView.findViewById(R.id.account_row_check);
144             this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
145         }
146     }
147 }