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.new_transaction;
20 import android.view.LayoutInflater;
21 import android.view.ViewGroup;
23 import androidx.annotation.NonNull;
24 import androidx.recyclerview.widget.AsyncListDiffer;
25 import androidx.recyclerview.widget.DiffUtil;
26 import androidx.recyclerview.widget.ItemTouchHelper;
27 import androidx.recyclerview.widget.RecyclerView;
29 import net.ktnx.mobileledger.databinding.NewTransactionRowBinding;
30 import net.ktnx.mobileledger.model.MobileLedgerProfile;
31 import net.ktnx.mobileledger.utils.Logger;
33 import java.util.List;
34 import java.util.Locale;
35 import java.util.Objects;
37 class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItemHolder> {
38 final NewTransactionModel model;
39 private final ItemTouchHelper touchHelper;
40 private final AsyncListDiffer<NewTransactionModel.Item> differ =
41 new AsyncListDiffer<>(this, new DiffUtil.ItemCallback<NewTransactionModel.Item>() {
43 public boolean areItemsTheSame(@NonNull NewTransactionModel.Item oldItem,
44 @NonNull NewTransactionModel.Item newItem) {
45 // Logger.debug("new-trans",
46 // String.format("comparing ids of {%s} and {%s}", oldItem.toString(),
47 // newItem.toString()));
48 return oldItem.getId() == newItem.getId();
51 public boolean areContentsTheSame(@NonNull NewTransactionModel.Item oldItem,
52 @NonNull NewTransactionModel.Item newItem) {
54 // Logger.debug("new-trans",
55 // String.format("comparing contents of {%s} and {%s}", oldItem
57 // newItem.toString()));
58 return oldItem.equalContents(newItem);
61 private MobileLedgerProfile mProfile;
62 private int checkHoldCounter = 0;
63 NewTransactionItemsAdapter(NewTransactionModel viewModel, MobileLedgerProfile profile) {
65 setHasStableIds(true);
70 NewTransactionItemsAdapter adapter = this;
72 touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
74 public boolean isLongPressDragEnabled() {
78 public boolean canDropOver(@NonNull RecyclerView recyclerView,
79 @NonNull RecyclerView.ViewHolder current,
80 @NonNull RecyclerView.ViewHolder target) {
81 final int adapterPosition = target.getAdapterPosition();
83 // first item is immovable
84 if (adapterPosition == 0)
87 return super.canDropOver(recyclerView, current, target);
90 public int getMovementFlags(@NonNull RecyclerView recyclerView,
91 @NonNull RecyclerView.ViewHolder viewHolder) {
92 int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
93 // the top (date and description) and the bottom (padding) items are always there
94 final int adapterPosition = viewHolder.getAdapterPosition();
95 if (adapterPosition > 0) {
96 flags |= makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
97 ItemTouchHelper.UP | ItemTouchHelper.DOWN) |
98 makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
99 ItemTouchHelper.START | ItemTouchHelper.END);
105 public boolean onMove(@NonNull RecyclerView recyclerView,
106 @NonNull RecyclerView.ViewHolder viewHolder,
107 @NonNull RecyclerView.ViewHolder target) {
109 model.moveItem(viewHolder.getAdapterPosition(), target.getAdapterPosition());
113 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
114 int pos = viewHolder.getAdapterPosition();
115 viewModel.removeItem(pos);
120 public long getItemId(int position) {
121 return differ.getCurrentList()
125 public void setProfile(MobileLedgerProfile profile) {
130 public NewTransactionItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
131 NewTransactionRowBinding b =
132 NewTransactionRowBinding.inflate(LayoutInflater.from(parent.getContext()), parent,
135 final NewTransactionItemHolder newHolder = new NewTransactionItemHolder(b, this);
136 Logger.debug("new-trans",
137 "Creating new ViewHolder " + Integer.toHexString(newHolder.hashCode()));
141 public void onBindViewHolder(@NonNull NewTransactionItemHolder holder, int position) {
143 String.format(Locale.US, "Binding item at position %d, holder %s", position,
144 Integer.toHexString(holder.hashCode())));
145 NewTransactionModel.Item item = Objects.requireNonNull(differ.getCurrentList()
148 Logger.debug("bind", String.format(Locale.US, "Bound %s item at position %d", item.getType()
153 public int getItemCount() {
154 return differ.getCurrentList()
158 public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
159 super.onAttachedToRecyclerView(recyclerView);
160 touchHelper.attachToRecyclerView(recyclerView);
163 public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
164 touchHelper.attachToRecyclerView(null);
165 super.onDetachedFromRecyclerView(recyclerView);
167 void noteFocusIsOnAccount(int position) {
168 model.noteFocusChanged(position, FocusedElement.Account);
170 void noteFocusIsOnAmount(int position) {
171 model.noteFocusChanged(position, FocusedElement.Amount);
173 void noteFocusIsOnComment(int position) {
174 model.noteFocusChanged(position, FocusedElement.Comment);
176 void noteFocusIsOnTransactionComment(int position) {
177 model.noteFocusChanged(position, FocusedElement.TransactionComment);
179 public void noteFocusIsOnDescription(int pos) {
180 model.noteFocusChanged(pos, FocusedElement.Description);
182 private void holdSubmittableChecks() {
185 private void releaseSubmittableChecks() {
186 if (checkHoldCounter == 0)
187 throw new RuntimeException("Asymmetrical call to releaseSubmittableChecks");
190 void setItemCurrency(int position, String newCurrency) {
191 model.setItemCurrency(position, newCurrency);
194 public void setItems(List<NewTransactionModel.Item> newList) {
195 Logger.debug("new-trans", "adapter: submitting new item list");
196 differ.submitList(newList);