]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/new_transaction/NewTransactionItemsAdapter.java
format amounts when the input field loses focus
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / new_transaction / NewTransactionItemsAdapter.java
1 /*
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.
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.new_transaction;
19
20 import android.view.LayoutInflater;
21 import android.view.ViewGroup;
22
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;
28
29 import net.ktnx.mobileledger.databinding.NewTransactionRowBinding;
30 import net.ktnx.mobileledger.model.MobileLedgerProfile;
31 import net.ktnx.mobileledger.utils.Logger;
32
33 import java.util.List;
34 import java.util.Locale;
35 import java.util.Objects;
36
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>() {
42                 @Override
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();
49                 }
50                 @Override
51                 public boolean areContentsTheSame(@NonNull NewTransactionModel.Item oldItem,
52                                                   @NonNull NewTransactionModel.Item newItem) {
53
54 //                    Logger.debug("new-trans",
55 //                            String.format("comparing contents of {%s} and {%s}", oldItem
56 //                            .toString(),
57 //                                    newItem.toString()));
58                     return oldItem.equalContents(newItem);
59                 }
60             });
61     private MobileLedgerProfile mProfile;
62     private int checkHoldCounter = 0;
63     NewTransactionItemsAdapter(NewTransactionModel viewModel, MobileLedgerProfile profile) {
64         super();
65         setHasStableIds(true);
66         model = viewModel;
67         mProfile = profile;
68
69
70         NewTransactionItemsAdapter adapter = this;
71
72         touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
73             @Override
74             public boolean isLongPressDragEnabled() {
75                 return true;
76             }
77             @Override
78             public boolean canDropOver(@NonNull RecyclerView recyclerView,
79                                        @NonNull RecyclerView.ViewHolder current,
80                                        @NonNull RecyclerView.ViewHolder target) {
81                 final int adapterPosition = target.getAdapterPosition();
82
83                 // first item is immovable
84                 if (adapterPosition == 0)
85                     return false;
86
87                 return super.canDropOver(recyclerView, current, target);
88             }
89             @Override
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);
100                 }
101
102                 return flags;
103             }
104             @Override
105             public boolean onMove(@NonNull RecyclerView recyclerView,
106                                   @NonNull RecyclerView.ViewHolder viewHolder,
107                                   @NonNull RecyclerView.ViewHolder target) {
108
109                 model.moveItem(viewHolder.getAdapterPosition(), target.getAdapterPosition());
110                 return true;
111             }
112             @Override
113             public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
114                 int pos = viewHolder.getAdapterPosition();
115                 viewModel.removeItem(pos);
116             }
117         });
118     }
119     @Override
120     public long getItemId(int position) {
121         return differ.getCurrentList()
122                      .get(position)
123                      .getId();
124     }
125     public void setProfile(MobileLedgerProfile profile) {
126         mProfile = profile;
127     }
128     @NonNull
129     @Override
130     public NewTransactionItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
131         NewTransactionRowBinding b =
132                 NewTransactionRowBinding.inflate(LayoutInflater.from(parent.getContext()), parent,
133                         false);
134
135         return new NewTransactionItemHolder(b, this);
136     }
137     @Override
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()
141                                                                      .get(position));
142         holder.bind(item);
143         Logger.debug("bind", String.format(Locale.US, "Bound %s item at position %d", item.getType()
144                                                                                           .toString(),
145                 position));
146     }
147     @Override
148     public int getItemCount() {
149         return differ.getCurrentList()
150                      .size();
151     }
152     @Override
153     public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
154         super.onAttachedToRecyclerView(recyclerView);
155         touchHelper.attachToRecyclerView(recyclerView);
156     }
157     @Override
158     public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
159         touchHelper.attachToRecyclerView(null);
160         super.onDetachedFromRecyclerView(recyclerView);
161     }
162     void noteFocusIsOnAccount(int position) {
163         model.noteFocusChanged(position, FocusedElement.Account);
164     }
165     void noteFocusIsOnAmount(int position) {
166         model.noteFocusChanged(position, FocusedElement.Amount);
167     }
168     void noteFocusIsOnComment(int position) {
169         model.noteFocusChanged(position, FocusedElement.Comment);
170     }
171     void noteFocusIsOnTransactionComment(int position) {
172         model.noteFocusChanged(position, FocusedElement.TransactionComment);
173     }
174     public void noteFocusIsOnDescription(int pos) {
175         model.noteFocusChanged(pos, FocusedElement.Description);
176     }
177     private void holdSubmittableChecks() {
178         checkHoldCounter++;
179     }
180     private void releaseSubmittableChecks() {
181         if (checkHoldCounter == 0)
182             throw new RuntimeException("Asymmetrical call to releaseSubmittableChecks");
183         checkHoldCounter--;
184     }
185     void setItemCurrency(int position, String newCurrency) {
186         model.setItemCurrency(position, newCurrency);
187     }
188
189     public void setItems(List<NewTransactionModel.Item> newList) {
190         Logger.debug("new-trans", "adapter: submitting new item list");
191         differ.submitList(newList);
192     }
193 }