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.annotation.SuppressLint;
21 import android.os.Handler;
22 import android.os.Looper;
23 import android.text.TextUtils;
25 import androidx.annotation.NonNull;
26 import androidx.annotation.Nullable;
27 import androidx.lifecycle.LifecycleOwner;
28 import androidx.lifecycle.LiveData;
29 import androidx.lifecycle.MutableLiveData;
30 import androidx.lifecycle.Observer;
31 import androidx.lifecycle.ViewModel;
33 import net.ktnx.mobileledger.BuildConfig;
34 import net.ktnx.mobileledger.db.DB;
35 import net.ktnx.mobileledger.db.TemplateAccount;
36 import net.ktnx.mobileledger.db.TemplateHeader;
37 import net.ktnx.mobileledger.model.Data;
38 import net.ktnx.mobileledger.model.InertMutableLiveData;
39 import net.ktnx.mobileledger.model.LedgerTransaction;
40 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
41 import net.ktnx.mobileledger.model.MatchedTemplate;
42 import net.ktnx.mobileledger.model.MobileLedgerProfile;
43 import net.ktnx.mobileledger.utils.Globals;
44 import net.ktnx.mobileledger.utils.Logger;
45 import net.ktnx.mobileledger.utils.Misc;
46 import net.ktnx.mobileledger.utils.SimpleDate;
48 import org.jetbrains.annotations.NotNull;
50 import java.text.ParseException;
51 import java.util.ArrayList;
52 import java.util.Calendar;
53 import java.util.GregorianCalendar;
54 import java.util.HashMap;
55 import java.util.List;
56 import java.util.Locale;
57 import java.util.Objects;
59 import java.util.concurrent.atomic.AtomicInteger;
60 import java.util.regex.MatchResult;
62 enum ItemType {generalData, transactionRow}
64 enum FocusedElement {Account, Comment, Amount, Description, TransactionComment}
67 public class NewTransactionModel extends ViewModel {
68 private static final int MIN_ITEMS = 3;
69 private final MutableLiveData<Boolean> showCurrency = new MutableLiveData<>(false);
70 private final MutableLiveData<Boolean> isSubmittable = new InertMutableLiveData<>(false);
71 private final MutableLiveData<Boolean> showComments = new MutableLiveData<>(true);
72 private final MutableLiveData<List<Item>> items = new MutableLiveData<>();
73 private final MutableLiveData<Boolean> simulateSave = new InertMutableLiveData<>(false);
74 private final AtomicInteger busyCounter = new AtomicInteger(0);
75 private final MutableLiveData<Boolean> busyFlag = new InertMutableLiveData<>(false);
76 private final Observer<MobileLedgerProfile> profileObserver = profile -> {
77 showCurrency.postValue(profile.getShowCommodityByDefault());
78 showComments.postValue(profile.getShowCommentsByDefault());
80 private final MutableLiveData<FocusInfo> focusInfo = new MutableLiveData<>();
81 private boolean observingDataProfile;
82 public NewTransactionModel() {
85 public LiveData<Boolean> getShowCurrency() {
88 public LiveData<List<Item>> getItems() {
91 private void setItems(@NonNull List<Item> newList) {
92 checkTransactionSubmittable(newList);
93 setItemsWithoutSubmittableChecks(newList);
95 private void replaceItems(@NonNull List<Item> newList) {
101 * make old items replaceable in-place. makes the new values visually blend in
103 private void renumberItems() {
104 final List<Item> list = items.getValue();
110 for (Item item : list)
113 private void setItemsWithoutSubmittableChecks(@NonNull List<Item> list) {
114 final int cnt = list.size();
115 for (int i = 1; i < cnt - 1; i++) {
116 final TransactionAccount item = list.get(i)
117 .toTransactionAccount();
119 TransactionAccount replacement = new TransactionAccount(item);
120 replacement.isLast = false;
121 list.set(i, replacement);
124 final TransactionAccount last = list.get(cnt - 1)
125 .toTransactionAccount();
127 TransactionAccount replacement = new TransactionAccount(last);
128 replacement.isLast = true;
129 list.set(cnt - 1, replacement);
132 if (BuildConfig.DEBUG)
133 dumpItemList("Before setValue()", list);
134 items.setValue(list);
136 private List<Item> copyList() {
137 List<Item> copy = new ArrayList<>();
138 List<Item> oldList = items.getValue();
141 for (Item item : oldList) {
142 copy.add(Item.from(item));
147 private List<Item> copyListWithoutItem(int position) {
148 List<Item> copy = new ArrayList<>();
149 List<Item> oldList = items.getValue();
151 if (oldList != null) {
153 for (Item item : oldList) {
156 copy.add(Item.from(item));
162 private List<Item> shallowCopyList() {
163 return new ArrayList<>(items.getValue());
165 LiveData<Boolean> getShowComments() {
168 void observeDataProfile(LifecycleOwner activity) {
169 if (!observingDataProfile)
170 Data.observeProfile(activity, profileObserver);
171 observingDataProfile = true;
173 boolean getSimulateSaveFlag() {
174 Boolean value = simulateSave.getValue();
179 LiveData<Boolean> getSimulateSave() {
182 void toggleSimulateSave() {
183 simulateSave.setValue(!getSimulateSaveFlag());
185 LiveData<Boolean> isSubmittable() {
186 return this.isSubmittable;
189 Logger.debug("new-trans", "Resetting model");
190 List<Item> list = new ArrayList<>();
191 Item.resetIdDispenser();
192 list.add(new TransactionHead(""));
193 list.add(new TransactionAccount(""));
194 list.add(new TransactionAccount(""));
195 noteFocusChanged(0, FocusedElement.Description);
197 isSubmittable.setValue(false);
198 setItemsWithoutSubmittableChecks(list);
200 boolean accountsInInitialState() {
201 final List<Item> list = items.getValue();
206 for (Item item : list) {
207 if (!(item instanceof TransactionAccount))
210 TransactionAccount accRow = (TransactionAccount) item;
211 if (!accRow.isEmpty())
217 void applyTemplate(MatchedTemplate matchedTemplate, String text) {
218 SimpleDate transactionDate = null;
219 final MatchResult matchResult = matchedTemplate.matchResult;
220 final TemplateHeader templateHead = matchedTemplate.templateHead;
222 int day = extractIntFromMatches(matchResult, templateHead.getDateDayMatchGroup(),
223 templateHead.getDateDay());
224 int month = extractIntFromMatches(matchResult, templateHead.getDateMonthMatchGroup(),
225 templateHead.getDateMonth());
226 int year = extractIntFromMatches(matchResult, templateHead.getDateYearMatchGroup(),
227 templateHead.getDateYear());
229 if (year > 0 || month > 0 || day > 0) {
230 SimpleDate today = SimpleDate.today();
238 transactionDate = new SimpleDate(year, month, day);
240 Logger.debug("pattern", "setting transaction date to " + transactionDate);
244 List<Item> present = copyList();
246 TransactionHead head = new TransactionHead(present.get(0)
247 .toTransactionHead());
248 if (transactionDate != null)
249 head.setDate(transactionDate);
251 final String transactionDescription = extractStringFromMatches(matchResult,
252 templateHead.getTransactionDescriptionMatchGroup(),
253 templateHead.getTransactionDescription());
254 if (Misc.emptyIsNull(transactionDescription) != null)
255 head.setDescription(transactionDescription);
257 final String transactionComment = extractStringFromMatches(matchResult,
258 templateHead.getTransactionCommentMatchGroup(),
259 templateHead.getTransactionComment());
260 if (Misc.emptyIsNull(transactionComment) != null)
261 head.setComment(transactionComment);
263 List<Item> newItems = new ArrayList<>();
267 for (int i = 1; i < present.size(); i++) {
268 final TransactionAccount row = present.get(i)
269 .toTransactionAccount();
271 newItems.add(new TransactionAccount(row));
276 .getTemplateWithAccountsAsync(templateHead.getId(), entry -> {
278 final boolean accountsInInitialState = accountsInInitialState();
279 for (TemplateAccount acc : entry.accounts) {
283 extractStringFromMatches(matchResult, acc.getAccountNameMatchGroup(),
284 acc.getAccountName());
285 String accountComment =
286 extractStringFromMatches(matchResult, acc.getAccountCommentMatchGroup(),
287 acc.getAccountComment());
288 Float amount = extractFloatFromMatches(matchResult, acc.getAmountMatchGroup(),
290 if (amount != null && acc.getNegateAmount() != null && acc.getNegateAmount())
294 TransactionAccount accRow = new TransactionAccount(accountName);
295 accRow.setComment(accountComment);
297 accRow.setAmount(amount);
299 newItems.add(accRow);
302 new Handler(Looper.getMainLooper()).post(() -> setItems(newItems));
305 private int extractIntFromMatches(MatchResult m, Integer group, Integer literal) {
311 if (grp > 0 & grp <= m.groupCount())
313 return Integer.parseInt(m.group(grp));
315 catch (NumberFormatException e) {
316 Logger.debug("new-trans", "Error extracting matched number", e);
322 private String extractStringFromMatches(MatchResult m, Integer group, String literal) {
328 if (grp > 0 & grp <= m.groupCount())
334 private Float extractFloatFromMatches(MatchResult m, Integer group, Float literal) {
340 if (grp > 0 & grp <= m.groupCount())
342 return Float.valueOf(m.group(grp));
344 catch (NumberFormatException e) {
345 Logger.debug("new-trans", "Error extracting matched number", e);
351 void removeItem(int pos) {
352 Logger.debug("new-trans", String.format(Locale.US, "Removing item at position %d", pos));
353 List<Item> newList = copyListWithoutItem(pos);
354 final FocusInfo fi = focusInfo.getValue();
355 if ((fi != null) && (pos < fi.position))
356 noteFocusChanged(fi.position - 1, fi.element);
359 void noteFocusChanged(int position, FocusedElement element) {
360 FocusInfo present = focusInfo.getValue();
361 if (present == null || present.position != position || present.element != element)
362 focusInfo.setValue(new FocusInfo(position, element));
364 public LiveData<FocusInfo> getFocusInfo() {
367 void moveItem(int fromIndex, int toIndex) {
368 List<Item> newList = shallowCopyList();
369 Item item = newList.remove(fromIndex);
370 newList.add(toIndex, item);
372 FocusInfo fi = focusInfo.getValue();
373 if (fi != null && fi.position == fromIndex)
374 noteFocusChanged(toIndex, fi.element);
376 items.setValue(newList); // same count, same submittable state
378 void moveItemLast(List<Item> list, int index) {
382 3 <-- desired position
385 int itemCount = list.size();
387 if (index < itemCount - 1)
388 list.add(list.remove(index));
390 void toggleCurrencyVisible() {
391 final boolean newValue = !Objects.requireNonNull(showCurrency.getValue());
393 // remove currency from all items, or reset currency to the default
394 // no need to clone the list, because the removal of the currency won't lead to
395 // visual changes -- the currency fields will be hidden or reset to default anyway
396 // still, there may be changes in the submittable state
397 final List<Item> list = Objects.requireNonNull(this.items.getValue());
398 for (int i = 1; i < list.size(); i++) {
399 ((TransactionAccount) list.get(i)).setCurrency(newValue ? Data.getProfile()
400 .getDefaultCommodity()
403 checkTransactionSubmittable(null);
404 showCurrency.setValue(newValue);
406 void stopObservingBusyFlag(Observer<Boolean> observer) {
407 busyFlag.removeObserver(observer);
409 void incrementBusyCounter() {
410 int newValue = busyCounter.incrementAndGet();
412 busyFlag.postValue(true);
414 void decrementBusyCounter() {
415 int newValue = busyCounter.decrementAndGet();
417 busyFlag.postValue(false);
419 public LiveData<Boolean> getBusyFlag() {
422 public void toggleShowComments() {
423 showComments.setValue(!Objects.requireNonNull(showComments.getValue()));
425 public LedgerTransaction constructLedgerTransaction() {
426 List<Item> list = Objects.requireNonNull(items.getValue());
427 TransactionHead head = list.get(0)
428 .toTransactionHead();
429 SimpleDate date = head.getDate();
430 LedgerTransaction tr = head.asLedgerTransaction();
432 tr.setComment(head.getComment());
433 LedgerTransactionAccount emptyAmountAccount = null;
434 float emptyAmountAccountBalance = 0;
435 for (int i = 1; i < list.size(); i++) {
436 TransactionAccount item = list.get(i)
437 .toTransactionAccount();
438 LedgerTransactionAccount acc = new LedgerTransactionAccount(item.getAccountName()
441 if (acc.getAccountName()
445 acc.setComment(item.getComment());
447 if (item.isAmountSet()) {
448 acc.setAmount(item.getAmount());
449 emptyAmountAccountBalance += item.getAmount();
452 emptyAmountAccount = acc;
458 if (emptyAmountAccount != null)
459 emptyAmountAccount.setAmount(-emptyAmountAccountBalance);
463 void loadTransactionIntoModel(String profileUUID, int transactionId) {
464 List<Item> newList = new ArrayList<>();
465 Item.resetIdDispenser();
466 LedgerTransaction tr;
467 MobileLedgerProfile profile = Data.getProfile(profileUUID);
469 throw new RuntimeException(String.format(
470 "Unable to find profile %s, which is supposed to contain transaction %d",
471 profileUUID, transactionId));
473 tr = profile.loadTransaction(transactionId);
474 TransactionHead head = new TransactionHead(tr.getDescription());
475 head.setComment(tr.getComment());
479 List<LedgerTransactionAccount> accounts = tr.getAccounts();
481 TransactionAccount firstNegative = null;
482 TransactionAccount firstPositive = null;
483 int singleNegativeIndex = -1;
484 int singlePositiveIndex = -1;
485 int negativeCount = 0;
486 for (int i = 0; i < accounts.size(); i++) {
487 LedgerTransactionAccount acc = accounts.get(i);
488 TransactionAccount item =
489 new TransactionAccount(acc.getAccountName(), acc.getCurrency());
492 item.setAccountName(acc.getAccountName());
493 item.setComment(acc.getComment());
494 if (acc.isAmountSet()) {
495 item.setAmount(acc.getAmount());
496 if (acc.getAmount() < 0) {
497 if (firstNegative == null) {
498 firstNegative = item;
499 singleNegativeIndex = i + 1;
502 singleNegativeIndex = -1;
505 if (firstPositive == null) {
506 firstPositive = item;
507 singlePositiveIndex = i + 1;
510 singlePositiveIndex = -1;
516 if (BuildConfig.DEBUG)
517 dumpItemList("Loaded previous transaction", newList);
519 if (singleNegativeIndex != -1) {
520 firstNegative.resetAmount();
521 moveItemLast(newList, singleNegativeIndex);
523 else if (singlePositiveIndex != -1) {
524 firstPositive.resetAmount();
525 moveItemLast(newList, singlePositiveIndex);
530 noteFocusChanged(1, FocusedElement.Amount);
533 * A transaction is submittable if:
535 * 1) has at least two account names
536 * 2) each row with amount has account name
537 * 3) for each commodity:
538 * 3a) amounts must balance to 0, or
539 * 3b) there must be exactly one empty amount (with account)
540 * 4) empty accounts with empty amounts are ignored
542 * 5) a row with an empty account name or empty amount is guaranteed to exist for each
544 * 6) at least two rows need to be present in the ledger
546 * @param list - the item list to check. Can be the displayed list or a list that will be
549 @SuppressLint("DefaultLocale")
550 void checkTransactionSubmittable(@Nullable List<Item> list) {
551 boolean workingWithLiveList = false;
554 workingWithLiveList = true;
557 if (BuildConfig.DEBUG)
558 dumpItemList(String.format("Before submittable checks (%s)",
559 workingWithLiveList ? "LIVE LIST" : "custom list"), list);
562 final BalanceForCurrency balance = new BalanceForCurrency();
563 final String descriptionText = list.get(0)
566 boolean submittable = true;
567 boolean listChanged = false;
568 final ItemsForCurrency itemsForCurrency = new ItemsForCurrency();
569 final ItemsForCurrency itemsWithEmptyAmountForCurrency = new ItemsForCurrency();
570 final ItemsForCurrency itemsWithAccountAndEmptyAmountForCurrency = new ItemsForCurrency();
571 final ItemsForCurrency itemsWithEmptyAccountForCurrency = new ItemsForCurrency();
572 final ItemsForCurrency itemsWithAmountForCurrency = new ItemsForCurrency();
573 final ItemsForCurrency itemsWithAccountForCurrency = new ItemsForCurrency();
574 final ItemsForCurrency emptyRowsForCurrency = new ItemsForCurrency();
575 final List<Item> emptyRows = new ArrayList<>();
578 if ((descriptionText == null) || descriptionText.trim()
581 Logger.debug("submittable", "Transaction not submittable: missing description");
585 for (int i = 1; i < list.size(); i++) {
586 TransactionAccount item = list.get(i)
587 .toTransactionAccount();
589 String accName = item.getAccountName()
591 String currName = item.getCurrency();
593 itemsForCurrency.add(currName, item);
595 if (accName.isEmpty()) {
596 itemsWithEmptyAccountForCurrency.add(currName, item);
598 if (item.isAmountSet()) {
599 // 2) each amount has account name
600 Logger.debug("submittable", String.format(
601 "Transaction not submittable: row %d has no account name, but" +
602 " has" + " amount %1.2f", i + 1, item.getAmount()));
606 emptyRowsForCurrency.add(currName, item);
611 itemsWithAccountForCurrency.add(currName, item);
614 if (!item.isAmountValid()) {
615 Logger.debug("submittable",
616 String.format("Not submittable: row %d has an invalid amount", i + 1));
619 else if (item.isAmountSet()) {
620 itemsWithAmountForCurrency.add(currName, item);
621 balance.add(currName, item.getAmount());
624 itemsWithEmptyAmountForCurrency.add(currName, item);
626 if (!accName.isEmpty())
627 itemsWithAccountAndEmptyAmountForCurrency.add(currName, item);
631 // 1) has at least two account names
634 Logger.debug("submittable", "Transaction not submittable: no account names");
635 else if (accounts == 1)
636 Logger.debug("submittable",
637 "Transaction not submittable: only one account name");
639 Logger.debug("submittable",
640 String.format("Transaction not submittable: only %d account names",
645 // 3) for each commodity:
646 // 3a) amount must balance to 0, or
647 // 3b) there must be exactly one empty amount (with account)
648 for (String balCurrency : itemsForCurrency.currencies()) {
649 float currencyBalance = balance.get(balCurrency);
650 if (Misc.isZero(currencyBalance)) {
651 // remove hints from all amount inputs in that currency
652 for (int i = 1; i < list.size(); i++) {
653 TransactionAccount acc = list.get(i)
654 .toTransactionAccount();
655 if (Misc.equalStrings(acc.getCurrency(), balCurrency)) {
656 if (BuildConfig.DEBUG)
657 Logger.debug("submittable",
658 String.format(Locale.US, "Resetting hint of %d:'%s' [%s]",
659 i, Misc.nullIsEmpty(acc.getAccountName()),
661 // skip if the amount is set, in which case the hint is not
663 if (!acc.isAmountSet() && acc.amountHintIsSet &&
664 !TextUtils.isEmpty(acc.getAmountHint()))
666 acc.setAmountHint(null);
674 itemsWithAccountAndEmptyAmountForCurrency.getList(balCurrency);
675 int balanceReceiversCount = tmpList.size();
676 if (balanceReceiversCount != 1) {
677 if (BuildConfig.DEBUG) {
678 if (balanceReceiversCount == 0)
679 Logger.debug("submittable", String.format(
680 "Transaction not submittable [%s]: non-zero balance " +
681 "with no empty amounts with accounts", balCurrency));
683 Logger.debug("submittable", String.format(
684 "Transaction not submittable [%s]: non-zero balance " +
685 "with multiple empty amounts with accounts", balCurrency));
690 List<Item> emptyAmountList =
691 itemsWithEmptyAmountForCurrency.getList(balCurrency);
693 // suggest off-balance amount to a row and remove hints on other rows
694 Item receiver = null;
695 if (!tmpList.isEmpty())
696 receiver = tmpList.get(0);
697 else if (!emptyAmountList.isEmpty())
698 receiver = emptyAmountList.get(0);
700 for (int i = 0; i < list.size(); i++) {
701 Item item = list.get(i);
702 if (!(item instanceof TransactionAccount))
705 TransactionAccount acc = item.toTransactionAccount();
706 if (!Misc.equalStrings(acc.getCurrency(), balCurrency))
709 if (item == receiver) {
710 final String hint = String.format("%1.2f", -currencyBalance);
711 if (!acc.isAmountHintSet() ||
712 !Misc.equalStrings(acc.getAmountHint(), hint))
714 Logger.debug("submittable",
715 String.format("Setting amount hint of {%s} to %s [%s]",
716 acc.toString(), hint, balCurrency));
717 acc.setAmountHint(hint);
722 if (BuildConfig.DEBUG)
723 Logger.debug("submittable",
724 String.format("Resetting hint of '%s' [%s]",
725 Misc.nullIsEmpty(acc.getAccountName()),
727 if (acc.amountHintIsSet && !TextUtils.isEmpty(acc.getAmountHint())) {
728 acc.setAmountHint(null);
736 // 5) a row with an empty account name or empty amount is guaranteed to exist for
738 for (String balCurrency : balance.currencies()) {
739 int currEmptyRows = itemsWithEmptyAccountForCurrency.size(balCurrency);
740 int currRows = itemsForCurrency.size(balCurrency);
741 int currAccounts = itemsWithAccountForCurrency.size(balCurrency);
742 int currAmounts = itemsWithAmountForCurrency.size(balCurrency);
743 if ((currEmptyRows == 0) &&
744 ((currRows == currAccounts) || (currRows == currAmounts)))
746 // perhaps there already is an unused empty row for another currency that
748 // boolean foundIt = false;
749 // for (Item item : emptyRows) {
750 // Currency itemCurrency = item.getCurrency();
751 // String itemCurrencyName =
752 // (itemCurrency == null) ? "" : itemCurrency.getName();
753 // if (Misc.isZero(balance.get(itemCurrencyName))) {
754 // item.setCurrency(Currency.loadByName(balCurrency));
755 // item.setAmountHint(
756 // String.format("%1.2f", -balance.get(balCurrency)));
763 final TransactionAccount newAcc = new TransactionAccount("", balCurrency);
764 final float bal = balance.get(balCurrency);
765 if (!Misc.isZero(bal) && currAmounts == currRows)
766 newAcc.setAmountHint(String.format("%4.2f", -bal));
767 Logger.debug("submittable",
768 String.format("Adding new item with %s for currency %s",
769 newAcc.getAmountHint(), balCurrency));
775 // drop extra empty rows, not needed
776 for (String currName : emptyRowsForCurrency.currencies()) {
777 List<Item> emptyItems = emptyRowsForCurrency.getList(currName);
778 while ((list.size() > MIN_ITEMS) && (emptyItems.size() > 1)) {
779 // the list is a copy, so the empty item is no longer present
780 Item itemToRemove = emptyItems.remove(1);
781 removeItemById(list, itemToRemove.id);
785 // unused currency, remove last item (which is also an empty one)
786 if ((list.size() > MIN_ITEMS) && (emptyItems.size() == 1)) {
787 List<Item> currItems = itemsForCurrency.getList(currName);
789 if (currItems.size() == 1) {
790 // the list is a copy, so the empty item is no longer present
791 removeItemById(list, emptyItems.get(0).id);
797 // 6) at least two rows need to be present in the ledger
798 // (the list also contains header and trailer)
799 while (list.size() < MIN_ITEMS) {
800 list.add(new TransactionAccount(""));
804 Logger.debug("submittable", submittable ? "YES" : "NO");
805 isSubmittable.setValue(submittable);
807 if (BuildConfig.DEBUG)
808 dumpItemList("After submittable checks", list);
810 catch (NumberFormatException e) {
811 Logger.debug("submittable", "NO (because of NumberFormatException)");
812 isSubmittable.setValue(false);
814 catch (Exception e) {
816 Logger.debug("submittable", "NO (because of an Exception)");
817 isSubmittable.setValue(false);
820 if (listChanged && workingWithLiveList) {
821 setItemsWithoutSubmittableChecks(list);
824 private void removeItemById(@NotNull List<Item> list, int id) {
825 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
826 list.removeIf(item -> item.id == id);
829 for (Item item : list) {
837 @SuppressLint("DefaultLocale")
838 private void dumpItemList(@NotNull String msg, @NotNull List<Item> list) {
839 Logger.debug("submittable", "== Dump of all items " + msg);
840 for (int i = 1; i < list.size(); i++) {
841 TransactionAccount item = list.get(i)
842 .toTransactionAccount();
843 Logger.debug("submittable", String.format("%d:%s", i, item.toString()));
846 public void setItemCurrency(int position, String newCurrency) {
847 TransactionAccount item = Objects.requireNonNull(items.getValue())
849 .toTransactionAccount();
850 final String oldCurrency = item.getCurrency();
852 if (Misc.equalStrings(oldCurrency, newCurrency))
855 List<Item> newList = copyList();
856 newList.get(position)
857 .toTransactionAccount()
858 .setCurrency(newCurrency);
862 public boolean accountListIsEmpty() {
863 List<Item> items = Objects.requireNonNull(this.items.getValue());
865 for (Item item : items) {
866 if (!(item instanceof TransactionAccount))
869 if (!((TransactionAccount) item).isEmpty())
876 public static class FocusInfo {
878 FocusedElement element;
879 public FocusInfo(int position, FocusedElement element) {
880 this.position = position;
881 this.element = element;
885 static abstract class Item {
886 private static int idDispenser = 0;
889 if (this instanceof TransactionHead)
892 synchronized (Item.class) {
896 public Item(int id) {
899 public static Item from(Item origin) {
900 if (origin instanceof TransactionHead)
901 return new TransactionHead((TransactionHead) origin);
902 if (origin instanceof TransactionAccount)
903 return new TransactionAccount((TransactionAccount) origin);
904 throw new RuntimeException("Don't know how to handle " + origin);
906 private static void resetIdDispenser() {
912 public abstract ItemType getType();
913 public TransactionHead toTransactionHead() {
914 if (this instanceof TransactionHead)
915 return (TransactionHead) this;
917 throw new IllegalStateException("Wrong item type " + this);
919 public TransactionAccount toTransactionAccount() {
920 if (this instanceof TransactionAccount)
921 return (TransactionAccount) this;
923 throw new IllegalStateException("Wrong item type " + this);
925 public boolean equalContents(@Nullable Object item) {
929 if (!getClass().equals(item.getClass()))
932 // shortcut - comparing same instance
936 if (this instanceof TransactionHead)
937 return ((TransactionHead) item).equalContents((TransactionHead) this);
938 if (this instanceof TransactionAccount)
939 return ((TransactionAccount) item).equalContents((TransactionAccount) this);
941 throw new RuntimeException("Don't know how to handle " + this);
946 //==========================================================================================
948 public static class TransactionHead extends Item {
949 private SimpleDate date;
950 private String description;
951 private String comment;
952 TransactionHead(String description) {
954 this.description = description;
956 public TransactionHead(TransactionHead origin) {
959 description = origin.description;
960 comment = origin.comment;
962 public SimpleDate getDate() {
965 public void setDate(SimpleDate date) {
968 public void setDate(String text) throws ParseException {
969 if (Misc.emptyIsNull(text) == null) {
974 date = Globals.parseLedgerDate(text);
979 * @return nicely formatted, shortest available date representation
981 String getFormattedDate() {
985 Calendar today = GregorianCalendar.getInstance();
987 if (today.get(Calendar.YEAR) != date.year) {
988 return String.format(Locale.US, "%d/%02d/%02d", date.year, date.month, date.day);
991 if (today.get(Calendar.MONTH) + 1 != date.month) {
992 return String.format(Locale.US, "%d/%02d", date.month, date.day);
995 return String.valueOf(date.day);
999 public String toString() {
1000 @SuppressLint("DefaultLocale") StringBuilder b = new StringBuilder(
1001 String.format("id:%d/%s", id, Integer.toHexString(hashCode())));
1003 if (TextUtils.isEmpty(description))
1004 b.append(" «no description»");
1006 b.append(String.format(" '%s'", description));
1009 b.append(String.format("@%s", date.toString()));
1011 if (!TextUtils.isEmpty(comment))
1012 b.append(String.format(" /%s/", comment));
1014 return b.toString();
1016 public String getDescription() {
1019 public void setDescription(String description) {
1020 this.description = description;
1022 public String getComment() {
1025 public void setComment(String comment) {
1026 this.comment = comment;
1029 public ItemType getType() {
1030 return ItemType.generalData;
1032 public LedgerTransaction asLedgerTransaction() {
1033 return new LedgerTransaction(null, date, description, Data.getProfile());
1035 public boolean equalContents(TransactionHead other) {
1039 return Objects.equals(date, other.date) &&
1040 Misc.equalStrings(description, other.description) &&
1041 Misc.equalStrings(comment, other.comment);
1045 public static class TransactionAccount extends Item {
1046 private String accountName;
1047 private String amountHint;
1048 private String comment;
1049 private String currency;
1050 private float amount;
1051 private boolean amountSet;
1052 private boolean amountValid = true;
1053 private FocusedElement focusedElement = FocusedElement.Account;
1054 private boolean amountHintIsSet = false;
1055 private boolean isLast = false;
1056 private int accountNameCursorPosition;
1057 public TransactionAccount(TransactionAccount origin) {
1059 accountName = origin.accountName;
1060 amount = origin.amount;
1061 amountSet = origin.amountSet;
1062 amountHint = origin.amountHint;
1063 amountHintIsSet = origin.amountHintIsSet;
1064 comment = origin.comment;
1065 currency = origin.currency;
1066 amountValid = origin.amountValid;
1067 focusedElement = origin.focusedElement;
1068 isLast = origin.isLast;
1069 accountNameCursorPosition = origin.accountNameCursorPosition;
1071 public TransactionAccount(LedgerTransactionAccount account) {
1073 currency = account.getCurrency();
1074 amount = account.getAmount();
1076 public TransactionAccount(String accountName) {
1078 this.accountName = accountName;
1080 public TransactionAccount(String accountName, String currency) {
1082 this.accountName = accountName;
1083 this.currency = currency;
1085 public boolean isLast() {
1088 public boolean isAmountSet() {
1091 public String getAccountName() {
1094 public void setAccountName(String accountName) {
1095 this.accountName = accountName;
1097 public float getAmount() {
1099 throw new IllegalStateException("Amount is not set");
1102 public void setAmount(float amount) {
1103 this.amount = amount;
1106 public void resetAmount() {
1110 public ItemType getType() {
1111 return ItemType.transactionRow;
1113 public String getAmountHint() {
1116 public void setAmountHint(String amountHint) {
1117 this.amountHint = amountHint;
1118 amountHintIsSet = !TextUtils.isEmpty(amountHint);
1120 public String getComment() {
1123 public void setComment(String comment) {
1124 this.comment = comment;
1126 public String getCurrency() {
1129 public void setCurrency(String currency) {
1130 this.currency = currency;
1132 public boolean isAmountValid() {
1135 public void setAmountValid(boolean amountValid) {
1136 this.amountValid = amountValid;
1138 public FocusedElement getFocusedElement() {
1139 return focusedElement;
1141 public void setFocusedElement(FocusedElement focusedElement) {
1142 this.focusedElement = focusedElement;
1144 public boolean isAmountHintSet() {
1145 return amountHintIsSet;
1147 public void setAmountHintIsSet(boolean amountHintIsSet) {
1148 this.amountHintIsSet = amountHintIsSet;
1150 public boolean isEmpty() {
1151 return !amountSet && Misc.emptyIsNull(accountName) == null &&
1152 Misc.emptyIsNull(comment) == null;
1154 @SuppressLint("DefaultLocale")
1156 public String toString() {
1157 StringBuilder b = new StringBuilder();
1158 b.append(String.format("id:%d/%s", id, Integer.toHexString(hashCode())));
1159 if (!TextUtils.isEmpty(accountName))
1160 b.append(String.format(" acc'%s'", accountName));
1163 b.append(String.format(" %4.2f", amount));
1164 else if (amountHintIsSet)
1165 b.append(String.format(" (%s)", amountHint));
1167 if (!TextUtils.isEmpty(currency))
1171 if (!TextUtils.isEmpty(comment))
1172 b.append(String.format(" /%s/", comment));
1177 return b.toString();
1179 public boolean equalContents(TransactionAccount other) {
1183 boolean equal = Misc.equalStrings(accountName, other.accountName);
1184 equal = equal && Misc.equalStrings(comment, other.comment) &&
1185 (amountSet ? other.amountSet && amount == other.amount : !other.amountSet);
1187 // compare amount hint only if there is no amount
1189 equal = equal && (amountHintIsSet ? other.amountHintIsSet &&
1190 Misc.equalStrings(amountHint, other.amountHint)
1191 : !other.amountHintIsSet);
1192 equal = equal && Misc.equalStrings(currency, other.currency) && isLast == other.isLast;
1194 Logger.debug("new-trans",
1195 String.format("Comparing {%s} and {%s}: %s", this.toString(), other.toString(),
1199 public int getAccountNameCursorPosition() {
1200 return accountNameCursorPosition;
1202 public void setAccountNameCursorPosition(int position) {
1203 this.accountNameCursorPosition = position;
1207 private static class BalanceForCurrency {
1208 private final HashMap<String, Float> hashMap = new HashMap<>();
1209 float get(String currencyName) {
1210 Float f = hashMap.get(currencyName);
1213 hashMap.put(currencyName, f);
1217 void add(String currencyName, float amount) {
1218 hashMap.put(currencyName, get(currencyName) + amount);
1220 Set<String> currencies() {
1221 return hashMap.keySet();
1223 boolean containsCurrency(String currencyName) {
1224 return hashMap.containsKey(currencyName);
1228 private static class ItemsForCurrency {
1229 private final HashMap<String, List<Item>> hashMap = new HashMap<>();
1231 List<NewTransactionModel.Item> getList(@Nullable String currencyName) {
1232 List<NewTransactionModel.Item> list = hashMap.get(currencyName);
1234 list = new ArrayList<>();
1235 hashMap.put(currencyName, list);
1239 void add(@Nullable String currencyName, @NonNull NewTransactionModel.Item item) {
1240 getList(currencyName).add(item);
1242 int size(@Nullable String currencyName) {
1243 return this.getList(currencyName)
1246 Set<String> currencies() {
1247 return hashMap.keySet();