2 * Copyright © 2019 Damyan Ivanov.
3 * This file is part of Mobile-Ledger.
4 * Mobile-Ledger 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 * Mobile-Ledger 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 Mobile-Ledger. 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.os.Build;
24 import android.support.annotation.NonNull;
25 import android.support.v7.widget.RecyclerView;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 import android.widget.CheckBox;
30 import android.widget.LinearLayout;
31 import android.widget.TextView;
33 import net.ktnx.mobileledger.R;
34 import net.ktnx.mobileledger.model.Data;
35 import net.ktnx.mobileledger.model.LedgerAccount;
37 class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
38 private boolean selectionActive;
40 AccountSummaryAdapter() {
41 this.selectionActive = false;
44 public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
45 LedgerAccount acc = Data.accounts.get().get(position);
46 Context ctx = holder.row.getContext();
47 Resources rm = ctx.getResources();
49 holder.tvAccountName.setText(acc.getShortName());
50 holder.tvAccountName.setPadding(
51 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.activity_horizontal_margin) / 2,
53 holder.tvAccountAmounts.setText(acc.getAmountsString());
56 holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
57 holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
60 holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
61 holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
64 if (position % 2 == 0) {
65 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
66 .setBackgroundColor(rm.getColor(R.color.table_row_dark_bg, ctx.getTheme()));
67 else holder.row.setBackgroundColor(rm.getColor(R.color.table_row_dark_bg));
70 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) holder.row
71 .setBackgroundColor(rm.getColor(R.color.drawer_background, ctx.getTheme()));
72 else holder.row.setBackgroundColor(rm.getColor(R.color.drawer_background));
75 holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
76 holder.selectionCb.setChecked(!acc.isHiddenToBe());
78 holder.row.setTag(R.id.POS, position);
83 public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
84 View row = LayoutInflater.from(parent.getContext())
85 .inflate(R.layout.account_summary_row, parent, false);
86 return new LedgerRowHolder(row);
90 public int getItemCount() {
91 return Data.accounts.get().size();
93 public void startSelection() {
94 for (LedgerAccount acc : Data.accounts.get()) acc.setHiddenToBe(acc.isHidden());
95 this.selectionActive = true;
96 notifyDataSetChanged();
99 public void stopSelection() {
100 this.selectionActive = false;
101 notifyDataSetChanged();
104 public boolean isSelectionActive() {
105 return selectionActive;
108 public void selectItem(int position) {
109 LedgerAccount acc = Data.accounts.get().get(position);
110 acc.toggleHiddenToBe();
111 toggleChildrenOf(acc, acc.isHiddenToBe(), position);
112 notifyItemChanged(position);
114 void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
115 int i = parentPosition + 1;
116 for (LedgerAccount acc : Data.accounts.get()) {
117 if (acc.getName().startsWith(parent.getName() + ":")) {
118 acc.setHiddenToBe(hiddenToBe);
119 notifyItemChanged(i);
120 toggleChildrenOf(acc, hiddenToBe, i);
126 class LedgerRowHolder extends RecyclerView.ViewHolder {
127 CheckBox selectionCb;
128 TextView tvAccountName, tvAccountAmounts;
130 public LedgerRowHolder(@NonNull View itemView) {
132 this.row = (LinearLayout) itemView;
133 this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
134 this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
135 this.selectionCb = itemView.findViewById(R.id.account_row_check);