]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/account_summary/AccountSummaryAdapter.java
2d7ea6ed6e3a8404749a781077b49d1ae4fd19de
[mobile-ledger-staging.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.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28
29 import androidx.annotation.NonNull;
30 import androidx.appcompat.app.AlertDialog;
31 import androidx.constraintlayout.widget.ConstraintLayout;
32 import androidx.recyclerview.widget.AsyncListDiffer;
33 import androidx.recyclerview.widget.DiffUtil;
34 import androidx.recyclerview.widget.RecyclerView;
35
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.async.DbOpQueue;
38 import net.ktnx.mobileledger.model.LedgerAccount;
39 import net.ktnx.mobileledger.model.MobileLedgerProfile;
40 import net.ktnx.mobileledger.ui.activity.MainActivity;
41 import net.ktnx.mobileledger.utils.Locker;
42 import net.ktnx.mobileledger.utils.Logger;
43
44 import org.jetbrains.annotations.NotNull;
45
46 import java.util.List;
47 import java.util.Locale;
48
49 import static net.ktnx.mobileledger.utils.Logger.debug;
50
51 public class AccountSummaryAdapter
52         extends RecyclerView.Adapter<AccountSummaryAdapter.LedgerRowHolder> {
53     public static final int AMOUNT_LIMIT = 3;
54     private AsyncListDiffer<LedgerAccount> listDiffer;
55     AccountSummaryAdapter() {
56         listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<LedgerAccount>() {
57             @Override
58             public boolean areItemsTheSame(@NotNull LedgerAccount oldItem,
59                                            @NotNull LedgerAccount newItem) {
60                 return TextUtils.equals(oldItem.getName(), newItem.getName());
61             }
62             @Override
63             public boolean areContentsTheSame(@NotNull LedgerAccount oldItem,
64                                               @NotNull LedgerAccount newItem) {
65                 return oldItem.equals(newItem);
66             }
67         });
68     }
69
70     public void onBindViewHolder(@NonNull LedgerRowHolder holder, int position) {
71         holder.bindToAccount(listDiffer.getCurrentList()
72                                        .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()
86                          .size();
87     }
88     public void setAccounts(List<LedgerAccount> newList) {
89         listDiffer.submitList(newList);
90     }
91     static class LedgerRowHolder extends RecyclerView.ViewHolder {
92         TextView tvAccountName, tvAccountAmounts;
93         ConstraintLayout row;
94         View expanderContainer;
95         ImageView expander;
96         View accountExpanderContainer;
97         LedgerAccount mAccount;
98         public LedgerRowHolder(@NonNull View itemView) {
99             super(itemView);
100
101             row = itemView.findViewById(R.id.account_summary_row);
102             tvAccountName = itemView.findViewById(R.id.account_row_acc_name);
103             tvAccountAmounts = itemView.findViewById(R.id.account_row_acc_amounts);
104             expanderContainer = itemView.findViewById(R.id.account_expander_container);
105             expander = itemView.findViewById(R.id.account_expander);
106             accountExpanderContainer =
107                     itemView.findViewById(R.id.account_row_amounts_expander_container);
108
109             itemView.setOnLongClickListener(this::onItemLongClick);
110             tvAccountName.setOnLongClickListener(this::onItemLongClick);
111             tvAccountAmounts.setOnLongClickListener(this::onItemLongClick);
112             expanderContainer.setOnLongClickListener(this::onItemLongClick);
113             expander.setOnLongClickListener(this::onItemLongClick);
114             row.setOnLongClickListener(this::onItemLongClick);
115
116             tvAccountName.setOnClickListener(v -> toggleAccountExpanded());
117             expanderContainer.setOnClickListener(v -> toggleAccountExpanded());
118             expander.setOnClickListener(v -> toggleAccountExpanded());
119             tvAccountAmounts.setOnClickListener(v -> toggleAmountsExpanded());
120         }
121         private void toggleAccountExpanded() {
122             if (!mAccount.hasSubAccounts())
123                 return;
124             debug("accounts", "Account expander clicked");
125
126             // make sure we use the same object as the one in the allAccounts list
127             MobileLedgerProfile profile = mAccount.getProfile();
128             if (profile == null) {
129                 return;
130             }
131             try (Locker ignored = profile.lockAccountsForWriting()) {
132                 LedgerAccount realAccount = profile.locateAccount(mAccount.getName());
133                 if (realAccount == null)
134                     return;
135
136                 mAccount = realAccount;
137                 mAccount.toggleExpanded();
138             }
139             expanderContainer.animate()
140                              .rotation(mAccount.isExpanded() ? 0 : 180);
141             profile.updateDisplayedAccounts();
142
143             DbOpQueue.add("update accounts set expanded=? where name=? and profile=?",
144                     new Object[]{mAccount.isExpanded(), mAccount.getName(), profile.getUuid()
145                     });
146
147         }
148         private void toggleAmountsExpanded() {
149             if (mAccount.getAmountCount() <= AMOUNT_LIMIT)
150                 return;
151
152             mAccount.toggleAmountsExpanded();
153             if (mAccount.amountsExpanded()) {
154                 tvAccountAmounts.setText(mAccount.getAmountsString());
155                 accountExpanderContainer.setVisibility(View.GONE);
156             }
157             else {
158                 tvAccountAmounts.setText(mAccount.getAmountsString(AMOUNT_LIMIT));
159                 accountExpanderContainer.setVisibility(View.VISIBLE);
160             }
161
162             MobileLedgerProfile profile = mAccount.getProfile();
163             if (profile == null)
164                 return;
165
166             DbOpQueue.add("update accounts set amounts_expanded=? where name=? and profile=?",
167                     new Object[]{mAccount.amountsExpanded(), mAccount.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             final String accountName = mAccount.getName();
175             builder.setTitle(accountName);
176             builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
177                 switch (which) {
178                     case 0:
179                         // show transactions
180                         activity.showAccountTransactions(accountName);
181                         break;
182                     default:
183                         throw new RuntimeException(
184                                 String.format("Unknown menu item id (%d)", which));
185                 }
186                 dialog.dismiss();
187             });
188             builder.show();
189             return true;
190         }
191         public void bindToAccount(LedgerAccount acc) {
192             Logger.debug("accounts", String.format(Locale.US, "Binding to '%s'", acc.getName()));
193             Context ctx = row.getContext();
194             Resources rm = ctx.getResources();
195             mAccount = acc;
196
197             row.setTag(acc);
198
199             tvAccountName.setText(acc.getShortName());
200
201             ConstraintLayout.LayoutParams lp =
202                     (ConstraintLayout.LayoutParams) tvAccountName.getLayoutParams();
203             lp.setMarginStart(
204                     acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
205
206             if (acc.hasSubAccounts()) {
207                 expanderContainer.setVisibility(View.VISIBLE);
208                 expanderContainer.setRotation(acc.isExpanded() ? 0 : 180);
209             }
210             else {
211                 expanderContainer.setVisibility(View.GONE);
212             }
213
214             int amounts = acc.getAmountCount();
215             if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
216                 tvAccountAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
217                 accountExpanderContainer.setVisibility(View.VISIBLE);
218             }
219             else {
220                 tvAccountAmounts.setText(acc.getAmountsString());
221                 accountExpanderContainer.setVisibility(View.GONE);
222             }
223         }
224     }
225 }