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