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