]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
locks around observable list's access
[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 import net.ktnx.mobileledger.utils.LockHolder;
35
36 import androidx.annotation.NonNull;
37 import androidx.constraintlayout.widget.ConstraintLayout;
38 import androidx.recyclerview.widget.RecyclerView;
39
40 public class AccountSummaryAdapter
41         extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
42     private boolean selectionActive;
43
44     AccountSummaryAdapter() {
45         this.selectionActive = false;
46     }
47
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();
54
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();
61                 lp.setMarginStart(
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());
67
68                 if (acc.isHiddenByStar()) {
69                     holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
70                     holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
71                 }
72                 else {
73                     holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
74                     holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
75                 }
76
77                 holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
78                 holder.selectionCb.setChecked(!acc.isHiddenByStarToBe());
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
89     @NonNull
90     @Override
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);
95     }
96
97     @Override
98     public int getItemCount() {
99         return Data.accounts.size() + 1;
100     }
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());
106             }
107             this.selectionActive = true;
108             lh.downgrade();
109             notifyDataSetChanged();
110         }
111     }
112
113     public void stopSelection() {
114         this.selectionActive = false;
115         notifyDataSetChanged();
116     }
117
118     public boolean isSelectionActive() {
119         return selectionActive;
120     }
121
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);
128         }
129     }
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);
139                     i++;
140                 }
141             }
142         }
143     }
144
145     class LedgerRowHolder extends RecyclerView.ViewHolder {
146         CheckBox selectionCb;
147         TextView tvAccountName, tvAccountAmounts;
148         ConstraintLayout row;
149         View vTrailer;
150         FrameLayout expanderContainer;
151         ImageView expander;
152         public LedgerRowHolder(@NonNull View itemView) {
153             super(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);
161
162             expanderContainer.addOnLayoutChangeListener(
163                     (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
164                         int w = right - left;
165                         int h = bottom - top;
166                         if (h > w) {
167                             int p = (h - w) / 2;
168                             v.setPadding(0, p, 0, p);
169                         }
170                         else v.setPadding(0, 0, 0, 0);
171                     });
172         }
173     }
174 }