2 * Copyright © 2019 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.activity;
20 import android.annotation.SuppressLint;
21 import android.app.Activity;
22 import android.database.Cursor;
23 import android.view.LayoutInflater;
24 import android.view.ViewGroup;
25 import android.widget.LinearLayout;
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.recyclerview.widget.ItemTouchHelper;
30 import androidx.recyclerview.widget.RecyclerView;
32 import net.ktnx.mobileledger.App;
33 import net.ktnx.mobileledger.BuildConfig;
34 import net.ktnx.mobileledger.R;
35 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
36 import net.ktnx.mobileledger.model.Currency;
37 import net.ktnx.mobileledger.model.Data;
38 import net.ktnx.mobileledger.model.LedgerTransaction;
39 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
40 import net.ktnx.mobileledger.model.MobileLedgerProfile;
41 import net.ktnx.mobileledger.utils.Logger;
42 import net.ktnx.mobileledger.utils.MLDB;
43 import net.ktnx.mobileledger.utils.Misc;
45 import java.util.ArrayList;
46 import java.util.HashMap;
47 import java.util.List;
48 import java.util.Locale;
51 import static net.ktnx.mobileledger.utils.Logger.debug;
53 class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItemHolder>
54 implements DescriptionSelectedCallback {
55 private NewTransactionModel model;
56 private MobileLedgerProfile mProfile;
57 private ItemTouchHelper touchHelper;
58 private RecyclerView recyclerView;
59 private int checkHoldCounter = 0;
60 NewTransactionItemsAdapter(NewTransactionModel viewModel, MobileLedgerProfile profile) {
64 int size = model.getAccountCount();
66 Logger.debug("new-transaction",
67 String.format(Locale.US, "%d accounts is too little, Calling addRow()", size));
71 NewTransactionItemsAdapter adapter = this;
73 touchHelper = new ItemTouchHelper(new ItemTouchHelper.Callback() {
75 public boolean isLongPressDragEnabled() {
79 public boolean canDropOver(@NonNull RecyclerView recyclerView,
80 @NonNull RecyclerView.ViewHolder current,
81 @NonNull RecyclerView.ViewHolder target) {
82 final int adapterPosition = target.getAdapterPosition();
84 // first and last items are immovable
85 if (adapterPosition == 0)
87 if (adapterPosition == adapter.getItemCount() - 1)
90 return super.canDropOver(recyclerView, current, target);
93 public int getMovementFlags(@NonNull RecyclerView recyclerView,
94 @NonNull RecyclerView.ViewHolder viewHolder) {
95 int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
96 // the top (date and description) and the bottom (padding) items are always there
97 final int adapterPosition = viewHolder.getAdapterPosition();
98 if ((adapterPosition > 0) && (adapterPosition < adapter.getItemCount() - 1)) {
99 flags |= makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
100 ItemTouchHelper.UP | ItemTouchHelper.DOWN) |
101 makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
102 ItemTouchHelper.START | ItemTouchHelper.END);
108 public boolean onMove(@NonNull RecyclerView recyclerView,
109 @NonNull RecyclerView.ViewHolder viewHolder,
110 @NonNull RecyclerView.ViewHolder target) {
112 model.swapItems(viewHolder.getAdapterPosition(), target.getAdapterPosition());
113 notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
117 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
118 int pos = viewHolder.getAdapterPosition();
119 viewModel.removeItem(pos - 1);
120 notifyItemRemoved(pos);
121 viewModel.sendCountNotifications(); // needed after items re-arrangement
122 checkTransactionSubmittable();
126 public void setProfile(MobileLedgerProfile profile) {
129 private int addRow() {
132 private int addRow(String commodity) {
133 final int newAccountCount = model.addAccount(new LedgerTransactionAccount("", commodity));
134 Logger.debug("new-transaction",
135 String.format(Locale.US, "invoking notifyItemInserted(%d)", newAccountCount));
136 // the header is at position 0
137 notifyItemInserted(newAccountCount);
138 model.sendCountNotifications(); // needed after holders' positions have changed
139 return newAccountCount;
143 public NewTransactionItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
144 LinearLayout row = (LinearLayout) LayoutInflater.from(parent.getContext())
145 .inflate(R.layout.new_transaction_row,
148 return new NewTransactionItemHolder(row, this);
151 public void onBindViewHolder(@NonNull NewTransactionItemHolder holder, int position) {
152 Logger.debug("bind", String.format(Locale.US, "Binding item at position %d", position));
153 NewTransactionModel.Item item = model.getItem(position);
154 holder.setData(item);
155 Logger.debug("bind", String.format(Locale.US, "Bound %s item at position %d", item.getType()
160 public int getItemCount() {
161 return model.getAccountCount() + 2;
163 private boolean accountListIsEmpty() {
164 for (int i = 0; i < model.getAccountCount(); i++) {
165 LedgerTransactionAccount acc = model.getAccount(i);
166 if (!acc.getAccountName()
169 if (acc.isAmountSet())
176 public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
177 super.onAttachedToRecyclerView(recyclerView);
178 this.recyclerView = recyclerView;
179 touchHelper.attachToRecyclerView(recyclerView);
182 public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
183 touchHelper.attachToRecyclerView(null);
184 super.onDetachedFromRecyclerView(recyclerView);
185 this.recyclerView = null;
187 public void descriptionSelected(String description) {
188 debug("descr selected", description);
189 if (!accountListIsEmpty())
192 String accFilter = mProfile.getPreferredAccountsFilter();
194 ArrayList<String> params = new ArrayList<>();
195 StringBuilder sb = new StringBuilder("select t.profile, t.id from transactions t");
197 if (!Misc.isEmptyOrNull(accFilter)) {
198 sb.append(" JOIN transaction_accounts ta")
199 .append(" ON ta.profile = t.profile")
200 .append(" AND ta.transaction_id = t.id");
203 sb.append(" WHERE t.description=?");
204 params.add(description);
206 if (!Misc.isEmptyOrNull(accFilter)) {
207 sb.append(" AND ta.account_name LIKE '%'||?||'%'");
208 params.add(accFilter);
211 sb.append(" ORDER BY t.date DESC LIMIT 1");
213 final String sql = sb.toString();
215 debug("descr", params.toString());
217 Activity activity = (Activity) recyclerView.getContext();
218 // FIXME: handle exceptions?
219 MLDB.queryInBackground(sql, params.toArray(new String[]{}), new MLDB.CallbackHelper() {
221 public void onStart() {
222 model.incrementBusyCounter();
225 public void onDone() {
226 model.decrementBusyCounter();
229 public boolean onRow(@NonNull Cursor cursor) {
230 final String profileUUID = cursor.getString(0);
231 final int transactionId = cursor.getInt(1);
232 activity.runOnUiThread(() -> loadTransactionIntoModel(profileUUID, transactionId));
233 return false; // limit 1, by the way
236 public void onNoRows() {
237 if (Misc.isEmptyOrNull(accFilter))
240 debug("descr", "Trying transaction search without preferred account filter");
242 final String broaderSql =
243 "select t.profile, t.id from transactions t where t.description=?" +
244 " ORDER BY date desc LIMIT 1";
246 debug("descr", broaderSql);
247 debug("descr", description);
249 MLDB.queryInBackground(broaderSql, new String[]{description},
250 new MLDB.CallbackHelper() {
252 public void onStart() {
253 model.incrementBusyCounter();
256 public boolean onRow(@NonNull Cursor cursor) {
257 final String profileUUID = cursor.getString(0);
258 final int transactionId = cursor.getInt(1);
259 activity.runOnUiThread(
260 () -> loadTransactionIntoModel(profileUUID, transactionId));
264 public void onDone() {
265 model.decrementBusyCounter();
271 private void loadTransactionIntoModel(String profileUUID, int transactionId) {
272 LedgerTransaction tr;
273 MobileLedgerProfile profile = Data.getProfile(profileUUID);
275 throw new RuntimeException(String.format(
276 "Unable to find profile %s, which is supposed to contain transaction %d",
277 profileUUID, transactionId));
279 tr = profile.loadTransaction(transactionId);
280 ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
281 NewTransactionModel.Item firstNegative = null;
282 NewTransactionModel.Item firstPositive = null;
283 int singleNegativeIndex = -1;
284 int singlePositiveIndex = -1;
285 int negativeCount = 0;
286 for (int i = 0; i < accounts.size(); i++) {
287 LedgerTransactionAccount acc = accounts.get(i);
288 NewTransactionModel.Item item;
289 if (model.getAccountCount() < i + 1) {
290 model.addAccount(acc);
291 notifyItemInserted(i + 1);
293 item = model.getItem(i + 1);
296 .setAccountName(acc.getAccountName());
297 item.setComment(acc.getComment());
298 if (acc.isAmountSet()) {
300 .setAmount(acc.getAmount());
301 if (acc.getAmount() < 0) {
302 if (firstNegative == null) {
303 firstNegative = item;
304 singleNegativeIndex = i;
307 singleNegativeIndex = -1;
310 if (firstPositive == null) {
311 firstPositive = item;
312 singlePositiveIndex = i;
315 singlePositiveIndex = -1;
321 notifyItemChanged(i + 1);
324 if (singleNegativeIndex != -1) {
325 firstNegative.getAccount()
327 model.moveItemLast(singleNegativeIndex);
329 else if (singlePositiveIndex != -1) {
330 firstPositive.getAccount()
332 model.moveItemLast(singlePositiveIndex);
335 checkTransactionSubmittable();
336 model.setFocusedItem(1);
338 public void toggleAllEditing(boolean editable) {
339 // item 0 is the header
340 for (int i = 0; i <= model.getAccountCount(); i++) {
342 .setEditable(editable);
343 notifyItemChanged(i);
344 // TODO perhaps do only one notification about the whole range (notifyDatasetChanged)?
348 int presentItemCount = model.getAccountCount();
350 notifyItemChanged(0); // header changed
351 notifyItemRangeChanged(1, 2); // the two empty rows
352 if (presentItemCount > 2)
353 notifyItemRangeRemoved(3, presentItemCount - 2); // all the rest are gone
355 void updateFocusedItem(int position) {
356 model.updateFocusedItem(position);
358 void noteFocusIsOnAccount(int position) {
359 model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Account);
361 void noteFocusIsOnAmount(int position) {
362 model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Amount);
364 void noteFocusIsOnComment(int position) {
365 model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Comment);
367 private void holdSubmittableChecks() {
370 private void releaseSubmittableChecks() {
371 if (checkHoldCounter == 0)
372 throw new RuntimeException("Asymmetrical call to releaseSubmittableChecks");
375 void setItemCurrency(NewTransactionModel.Item item, Currency newCurrency) {
376 Currency oldCurrency = item.getCurrency();
377 if (!Currency.equal(newCurrency, oldCurrency)) {
378 holdSubmittableChecks();
380 item.setCurrency(newCurrency);
381 // for (Item i : items) {
382 // if (Currency.equal(i.getCurrency(), oldCurrency))
383 // i.setCurrency(newCurrency);
387 releaseSubmittableChecks();
390 checkTransactionSubmittable();
394 A transaction is submittable if:
396 1) has at least two account names
397 2) each row with amount has account name
398 3) for each commodity:
399 3a) amounts must balance to 0, or
400 3b) there must be exactly one empty amount (with account)
401 4) empty accounts with empty amounts are ignored
403 5) a row with an empty account name or empty amount is guaranteed to exist for each
405 6) at least two rows need to be present in the ledger
408 @SuppressLint("DefaultLocale")
409 void checkTransactionSubmittable() {
410 if (checkHoldCounter > 0)
414 final BalanceForCurrency balance = new BalanceForCurrency();
415 final String descriptionText = model.getDescription();
416 boolean submittable = true;
417 final ItemsForCurrency itemsForCurrency = new ItemsForCurrency();
418 final ItemsForCurrency itemsWithEmptyAmountForCurrency = new ItemsForCurrency();
419 final ItemsForCurrency itemsWithAccountAndEmptyAmountForCurrency = new ItemsForCurrency();
420 final ItemsForCurrency itemsWithEmptyAccountForCurrency = new ItemsForCurrency();
421 final ItemsForCurrency itemsWithAmountForCurrency = new ItemsForCurrency();
422 final ItemsForCurrency itemsWithAccountForCurrency = new ItemsForCurrency();
423 final ItemsForCurrency emptyRowsForCurrency = new ItemsForCurrency();
424 final List<NewTransactionModel.Item> emptyRows = new ArrayList<>();
427 if ((descriptionText == null) || descriptionText.trim()
430 Logger.debug("submittable", "Transaction not submittable: missing description");
434 for (int i = 0; i < model.items.size(); i++) {
435 NewTransactionModel.Item item = model.items.get(i);
437 LedgerTransactionAccount acc = item.getAccount();
438 String acc_name = acc.getAccountName()
440 String currName = acc.getCurrency();
442 itemsForCurrency.add(currName, item);
444 if (acc_name.isEmpty()) {
445 itemsWithEmptyAccountForCurrency.add(currName, item);
447 if (acc.isAmountSet()) {
448 // 2) each amount has account name
449 Logger.debug("submittable", String.format(
450 "Transaction not submittable: row %d has no account name, but" +
451 " has" + " amount %1.2f", i + 1, acc.getAmount()));
455 emptyRowsForCurrency.add(currName, item);
460 itemsWithAccountForCurrency.add(currName, item);
463 if (acc.isAmountSet()) {
464 itemsWithAmountForCurrency.add(currName, item);
465 balance.add(currName, acc.getAmount());
468 itemsWithEmptyAmountForCurrency.add(currName, item);
470 if (!acc_name.isEmpty())
471 itemsWithAccountAndEmptyAmountForCurrency.add(currName, item);
475 // 1) has at least two account names
478 Logger.debug("submittable",
479 "Transaction not submittable: no account " + "names");
480 else if (accounts == 1)
481 Logger.debug("submittable",
482 "Transaction not submittable: only one account name");
484 Logger.debug("submittable",
485 String.format("Transaction not submittable: only %d account names",
490 // 3) for each commodity:
491 // 3a) amount must balance to 0, or
492 // 3b) there must be exactly one empty amount (with account)
493 for (String balCurrency : itemsForCurrency.currencies()) {
494 float currencyBalance = balance.get(balCurrency);
495 if (Misc.isZero(currencyBalance)) {
496 // remove hints from all amount inputs in that currency
497 for (NewTransactionModel.Item item : model.items) {
498 if (Currency.equal(item.getCurrency(), balCurrency))
499 item.setAmountHint(null);
503 List<NewTransactionModel.Item> list =
504 itemsWithAccountAndEmptyAmountForCurrency.getList(balCurrency);
505 int balanceReceiversCount = list.size();
506 if (balanceReceiversCount != 1) {
507 if (BuildConfig.DEBUG) {
508 if (balanceReceiversCount == 0)
509 Logger.debug("submittable", String.format(
510 "Transaction not submittable [%s]: non-zero balance " +
511 "with no empty amounts with accounts", balCurrency));
513 Logger.debug("submittable", String.format(
514 "Transaction not submittable [%s]: non-zero balance " +
515 "with multiple empty amounts with accounts", balCurrency));
520 List<NewTransactionModel.Item> emptyAmountList =
521 itemsWithEmptyAmountForCurrency.getList(balCurrency);
523 // suggest off-balance amount to a row and remove hints on other rows
524 NewTransactionModel.Item receiver = null;
526 receiver = list.get(0);
527 else if (!emptyAmountList.isEmpty())
528 receiver = emptyAmountList.get(0);
530 for (NewTransactionModel.Item item : model.items) {
531 if (!Currency.equal(item.getCurrency(), balCurrency))
534 if (item.equals(receiver)) {
535 if (BuildConfig.DEBUG)
536 Logger.debug("submittable",
537 String.format("Setting amount hint to %1.2f [%s]",
538 -currencyBalance, balCurrency));
539 item.setAmountHint(String.format("%1.2f", -currencyBalance));
542 if (BuildConfig.DEBUG)
543 Logger.debug("submittable",
544 String.format("Resetting hint of '%s' [%s]",
545 (item.getAccount() == null) ? "" : item.getAccount()
548 item.setAmountHint(null);
554 // 5) a row with an empty account name or empty amount is guaranteed to exist for
556 for (String balCurrency : balance.currencies()) {
557 int currEmptyRows = itemsWithEmptyAccountForCurrency.size(balCurrency);
558 int currRows = itemsForCurrency.size(balCurrency);
559 int currAccounts = itemsWithAccountForCurrency.size(balCurrency);
560 int currAmounts = itemsWithAmountForCurrency.size(balCurrency);
561 if ((currEmptyRows == 0) &&
562 ((currRows == currAccounts) || (currRows == currAmounts)))
564 // perhaps there already is an unused empty row for another currency that
566 // boolean foundIt = false;
567 // for (Item item : emptyRows) {
568 // Currency itemCurrency = item.getCurrency();
569 // String itemCurrencyName =
570 // (itemCurrency == null) ? "" : itemCurrency.getName();
571 // if (Misc.isZero(balance.get(itemCurrencyName))) {
572 // item.setCurrency(Currency.loadByName(balCurrency));
573 // item.setAmountHint(
574 // String.format("%1.2f", -balance.get(balCurrency)));
585 // drop extra empty rows, not needed
586 for (String currName : emptyRowsForCurrency.currencies()) {
587 List<NewTransactionModel.Item> emptyItems = emptyRowsForCurrency.getList(currName);
588 while ((model.items.size() > 2) && (emptyItems.size() > 1)) {
589 NewTransactionModel.Item item = emptyItems.get(1);
590 emptyItems.remove(1);
591 model.removeRow(item, this);
594 // unused currency, remove last item (which is also an empty one)
595 if ((model.items.size() > 2) && (emptyItems.size() == 1)) {
596 List<NewTransactionModel.Item> currItems = itemsForCurrency.getList(currName);
598 if (currItems.size() == 1) {
599 NewTransactionModel.Item item = emptyItems.get(0);
600 model.removeRow(item, this);
605 // 6) at least two rows need to be present in the ledger
606 while (model.items.size() < 2)
610 debug("submittable", submittable ? "YES" : "NO");
611 model.isSubmittable.setValue(submittable);
613 if (BuildConfig.DEBUG) {
614 debug("submittable", "== Dump of all items");
615 for (int i = 0; i < model.items.size(); i++) {
616 NewTransactionModel.Item item = model.items.get(i);
617 LedgerTransactionAccount acc = item.getAccount();
618 debug("submittable", String.format("Item %2d: [%4.2f(%s) %s] %s ; %s", i,
619 acc.isAmountSet() ? acc.getAmount() : 0,
620 item.isAmountHintSet() ? item.getAmountHint() : "ø", acc.getCurrency(),
621 acc.getAccountName(), acc.getComment()));
625 catch (NumberFormatException e) {
626 debug("submittable", "NO (because of NumberFormatException)");
627 model.isSubmittable.setValue(false);
629 catch (Exception e) {
631 debug("submittable", "NO (because of an Exception)");
632 model.isSubmittable.setValue(false);
636 private static class BalanceForCurrency {
637 private HashMap<String, Float> hashMap = new HashMap<>();
638 float get(String currencyName) {
639 Float f = hashMap.get(currencyName);
642 hashMap.put(currencyName, f);
646 void add(String currencyName, float amount) {
647 hashMap.put(currencyName, get(currencyName) + amount);
649 Set<String> currencies() {
650 return hashMap.keySet();
652 boolean containsCurrency(String currencyName) {
653 return hashMap.containsKey(currencyName);
657 private static class ItemsForCurrency {
658 private HashMap<String, List<NewTransactionModel.Item>> hashMap = new HashMap<>();
660 List<NewTransactionModel.Item> getList(@Nullable String currencyName) {
661 List<NewTransactionModel.Item> list = hashMap.get(currencyName);
663 list = new ArrayList<>();
664 hashMap.put(currencyName, list);
668 void add(@Nullable String currencyName, @NonNull NewTransactionModel.Item item) {
669 getList(currencyName).add(item);
671 int size(@Nullable String currencyName) {
672 return this.getList(currencyName)
675 Set<String> currencies() {
676 return hashMap.keySet();