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.BuildConfig;
33 import net.ktnx.mobileledger.R;
34 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
35 import net.ktnx.mobileledger.model.Currency;
36 import net.ktnx.mobileledger.model.Data;
37 import net.ktnx.mobileledger.model.LedgerTransaction;
38 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
39 import net.ktnx.mobileledger.model.MobileLedgerProfile;
40 import net.ktnx.mobileledger.utils.Logger;
41 import net.ktnx.mobileledger.utils.MLDB;
42 import net.ktnx.mobileledger.utils.Misc;
44 import java.util.ArrayList;
45 import java.util.HashMap;
46 import java.util.List;
47 import java.util.Locale;
50 import static net.ktnx.mobileledger.utils.Logger.debug;
52 class NewTransactionItemsAdapter extends RecyclerView.Adapter<NewTransactionItemHolder>
53 implements DescriptionSelectedCallback {
54 private NewTransactionModel model;
55 private MobileLedgerProfile mProfile;
56 private ItemTouchHelper touchHelper;
57 private RecyclerView recyclerView;
58 private int checkHoldCounter = 0;
59 NewTransactionItemsAdapter(NewTransactionModel viewModel, MobileLedgerProfile profile) {
63 int size = model.getAccountCount();
65 Logger.debug("new-transaction",
66 String.format(Locale.US, "%d accounts is too little, Calling addRow()", size));
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 and last items are immovable
84 if (adapterPosition == 0)
86 if (adapterPosition == adapter.getItemCount() - 1)
89 return super.canDropOver(recyclerView, current, target);
92 public int getMovementFlags(@NonNull RecyclerView recyclerView,
93 @NonNull RecyclerView.ViewHolder viewHolder) {
94 int flags = makeFlag(ItemTouchHelper.ACTION_STATE_IDLE, ItemTouchHelper.END);
95 // the top (date and description) and the bottom (padding) items are always there
96 final int adapterPosition = viewHolder.getAdapterPosition();
97 if ((adapterPosition > 0) && (adapterPosition < adapter.getItemCount() - 1)) {
98 flags |= makeFlag(ItemTouchHelper.ACTION_STATE_DRAG,
99 ItemTouchHelper.UP | ItemTouchHelper.DOWN) |
100 makeFlag(ItemTouchHelper.ACTION_STATE_SWIPE,
101 ItemTouchHelper.START | ItemTouchHelper.END);
107 public boolean onMove(@NonNull RecyclerView recyclerView,
108 @NonNull RecyclerView.ViewHolder viewHolder,
109 @NonNull RecyclerView.ViewHolder target) {
111 model.swapItems(viewHolder.getAdapterPosition(), target.getAdapterPosition());
112 notifyItemMoved(viewHolder.getAdapterPosition(), target.getAdapterPosition());
116 public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
117 int pos = viewHolder.getAdapterPosition();
118 viewModel.removeItem(pos - 1);
119 notifyItemRemoved(pos);
120 viewModel.sendCountNotifications(); // needed after items re-arrangement
121 checkTransactionSubmittable();
125 public void setProfile(MobileLedgerProfile profile) {
128 private int addRow() {
131 private int addRow(String commodity) {
132 final int newAccountCount = model.addAccount(new LedgerTransactionAccount("", commodity));
133 Logger.debug("new-transaction",
134 String.format(Locale.US, "invoking notifyItemInserted(%d)", newAccountCount));
135 // the header is at position 0
136 notifyItemInserted(newAccountCount);
137 model.sendCountNotifications(); // needed after holders' positions have changed
138 return newAccountCount;
142 public NewTransactionItemHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
143 LinearLayout row = (LinearLayout) LayoutInflater.from(parent.getContext())
144 .inflate(R.layout.new_transaction_row,
147 return new NewTransactionItemHolder(row, this);
150 public void onBindViewHolder(@NonNull NewTransactionItemHolder holder, int position) {
151 Logger.debug("bind", String.format(Locale.US, "Binding item at position %d", position));
152 NewTransactionModel.Item item = model.getItem(position);
153 holder.setData(item);
154 Logger.debug("bind", String.format(Locale.US, "Bound %s item at position %d", item.getType()
159 public int getItemCount() {
160 return model.getAccountCount() + 2;
162 private boolean accountListIsEmpty() {
163 for (int i = 0; i < model.getAccountCount(); i++) {
164 LedgerTransactionAccount acc = model.getAccount(i);
165 if (!acc.getAccountName()
168 if (acc.isAmountSet())
175 public void onAttachedToRecyclerView(@NonNull RecyclerView recyclerView) {
176 super.onAttachedToRecyclerView(recyclerView);
177 this.recyclerView = recyclerView;
178 touchHelper.attachToRecyclerView(recyclerView);
181 public void onDetachedFromRecyclerView(@NonNull RecyclerView recyclerView) {
182 touchHelper.attachToRecyclerView(null);
183 super.onDetachedFromRecyclerView(recyclerView);
184 this.recyclerView = null;
186 public void descriptionSelected(String description) {
187 debug("descr selected", description);
188 if (!accountListIsEmpty())
191 String accFilter = mProfile.getPreferredAccountsFilter();
193 ArrayList<String> params = new ArrayList<>();
194 StringBuilder sb = new StringBuilder("select t.profile, t.id from transactions t");
196 if (!Misc.isEmptyOrNull(accFilter)) {
197 sb.append(" JOIN transaction_accounts ta")
198 .append(" ON ta.profile = t.profile")
199 .append(" AND ta.transaction_id = t.id");
202 sb.append(" WHERE t.description=?");
203 params.add(description);
205 if (!Misc.isEmptyOrNull(accFilter)) {
206 sb.append(" AND ta.account_name LIKE '%'||?||'%'");
207 params.add(accFilter);
210 sb.append(" ORDER BY t.date DESC LIMIT 1");
212 final String sql = sb.toString();
214 debug("descr", params.toString());
216 Activity activity = (Activity) recyclerView.getContext();
217 // FIXME: handle exceptions?
218 MLDB.queryInBackground(sql, params.toArray(new String[]{}), new MLDB.CallbackHelper() {
220 public void onStart() {
221 model.incrementBusyCounter();
224 public void onDone() {
225 model.decrementBusyCounter();
228 public boolean onRow(@NonNull Cursor cursor) {
229 final String profileUUID = cursor.getString(0);
230 final int transactionId = cursor.getInt(1);
231 activity.runOnUiThread(() -> loadTransactionIntoModel(profileUUID, transactionId));
232 return false; // limit 1, by the way
235 public void onNoRows() {
236 if (Misc.isEmptyOrNull(accFilter))
239 debug("descr", "Trying transaction search without preferred account filter");
241 final String broaderSql =
242 "select t.profile, t.id from transactions t where t.description=?" +
243 " ORDER BY date desc LIMIT 1";
245 debug("descr", broaderSql);
246 debug("descr", description);
248 MLDB.queryInBackground(broaderSql, new String[]{description},
249 new MLDB.CallbackHelper() {
251 public void onStart() {
252 model.incrementBusyCounter();
255 public boolean onRow(@NonNull Cursor cursor) {
256 final String profileUUID = cursor.getString(0);
257 final int transactionId = cursor.getInt(1);
258 activity.runOnUiThread(
259 () -> loadTransactionIntoModel(profileUUID, transactionId));
263 public void onDone() {
264 model.decrementBusyCounter();
270 private void loadTransactionIntoModel(String profileUUID, int transactionId) {
271 LedgerTransaction tr;
272 MobileLedgerProfile profile = Data.getProfile(profileUUID);
274 throw new RuntimeException(String.format(
275 "Unable to find profile %s, which is supposed to contain transaction %d",
276 profileUUID, transactionId));
278 tr = profile.loadTransaction(transactionId);
279 ArrayList<LedgerTransactionAccount> accounts = tr.getAccounts();
280 NewTransactionModel.Item firstNegative = null;
281 NewTransactionModel.Item firstPositive = null;
282 int singleNegativeIndex = -1;
283 int singlePositiveIndex = -1;
284 int negativeCount = 0;
285 for (int i = 0; i < accounts.size(); i++) {
286 LedgerTransactionAccount acc = accounts.get(i);
287 NewTransactionModel.Item item;
288 if (model.getAccountCount() < i + 1) {
289 model.addAccount(acc);
290 notifyItemInserted(i + 1);
292 item = model.getItem(i + 1);
295 .setAccountName(acc.getAccountName());
296 item.setComment(acc.getComment());
297 if (acc.isAmountSet()) {
299 .setAmount(acc.getAmount());
300 if (acc.getAmount() < 0) {
301 if (firstNegative == null) {
302 firstNegative = item;
303 singleNegativeIndex = i;
306 singleNegativeIndex = -1;
309 if (firstPositive == null) {
310 firstPositive = item;
311 singlePositiveIndex = i;
314 singlePositiveIndex = -1;
320 notifyItemChanged(i + 1);
323 if (singleNegativeIndex != -1) {
324 firstNegative.getAccount()
326 model.moveItemLast(singleNegativeIndex);
328 else if (singlePositiveIndex != -1) {
329 firstPositive.getAccount()
331 model.moveItemLast(singlePositiveIndex);
334 checkTransactionSubmittable();
335 model.setFocusedItem(1);
337 public void toggleAllEditing(boolean editable) {
338 // item 0 is the header
339 for (int i = 0; i <= model.getAccountCount(); i++) {
341 .setEditable(editable);
342 notifyItemChanged(i);
343 // TODO perhaps do only one notification about the whole range (notifyDatasetChanged)?
347 int presentItemCount = model.getAccountCount();
349 notifyItemChanged(0); // header changed
350 notifyItemRangeChanged(1, 2); // the two empty rows
351 if (presentItemCount > 2)
352 notifyItemRangeRemoved(3, presentItemCount - 2); // all the rest are gone
354 void updateFocusedItem(int position) {
355 model.updateFocusedItem(position);
357 void noteFocusIsOnAccount(int position) {
358 model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Account);
360 void noteFocusIsOnAmount(int position) {
361 model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Amount);
363 void noteFocusIsOnComment(int position) {
364 model.noteFocusChanged(position, NewTransactionModel.FocusedElement.Comment);
366 private void holdSubmittableChecks() {
369 private void releaseSubmittableChecks() {
370 if (checkHoldCounter == 0)
371 throw new RuntimeException("Asymmetrical call to releaseSubmittableChecks");
374 void setItemCurrency(NewTransactionModel.Item item, Currency newCurrency) {
375 Currency oldCurrency = item.getCurrency();
376 if (!Currency.equal(newCurrency, oldCurrency)) {
377 holdSubmittableChecks();
379 item.setCurrency(newCurrency);
380 // for (Item i : items) {
381 // if (Currency.equal(i.getCurrency(), oldCurrency))
382 // i.setCurrency(newCurrency);
386 releaseSubmittableChecks();
389 checkTransactionSubmittable();
393 A transaction is submittable if:
395 1) has at least two account names
396 2) each row with amount has account name
397 3) for each commodity:
398 3a) amounts must balance to 0, or
399 3b) there must be exactly one empty amount (with account)
400 4) empty accounts with empty amounts are ignored
402 5) a row with an empty account name or empty amount is guaranteed to exist for each
404 6) at least two rows need to be present in the ledger
407 @SuppressLint("DefaultLocale")
408 void checkTransactionSubmittable() {
409 if (checkHoldCounter > 0)
413 final BalanceForCurrency balance = new BalanceForCurrency();
414 final String descriptionText = model.getDescription();
415 boolean submittable = true;
416 final ItemsForCurrency itemsForCurrency = new ItemsForCurrency();
417 final ItemsForCurrency itemsWithEmptyAmountForCurrency = new ItemsForCurrency();
418 final ItemsForCurrency itemsWithAccountAndEmptyAmountForCurrency = new ItemsForCurrency();
419 final ItemsForCurrency itemsWithEmptyAccountForCurrency = new ItemsForCurrency();
420 final ItemsForCurrency itemsWithAmountForCurrency = new ItemsForCurrency();
421 final ItemsForCurrency itemsWithAccountForCurrency = new ItemsForCurrency();
422 final ItemsForCurrency emptyRowsForCurrency = new ItemsForCurrency();
423 final List<NewTransactionModel.Item> emptyRows = new ArrayList<>();
426 if ((descriptionText == null) || descriptionText.trim()
429 Logger.debug("submittable", "Transaction not submittable: missing description");
433 for (int i = 0; i < model.items.size(); i++) {
434 NewTransactionModel.Item item = model.items.get(i);
436 LedgerTransactionAccount acc = item.getAccount();
437 String acc_name = acc.getAccountName()
439 String currName = acc.getCurrency();
441 itemsForCurrency.add(currName, item);
443 if (acc_name.isEmpty()) {
444 itemsWithEmptyAccountForCurrency.add(currName, item);
446 if (acc.isAmountSet()) {
447 // 2) each amount has account name
448 Logger.debug("submittable", String.format(
449 "Transaction not submittable: row %d has no account name, but" +
450 " has" + " amount %1.2f", i + 1, acc.getAmount()));
454 emptyRowsForCurrency.add(currName, item);
459 itemsWithAccountForCurrency.add(currName, item);
462 if (acc.isAmountSet()) {
463 itemsWithAmountForCurrency.add(currName, item);
464 balance.add(currName, acc.getAmount());
467 itemsWithEmptyAmountForCurrency.add(currName, item);
469 if (!acc_name.isEmpty())
470 itemsWithAccountAndEmptyAmountForCurrency.add(currName, item);
474 // 1) has at least two account names
477 Logger.debug("submittable",
478 "Transaction not submittable: no account " + "names");
479 else if (accounts == 1)
480 Logger.debug("submittable",
481 "Transaction not submittable: only one account name");
483 Logger.debug("submittable",
484 String.format("Transaction not submittable: only %d account names",
489 // 3) for each commodity:
490 // 3a) amount must balance to 0, or
491 // 3b) there must be exactly one empty amount (with account)
492 for (String balCurrency : itemsForCurrency.currencies()) {
493 float currencyBalance = balance.get(balCurrency);
494 if (Misc.isZero(currencyBalance)) {
495 // remove hints from all amount inputs in that currency
496 for (NewTransactionModel.Item item : model.items) {
497 if (Currency.equal(item.getCurrency(), balCurrency))
498 item.setAmountHint(null);
502 List<NewTransactionModel.Item> list =
503 itemsWithAccountAndEmptyAmountForCurrency.getList(balCurrency);
504 int balanceReceiversCount = list.size();
505 if (balanceReceiversCount != 1) {
506 if (BuildConfig.DEBUG) {
507 if (balanceReceiversCount == 0)
508 Logger.debug("submittable", String.format(
509 "Transaction not submittable [%s]: non-zero balance " +
510 "with no empty amounts with accounts", balCurrency));
512 Logger.debug("submittable", String.format(
513 "Transaction not submittable [%s]: non-zero balance " +
514 "with multiple empty amounts with accounts", balCurrency));
519 List<NewTransactionModel.Item> emptyAmountList =
520 itemsWithEmptyAmountForCurrency.getList(balCurrency);
522 // suggest off-balance amount to a row and remove hints on other rows
523 NewTransactionModel.Item receiver = null;
525 receiver = list.get(0);
526 else if (!emptyAmountList.isEmpty())
527 receiver = emptyAmountList.get(0);
529 for (NewTransactionModel.Item item : model.items) {
530 if (!Currency.equal(item.getCurrency(), balCurrency))
533 if (item.equals(receiver)) {
534 if (BuildConfig.DEBUG)
535 Logger.debug("submittable",
536 String.format("Setting amount hint to %1.2f [%s]",
537 -currencyBalance, balCurrency));
538 item.setAmountHint(String.format("%1.2f", -currencyBalance));
541 if (BuildConfig.DEBUG)
542 Logger.debug("submittable",
543 String.format("Resetting hint of '%s' [%s]",
544 (item.getAccount() == null) ? "" : item.getAccount()
547 item.setAmountHint(null);
553 // 5) a row with an empty account name or empty amount is guaranteed to exist for
555 for (String balCurrency : balance.currencies()) {
556 int currEmptyRows = itemsWithEmptyAccountForCurrency.size(balCurrency);
557 int currRows = itemsForCurrency.size(balCurrency);
558 int currAccounts = itemsWithAccountForCurrency.size(balCurrency);
559 int currAmounts = itemsWithAmountForCurrency.size(balCurrency);
560 if ((currEmptyRows == 0) &&
561 ((currRows == currAccounts) || (currRows == currAmounts)))
563 // perhaps there already is an unused empty row for another currency that
565 // boolean foundIt = false;
566 // for (Item item : emptyRows) {
567 // Currency itemCurrency = item.getCurrency();
568 // String itemCurrencyName =
569 // (itemCurrency == null) ? "" : itemCurrency.getName();
570 // if (Misc.isZero(balance.get(itemCurrencyName))) {
571 // item.setCurrency(Currency.loadByName(balCurrency));
572 // item.setAmountHint(
573 // String.format("%1.2f", -balance.get(balCurrency)));
584 // drop extra empty rows, not needed
585 for (String currName : emptyRowsForCurrency.currencies()) {
586 List<NewTransactionModel.Item> emptyItems = emptyRowsForCurrency.getList(currName);
587 while ((model.items.size() > 2) && (emptyItems.size() > 1)) {
588 NewTransactionModel.Item item = emptyItems.get(1);
589 emptyItems.remove(1);
590 model.removeRow(item, this);
593 // unused currency, remove last item (which is also an empty one)
594 if ((model.items.size() > 2) && (emptyItems.size() == 1)) {
595 List<NewTransactionModel.Item> currItems = itemsForCurrency.getList(currName);
597 if (currItems.size() == 1) {
598 NewTransactionModel.Item item = emptyItems.get(0);
599 model.removeRow(item, this);
604 // 6) at least two rows need to be present in the ledger
605 while (model.items.size() < 2)
609 debug("submittable", submittable ? "YES" : "NO");
610 model.isSubmittable.setValue(submittable);
612 if (BuildConfig.DEBUG) {
613 debug("submittable", "== Dump of all items");
614 for (int i = 0; i < model.items.size(); i++) {
615 NewTransactionModel.Item item = model.items.get(i);
616 LedgerTransactionAccount acc = item.getAccount();
617 debug("submittable", String.format("Item %2d: [%4.2f(%s) %s] %s ; %s", i,
618 acc.isAmountSet() ? acc.getAmount() : 0,
619 item.isAmountHintSet() ? item.getAmountHint() : "ø", acc.getCurrency(),
620 acc.getAccountName(), acc.getComment()));
624 catch (NumberFormatException e) {
625 debug("submittable", "NO (because of NumberFormatException)");
626 model.isSubmittable.setValue(false);
628 catch (Exception e) {
630 debug("submittable", "NO (because of an Exception)");
631 model.isSubmittable.setValue(false);
635 private static class BalanceForCurrency {
636 private HashMap<String, Float> hashMap = new HashMap<>();
637 float get(String currencyName) {
638 Float f = hashMap.get(currencyName);
641 hashMap.put(currencyName, f);
645 void add(String currencyName, float amount) {
646 hashMap.put(currencyName, get(currencyName) + amount);
648 Set<String> currencies() {
649 return hashMap.keySet();
651 boolean containsCurrency(String currencyName) {
652 return hashMap.containsKey(currencyName);
656 private static class ItemsForCurrency {
657 private HashMap<String, List<NewTransactionModel.Item>> hashMap = new HashMap<>();
659 List<NewTransactionModel.Item> getList(@Nullable String currencyName) {
660 List<NewTransactionModel.Item> list = hashMap.get(currencyName);
662 list = new ArrayList<>();
663 hashMap.put(currencyName, list);
667 void add(@Nullable String currencyName, @NonNull NewTransactionModel.Item item) {
668 getList(currencyName).add(item);
670 int size(@Nullable String currencyName) {
671 return this.getList(currencyName)
674 Set<String> currencies() {
675 return hashMap.keySet();