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 return new NewTransactionItemHolder(b, this);
138 public void onBindViewHolder(@NonNull NewTransactionItemHolder holder, int position) {
139 Logger.debug("bind", String.format(Locale.US, "Binding item at position %d", position));
140 NewTransactionModel.Item item = Objects.requireNonNull(differ.getCurrentList()
143 Logger.debug("bind", String.format(Locale.US, "Bound %s item at position %d", item.getType()
148 public int getItemCount() {
149 return differ.getCurrentList()
153 public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
154 super.onAttachedToRecyclerView(recyclerView);
155 touchHelper.attachToRecyclerView(recyclerView);
158 public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
159 touchHelper.attachToRecyclerView(null);
160 super.onDetachedFromRecyclerView(recyclerView);
162 void noteFocusIsOnAccount(int position) {
163 model.noteFocusChanged(position, FocusedElement.Account);
165 void noteFocusIsOnAmount(int position) {
166 model.noteFocusChanged(position, FocusedElement.Amount);
168 void noteFocusIsOnComment(int position) {
169 model.noteFocusChanged(position, FocusedElement.Comment);
171 void noteFocusIsOnTransactionComment(int position) {
172 model.noteFocusChanged(position, FocusedElement.TransactionComment);
174 public void noteFocusIsOnDescription(int pos) {
175 model.noteFocusChanged(pos, FocusedElement.Description);
177 private void holdSubmittableChecks() {
180 private void releaseSubmittableChecks() {
181 if (checkHoldCounter == 0)
182 throw new RuntimeException("Asymmetrical call to releaseSubmittableChecks");
185 void setItemCurrency(int position, String newCurrency) {
186 model.setItemCurrency(position, newCurrency);
189 public void setItems(List<NewTransactionModel.Item> newList) {
190 Logger.debug("new-trans", "adapter: submitting new item list");
191 differ.submitList(newList);