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