2 * Copyright © 2021 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.
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.
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/>.
18 package net.ktnx.mobileledger.ui.account_summary;
20 import android.content.res.Resources;
21 import android.os.AsyncTask;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
26 import androidx.annotation.NonNull;
27 import androidx.annotation.Nullable;
28 import androidx.appcompat.app.AlertDialog;
29 import androidx.constraintlayout.widget.ConstraintLayout;
30 import androidx.lifecycle.LifecycleOwner;
31 import androidx.recyclerview.widget.AsyncListDiffer;
32 import androidx.recyclerview.widget.DiffUtil;
33 import androidx.recyclerview.widget.RecyclerView;
35 import net.ktnx.mobileledger.R;
36 import net.ktnx.mobileledger.databinding.AccountListRowBinding;
37 import net.ktnx.mobileledger.databinding.AccountListSummaryRowBinding;
38 import net.ktnx.mobileledger.db.Account;
39 import net.ktnx.mobileledger.db.DB;
40 import net.ktnx.mobileledger.model.AccountListItem;
41 import net.ktnx.mobileledger.model.LedgerAccount;
42 import net.ktnx.mobileledger.ui.activity.MainActivity;
43 import net.ktnx.mobileledger.utils.Logger;
44 import net.ktnx.mobileledger.utils.Misc;
46 import org.jetbrains.annotations.NotNull;
48 import java.util.List;
49 import java.util.Locale;
51 import static net.ktnx.mobileledger.utils.Logger.debug;
53 public class AccountSummaryAdapter extends RecyclerView.Adapter<AccountSummaryAdapter.RowHolder> {
54 public static final int AMOUNT_LIMIT = 3;
55 private static final int ITEM_TYPE_HEADER = 1;
56 private static final int ITEM_TYPE_ACCOUNT = 2;
57 private final AsyncListDiffer<AccountListItem> listDiffer;
59 AccountSummaryAdapter() {
60 setHasStableIds(true);
62 listDiffer = new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<AccountListItem>() {
65 public Object getChangePayload(@NonNull AccountListItem oldItem,
66 @NonNull AccountListItem newItem) {
67 Change changes = new Change();
69 final LedgerAccount oldAcc = oldItem.getAccount();
70 final LedgerAccount newAcc = newItem.getAccount();
72 if (!Misc.equalStrings(oldAcc.getName(), newAcc.getName()))
73 changes.add(Change.NAME);
75 if (oldAcc.getLevel() != newAcc.getLevel())
76 changes.add(Change.LEVEL);
78 if (oldAcc.isExpanded() != newAcc.isExpanded())
79 changes.add(Change.EXPANDED);
81 if (oldAcc.amountsExpanded() != newAcc.amountsExpanded())
82 changes.add(Change.EXPANDED_AMOUNTS);
84 if (!oldAcc.getAmountsString()
85 .equals(newAcc.getAmountsString()))
86 changes.add(Change.AMOUNTS);
88 return changes.toPayload();
91 public boolean areItemsTheSame(@NotNull AccountListItem oldItem,
92 @NotNull AccountListItem newItem) {
93 final AccountListItem.Type oldType = oldItem.getType();
94 final AccountListItem.Type newType = newItem.getType();
95 if (oldType != newType)
97 if (oldType == AccountListItem.Type.HEADER)
100 return oldItem.getAccount()
101 .getId() == newItem.getAccount()
105 public boolean areContentsTheSame(@NotNull AccountListItem oldItem,
106 @NotNull AccountListItem newItem) {
107 return oldItem.sameContent(newItem);
112 public long getItemId(int position) {
115 return listDiffer.getCurrentList()
121 public void onBindViewHolder(@NonNull RowHolder holder, int position,
122 @NonNull List<Object> payloads) {
123 holder.bind(listDiffer.getCurrentList()
124 .get(position), payloads);
125 super.onBindViewHolder(holder, position, payloads);
127 public void onBindViewHolder(@NonNull RowHolder holder, int position) {
128 holder.bind(listDiffer.getCurrentList()
129 .get(position), null);
133 public RowHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
134 final LayoutInflater inflater = LayoutInflater.from(parent.getContext());
136 final RowHolder result;
138 case ITEM_TYPE_HEADER:
139 result = new HeaderRowHolder(
140 AccountListSummaryRowBinding.inflate(inflater, parent, false));
142 case ITEM_TYPE_ACCOUNT:
143 result = new AccountRowHolder(
144 AccountListRowBinding.inflate(inflater, parent, false));
147 throw new IllegalStateException("Unexpected value: " + viewType);
150 // Logger.debug("acc-ui", "Creating " + result);
154 public int getItemCount() {
155 return listDiffer.getCurrentList()
159 public int getItemViewType(int position) {
160 return (position == 0) ? ITEM_TYPE_HEADER : ITEM_TYPE_ACCOUNT;
162 public void setAccounts(List<AccountListItem> newList) {
163 Misc.onMainThread(() -> listDiffer.submitList(newList));
165 static class Change {
166 static final int NAME = 1;
167 static final int EXPANDED = 1 << 1;
168 static final int LEVEL = 1 << 2;
169 static final int EXPANDED_AMOUNTS = 1 << 3;
170 static final int AMOUNTS = 1 << 4;
171 private int value = 0;
174 public Change(int initialValue) {
175 value = initialValue;
177 public void add(int bits) {
178 value = value | bits;
180 public void add(Change change) {
181 value = value | change.value;
183 public void remove(int bits) {
184 value = value & (~bits);
186 public void remove(Change change) {
187 value = value & (~change.value);
189 public Change toPayload() {
194 public boolean has(int bits) {
195 return value == 0 || (value & bits) == bits;
199 static abstract class RowHolder extends RecyclerView.ViewHolder {
200 public RowHolder(@NonNull View itemView) {
203 public abstract void bind(AccountListItem accountListItem, @Nullable List<Object> payloads);
206 static class HeaderRowHolder extends RowHolder {
207 private final AccountListSummaryRowBinding b;
208 public HeaderRowHolder(@NonNull AccountListSummaryRowBinding binding) {
209 super(binding.getRoot());
213 public void bind(AccountListItem item, @Nullable List<Object> payloads) {
214 Resources r = itemView.getResources();
215 // Logger.debug("acc", itemView.getContext()
217 ((AccountListItem.Header) item).getText()
218 .observe((LifecycleOwner) itemView.getContext(),
219 b.lastUpdateText::setText);
223 class AccountRowHolder extends AccountSummaryAdapter.RowHolder {
224 private final AccountListRowBinding b;
225 public AccountRowHolder(@NonNull AccountListRowBinding binding) {
226 super(binding.getRoot());
229 itemView.setOnLongClickListener(this::onItemLongClick);
230 b.accountRowAccName.setOnLongClickListener(this::onItemLongClick);
231 b.accountRowAccAmounts.setOnLongClickListener(this::onItemLongClick);
232 b.accountExpanderContainer.setOnLongClickListener(this::onItemLongClick);
233 b.accountExpander.setOnLongClickListener(this::onItemLongClick);
235 b.accountRowAccName.setOnClickListener(v -> toggleAccountExpanded());
236 b.accountExpanderContainer.setOnClickListener(v -> toggleAccountExpanded());
237 b.accountExpander.setOnClickListener(v -> toggleAccountExpanded());
238 b.accountRowAccAmounts.setOnClickListener(v -> toggleAmountsExpanded());
240 private void toggleAccountExpanded() {
241 LedgerAccount account = getAccount();
242 if (!account.hasSubAccounts())
244 debug("accounts", "Account expander clicked");
246 AsyncTask.execute(() -> {
247 Account dbo = account.toDBO();
248 dbo.setExpanded(!dbo.isExpanded());
249 Logger.debug("accounts",
250 String.format(Locale.ROOT, "%s (%d) → %s", account.getName(), dbo.getId(),
251 dbo.isExpanded() ? "expanded" : "collapsed"));
258 private LedgerAccount getAccount() {
259 return listDiffer.getCurrentList()
260 .get(getBindingAdapterPosition())
263 private void toggleAmountsExpanded() {
264 LedgerAccount account = getAccount();
265 if (account.getAmountCount() <= AMOUNT_LIMIT)
268 account.toggleAmountsExpanded();
269 if (account.amountsExpanded()) {
270 b.accountRowAccAmounts.setText(account.getAmountsString());
271 b.accountRowAmountsExpanderContainer.setVisibility(View.GONE);
274 b.accountRowAccAmounts.setText(account.getAmountsString(AMOUNT_LIMIT));
275 b.accountRowAmountsExpanderContainer.setVisibility(View.VISIBLE);
278 AsyncTask.execute(() -> {
279 Account dbo = account.toDBO();
285 private boolean onItemLongClick(View v) {
286 MainActivity activity = (MainActivity) v.getContext();
287 AlertDialog.Builder builder = new AlertDialog.Builder(activity);
288 final String accountName = getAccount().getName();
289 builder.setTitle(accountName);
290 builder.setItems(R.array.acc_ctx_menu, (dialog, which) -> {
291 if (which == 0) {// show transactions
292 activity.showAccountTransactions(accountName);
295 throw new RuntimeException(String.format("Unknown menu item id (%d)", which));
303 public void bind(AccountListItem item, @Nullable List<Object> payloads) {
304 LedgerAccount acc = item.getAccount();
306 Change changes = new Change();
307 if (payloads != null) {
308 for (Object p : payloads) {
309 if (p instanceof Change)
310 changes.add((Change) p);
314 // String.format(Locale.US, "Binding '%s' to %s", acc.getName(), this));
316 Resources rm = b.getRoot()
320 if (changes.has(Change.NAME))
321 b.accountRowAccName.setText(acc.getShortName());
323 if (changes.has(Change.LEVEL)) {
324 ConstraintLayout.LayoutParams lp =
325 (ConstraintLayout.LayoutParams) b.flowWrapper.getLayoutParams();
327 acc.getLevel() * rm.getDimensionPixelSize(R.dimen.thumb_row_height) / 3);
330 if (acc.hasSubAccounts()) {
331 b.accountExpanderContainer.setVisibility(View.VISIBLE);
333 if (changes.has(Change.EXPANDED)) {
334 int wantedRotation = acc.isExpanded() ? 0 : 180;
335 if (b.accountExpanderContainer.getRotation() != wantedRotation) {
336 // Logger.debug("acc-ui",
337 // String.format(Locale.ROOT, "Rotating %s to %d", acc.getName(),
339 b.accountExpanderContainer.animate()
340 .rotation(wantedRotation);
345 b.accountExpanderContainer.setVisibility(View.GONE);
348 if (changes.has(Change.EXPANDED_AMOUNTS)) {
349 int amounts = acc.getAmountCount();
350 if ((amounts > AMOUNT_LIMIT) && !acc.amountsExpanded()) {
351 b.accountRowAccAmounts.setText(acc.getAmountsString(AMOUNT_LIMIT));
352 b.accountRowAmountsExpanderContainer.setVisibility(View.VISIBLE);
355 b.accountRowAccAmounts.setText(acc.getAmountsString());
356 b.accountRowAmountsExpanderContainer.setVisibility(View.GONE);