]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
overly long commodity lists fade to white and expand/collapse on click
[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.ui.activity.MainActivity;
35 import net.ktnx.mobileledger.utils.LockHolder;
36
37 import androidx.annotation.NonNull;
38 import androidx.appcompat.app.AlertDialog;
39 import androidx.constraintlayout.widget.ConstraintLayout;
40 import androidx.recyclerview.widget.RecyclerView;
41
42 public class AccountSummaryAdapter
43         extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
44     public static final int AMOUNT_LIMIT = 3;
45     private boolean selectionActive;
46
47     AccountSummaryAdapter() {
48         this.selectionActive = false;
49     }
50
51     public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
52         try (LockHolder lh = Data.accounts.lockForReading()) {
53             if (position < Data.accounts.size()) {
54                 LedgerAccount acc = Data.accounts.get(position);
55                 Context ctx = holder.row.getContext();
56                 Resources rm = ctx.getResources();
57
58                 holder.row.setTag(acc);
59                 holder.row.setVisibility(View.VISIBLE);
60                 holder.vTrailer.setVisibility(View.GONE);
61                 holder.tvAccountName.setText(acc.getShortName());
62                 ConstraintLayout.LayoutParams lp =
63                         (ConstraintLayout.LayoutParams) holder.tvAccountName.getLayoutParams();
64                 lp.setMarginStart(
65                         acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 2);
66                 holder.expanderContainer
67                         .setVisibility(acc.hasSubAccounts() ? View.VISIBLE : View.INVISIBLE);
68                 holder.expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
69                 int amounts = acc.getAmountCount();
70                 if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
71                     holder.tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
72                     holder.accountExpanderContainer.setVisibility(View.VISIBLE);
73                 }
74                 else {
75                     holder.tvAccountAmounts.setText(acc.getAmountsString());
76                     holder.accountExpanderContainer.setVisibility(View.GONE);
77                 }
78
79                 if (acc.isHiddenByStar()) {
80                     holder.tvAccountName.setTypeface(null, Typeface.ITALIC);
81                     holder.tvAccountAmounts.setTypeface(null, Typeface.ITALIC);
82                 }
83                 else {
84                     holder.tvAccountName.setTypeface(null, Typeface.NORMAL);
85                     holder.tvAccountAmounts.setTypeface(null, Typeface.NORMAL);
86                 }
87
88                 holder.selectionCb.setVisibility(selectionActive ? View.VISIBLE : View.GONE);
89                 holder.selectionCb.setChecked(!acc.isHiddenByStarToBe());
90
91                 holder.row.setTag(R.id.POS, position);
92             }
93             else {
94                 holder.vTrailer.setVisibility(View.VISIBLE);
95                 holder.row.setVisibility(View.GONE);
96             }
97         }
98     }
99
100     @NonNull
101     @Override
102     public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
103         View row = LayoutInflater.from(parent.getContext())
104                 .inflate(R.layout.account_summary_row, parent, false);
105         return new LedgerRowHolder(row);
106     }
107
108     @Override
109     public int getItemCount() {
110         return Data.accounts.size() + 1;
111     }
112     public void startSelection() {
113         try (LockHolder lh = Data.accounts.lockForWriting()) {
114             for (int i = 0; i < Data.accounts.size(); i++) {
115                 LedgerAccount acc = Data.accounts.get(i);
116                 acc.setHiddenByStarToBe(acc.isHiddenByStar());
117             }
118             this.selectionActive = true;
119             lh.downgrade();
120             notifyDataSetChanged();
121         }
122     }
123
124     public void stopSelection() {
125         this.selectionActive = false;
126         notifyDataSetChanged();
127     }
128
129     public boolean isSelectionActive() {
130         return selectionActive;
131     }
132
133     public void selectItem(int position) {
134         try (LockHolder lh = Data.accounts.lockForWriting()) {
135             LedgerAccount acc = Data.accounts.get(position);
136             acc.toggleHiddenToBe();
137             toggleChildrenOf(acc, acc.isHiddenByStarToBe(), position);
138             notifyItemChanged(position);
139         }
140     }
141     void toggleChildrenOf(LedgerAccount parent, boolean hiddenToBe, int parentPosition) {
142         int i = parentPosition + 1;
143         try (LockHolder lh = Data.accounts.lockForWriting()) {
144             for (int j = 0; j < Data.accounts.size(); j++) {
145                 LedgerAccount acc = Data.accounts.get(j);
146                 if (acc.getName().startsWith(parent.getName() + ":")) {
147                     acc.setHiddenByStarToBe(hiddenToBe);
148                     notifyItemChanged(i);
149                     toggleChildrenOf(acc, hiddenToBe, i);
150                     i++;
151                 }
152             }
153         }
154     }
155
156     class LedgerRowHolder extends RecyclerView.ViewHolder {
157         CheckBox selectionCb;
158         TextView tvAccountName, tvAccountAmounts;
159         ConstraintLayout row;
160         View vTrailer;
161         FrameLayout expanderContainer;
162         ImageView expander;
163         FrameLayout accountExpanderContainer;
164         public LedgerRowHolder(@NonNull View itemView) {
165             super(itemView);
166             this.row = itemView.findViewById(R.id.account_summary_row);
167             this.tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
168             this.tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
169             this.selectionCb = itemView.findViewById(R.id.account_row_check);
170             this.vTrailer = itemView.findViewById(R.id.account_summary_trailer);
171             this.expanderContainer = itemView.findViewById(R.id.account_expander_container);
172             this.expander = itemView.findViewById(R.id.account_expander);
173             this.accountExpanderContainer = itemView.findViewById(R.id.account_row_amounts_expander_container);
174
175             MainActivity activity = (MainActivity) row.getContext();
176
177             expanderContainer.addOnLayoutChangeListener(
178                     (v, left, top, right, bottom, oldLeft, oldTop, oldRight, oldBottom) -> {
179                         int w = right - left;
180                         int h = bottom - top;
181                         if (h > w) {
182                             int p = (h - w) / 2;
183                             v.setPadding(0, p, 0, p);
184                         }
185                         else v.setPadding(0, 0, 0, 0);
186                     });
187
188             itemView.setOnLongClickListener(new View.OnLongClickListener() {
189                 @Override
190                 public boolean onLongClick(View v) {
191                     AlertDialog.Builder builder = new AlertDialog.Builder(itemView.getContext());
192                     LedgerAccount acc =
193                             (LedgerAccount) v.findViewById(R.id.account_summary_row).getTag();
194                     builder.setTitle(acc.getName());
195                     builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
196                         switch(which) {
197                             case 0:
198                                 // show transactions
199                                 activity.showAccountTransactions(acc);
200                                 break;
201                         }
202                         dialog.dismiss();
203                     });
204                     builder.show();
205                     return true;
206                 }
207             });
208         }
209     }
210 }