]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
3a528f5783e0634365ce6250f03e79dfeae41d89
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / account_summary / AccountSummaryAdapter.java
1 /*
2  * Copyright © 2020 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.text.TextUtils;
23 import android.util.Log;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.view.ViewGroup;
27 import android.widget.ImageView;
28 import android.widget.TextView;
29
30 import androidx.annotation.NonNull;
31 import androidx.appcompat.app.AlertDialog;
32 import androidx.constraintlayout.widget.ConstraintLayout;
33 import androidx.recyclerview.widget.AsyncListDiffer;
34 import androidx.recyclerview.widget.DiffUtil;
35 import androidx.recyclerview.widget.RecyclerView;
36
37 import net.ktnx.mobileledger.R;
38 import net.ktnx.mobileledger.async.DbOpQueue;
39 import net.ktnx.mobileledger.model.LedgerAccount;
40 import net.ktnx.mobileledger.model.MobileLedgerProfile;
41 import net.ktnx.mobileledger.ui.activity.MainActivity;
42
43 import org.jetbrains.annotations.NotNull;
44
45 import java.util.ArrayList;
46
47 import static net.ktnx.mobileledger.utils.Logger.debug;
48
49 public class AccountSummaryAdapter
50         extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
51     public static final int AMOUNT_LIMIT = 3;
52     private MobileLedgerProfile profile;
53     private AsyncListDiffer<LedgerAccount> listDiffer;
54     AccountSummaryAdapter() {
55         listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<LedgerAccount>() {
56             @Override
57             public boolean areItemsTheSame(@NotNull LedgerAccount oldItem,
58                                            @NotNull LedgerAccount newItem) {
59                 return TextUtils.equals(oldItem.getName(), newItem.getName());
60             }
61             @Override
62             public boolean areContentsTheSame(@NotNull LedgerAccount oldItem,
63                                               @NotNull LedgerAccount newItem) {
64                 return (oldItem.isExpanded() == newItem.isExpanded()) &&
65                        (oldItem.amountsExpanded() == newItem.amountsExpanded() &&
66                         TextUtils.equals(oldItem.getAmountsString(), newItem.getAmountsString()));
67             }
68         });
69     }
70
71     public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
72         holder.bindToAccount(listDiffer.getCurrentList().get(position));
73     }
74
75     @NonNull
76     @Override
77     public LedgerRowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
78         View row = LayoutInflater.from(parent.getContext())
79                                  .inflate(R.layout.account_summary_row, parent, false);
80         return new LedgerRowHolder(row);
81     }
82
83     @Override
84     public int getItemCount() {
85         return listDiffer.getCurrentList().size();
86     }
87     public void setAccounts(MobileLedgerProfile profile, ArrayList<LedgerAccount> newList) {
88         this.profile = profile;
89         listDiffer.submitList(newList);
90     }
91     class LedgerRowHolder extends RecyclerView.ViewHolder {
92         TextView tvAccountName, tvAccountAmounts;
93         ConstraintLayout row;
94         View expanderContainer;
95         ImageView expander;
96         View accountExpanderContainer;
97         public LedgerRowHolder(@NonNull View itemView) {
98             super(itemView);
99             row = itemView.findViewById(R.id.account_summary_row);
100             tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
101             tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
102             expanderContainer = itemView.findViewById(R.id.account_expander_container);
103             expander = itemView.findViewById(R.id.account_expander);
104             accountExpanderContainer =
105                     itemView.findViewById(R.id.account_row_amounts_expander_container);
106
107             itemView.setOnLongClickListener(this::onItemLongClick);
108             tvAccountName.setOnLongClickListener(this::onItemLongClick);
109             tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
110             expanderContainer.setOnLongClickListener(this::onItemLongClick);
111             expander.setOnLongClickListener(this::onItemLongClick);
112             row.setOnLongClickListener(this::onItemLongClick);
113
114             tvAccountName.setOnClickListener(v -> toggleAccountExpanded());
115             expanderContainer.setOnClickListener(v -> toggleAccountExpanded());
116             expander.setOnClickListener(v -> toggleAccountExpanded());
117             tvAccountAmounts.setOnClickListener(v -> toggleAmountsExpanded());
118         }
119         private @NonNull
120         LedgerAccount getAccount() {
121             final ArrayList<LedgerAccount> accountList = profile.getAccounts()
122                                                                 .getValue();
123             if (accountList == null)
124                 throw new IllegalStateException("No account list");
125
126             return accountList.get(getAdapterPosition());
127         }
128         private void toggleAccountExpanded() {
129             LedgerAccount acc = getAccount();
130             if (!acc.hasSubAccounts())
131                 return;
132             debug("accounts", "Account expander clicked");
133
134             acc.toggleExpanded();
135             expanderContainer.animate()
136                              .rotation(acc.isExpanded() ? 0 : 180);
137
138             MobileLedgerProfile profile = acc.getProfile();
139             if (profile == null)
140                 return;
141
142             DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
143                     new Object[]{acc.isExpanded(), acc.getName(), profile.getUuid()
144                     }, profile::scheduleAccountListReload);
145
146         }
147         private void toggleAmountsExpanded() {
148             LedgerAccount acc = getAccount();
149             if (acc.getAmountCount() <= AMOUNT_LIMIT)
150                 return;
151
152             acc.toggleAmountsExpanded();
153             if (acc.amountsExpanded()) {
154                 tvAccountAmounts.setText(acc.getAmountsString());
155                 accountExpanderContainer.setVisibility(View.GONE);
156             }
157             else {
158                 tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
159                 accountExpanderContainer.setVisibility(View.VISIBLE);
160             }
161
162             MobileLedgerProfile profile = acc.getProfile();
163             if (profile == null)
164                 return;
165
166             DbOpQueue.add("update accounts set amounts_expanded=? where name=? and profile=?",
167                     new Object[]{acc.amountsExpanded(), acc.getName(), profile.getUuid()
168                     });
169
170         }
171         private boolean onItemLongClick(View v) {
172             MainActivity activity = (MainActivity) v.getContext();
173             AlertDialog.Builder builder = new AlertDialog.Builder(activity);
174             View row;
175             int id = v.getId();
176             switch (id) {
177                 case R.id.account_summary_row:
178                     row = v;
179                     break;
180                 case R.id.account_row_acc_amounts:
181                 case R.id.account_row_amounts_expander_container:
182                     row = (View) v.getParent();
183                     break;
184                 case R.id.account_row_acc_name:
185                 case R.id.account_expander_container:
186                     row = (View) v.getParent()
187                                   .getParent();
188                     break;
189                 case R.id.account_expander:
190                     row = (View) v.getParent()
191                                   .getParent()
192                                   .getParent();
193                     break;
194                 default:
195                     Log.e("error",
196                             String.format("Don't know how to handle long click on id %d", id));
197                     return false;
198             }
199             LedgerAccount acc = getAccount();
200             builder.setTitle(acc.getName());
201             builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
202                 switch (which) {
203                     case 0:
204                         // show transactions
205                         activity.showAccountTransactions(acc.getName());
206                         break;
207                 }
208                 dialog.dismiss();
209             });
210             builder.show();
211             return true;
212         }
213         public void bindToAccount(LedgerAccount acc) {
214             Context ctx = row.getContext();
215             Resources rm = ctx.getResources();
216
217             row.setTag(acc);
218
219             tvAccountName.setText(acc.getShortName());
220
221             ConstraintLayout.LayoutParams lp =
222                     (ConstraintLayout.LayoutParams) tvAccountName.getLayoutParams();
223             lp.setMarginStart(
224                     acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
225
226             if (acc.hasSubAccounts()) {
227                 expanderContainer.setVisibility(View.VISIBLE);
228                 expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
229             }
230             else {
231                 expanderContainer.setVisibility(View.GONE);
232             }
233
234             int amounts = acc.getAmountCount();
235             if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
236                 tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
237                 accountExpanderContainer.setVisibility(View.VISIBLE);
238             }
239             else {
240                 tvAccountAmounts.setText(acc.getAmountsString());
241                 accountExpanderContainer.setVisibility(View.GONE);
242             }
243         }
244     }
245 }