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.text.TextUtils;
23 import androidx.annotation.NonNull;
24 import androidx.annotation.Nullable;
25 import androidx.lifecycle.LifecycleOwner;
26 import androidx.lifecycle.LiveData;
27 import androidx.lifecycle.MutableLiveData;
28 import androidx.lifecycle.Observer;
29 import androidx.lifecycle.ViewModel;
31 import net.ktnx.mobileledger.BuildConfig;
32 import net.ktnx.mobileledger.db.DB;
33 import net.ktnx.mobileledger.db.TemplateAccount;
34 import net.ktnx.mobileledger.db.TemplateHeader;
35 import net.ktnx.mobileledger.model.Data;
36 import net.ktnx.mobileledger.model.InertMutableLiveData;
37 import net.ktnx.mobileledger.model.LedgerTransaction;
38 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
39 import net.ktnx.mobileledger.model.MatchedTemplate;
40 import net.ktnx.mobileledger.model.MobileLedgerProfile;
41 import net.ktnx.mobileledger.utils.Globals;
42 import net.ktnx.mobileledger.utils.Logger;
43 import net.ktnx.mobileledger.utils.Misc;
44 import net.ktnx.mobileledger.utils.SimpleDate;
46 import org.jetbrains.annotations.NotNull;
48 import java.text.ParseException;
49 import java.util.ArrayList;
50 import java.util.Calendar;
51 import java.util.GregorianCalendar;
52 import java.util.HashMap;
53 import java.util.List;
54 import java.util.Locale;
55 import java.util.Objects;
57 import java.util.concurrent.atomic.AtomicInteger;
58 import java.util.regex.MatchResult;
60 enum ItemType {generalData, transactionRow}
62 enum FocusedElement {Account, Comment, Amount, Description, TransactionComment}
65 public class NewTransactionModel extends ViewModel {
66 private static final int MIN_ITEMS = 3;
67 private final MutableLiveData<Boolean> showCurrency = new MutableLiveData<>(false);
68 private final MutableLiveData<Boolean> isSubmittable = new InertMutableLiveData<>(false);
69 private final MutableLiveData<Boolean> showComments = new MutableLiveData<>(true);
70 private final MutableLiveData<List<Item>> items = new MutableLiveData<>();
71 private final MutableLiveData<Boolean> simulateSave = new InertMutableLiveData<>(false);
72 private final AtomicInteger busyCounter = new AtomicInteger(0);
73 private final MutableLiveData<Boolean> busyFlag = new InertMutableLiveData<>(false);
74 private final Observer<MobileLedgerProfile> profileObserver = profile -> {
75 showCurrency.postValue(profile.getShowCommodityByDefault());
76 showComments.postValue(profile.getShowCommentsByDefault());
78 private final MutableLiveData<FocusInfo> focusInfo = new MutableLiveData<>();
79 private boolean observingDataProfile;
80 public NewTransactionModel() {
83 public LiveData<Boolean> getShowCurrency() {
86 public LiveData<List<Item>> getItems() {
89 private void setItems(@NonNull List<Item> newList) {
90 checkTransactionSubmittable(newList);
91 setItemsWithoutSubmittableChecks(newList);
93 private void setItemsWithoutSubmittableChecks(@NonNull List<Item> list) {
94 Logger.debug("new-trans", "model: Setting new item list");
95 final int cnt = list.size();
96 for (int i = 1; i < cnt - 1; i++) {
97 final TransactionAccount item = list.get(i)
98 .toTransactionAccount();
100 TransactionAccount replacement = new TransactionAccount(item);
101 replacement.isLast = false;
102 list.set(i, replacement);
105 final TransactionAccount last = list.get(cnt - 1)
106 .toTransactionAccount();
108 TransactionAccount replacement = new TransactionAccount(last);
109 replacement.isLast = true;
110 list.set(cnt - 1, replacement);
113 items.setValue(list);
115 private List<Item> copyList() {
116 return copyList(null);
118 private List<Item> copyList(@Nullable List<Item> source) {
119 List<Item> copy = new ArrayList<>();
120 List<Item> oldList = (source == null) ? items.getValue() : source;
123 for (Item item : oldList) {
124 copy.add(Item.from(item));
129 private List<Item> shallowCopyListWithoutItem(int position) {
130 List<Item> copy = new ArrayList<>();
131 List<Item> oldList = items.getValue();
133 if (oldList != null) {
135 for (Item item : oldList) {
144 private List<Item> shallowCopyList() {
145 return new ArrayList<>(items.getValue());
147 LiveData<Boolean> getShowComments() {
150 void observeDataProfile(LifecycleOwner activity) {
151 if (!observingDataProfile)
152 Data.observeProfile(activity, profileObserver);
153 observingDataProfile = true;
155 boolean getSimulateSaveFlag() {
156 Boolean value = simulateSave.getValue();
161 LiveData<Boolean> getSimulateSave() {
164 void toggleSimulateSave() {
165 simulateSave.setValue(!getSimulateSaveFlag());
167 LiveData<Boolean> isSubmittable() {
168 return this.isSubmittable;
171 List<Item> list = new ArrayList<>();
172 list.add(new TransactionHead(""));
173 list.add(new TransactionAccount(""));
174 list.add(new TransactionAccount(""));
175 setItemsWithoutSubmittableChecks(list);
177 boolean accountsInInitialState() {
178 final List<Item> list = items.getValue();
183 for (Item item : list) {
184 if (!(item instanceof TransactionAccount))
187 TransactionAccount accRow = (TransactionAccount) item;
188 if (!accRow.isEmpty())
194 void applyTemplate(MatchedTemplate matchedTemplate, String text) {
195 SimpleDate transactionDate = null;
196 final MatchResult matchResult = matchedTemplate.matchResult;
197 final TemplateHeader templateHead = matchedTemplate.templateHead;
199 int day = extractIntFromMatches(matchResult, templateHead.getDateDayMatchGroup(),
200 templateHead.getDateDay());
201 int month = extractIntFromMatches(matchResult, templateHead.getDateMonthMatchGroup(),
202 templateHead.getDateMonth());
203 int year = extractIntFromMatches(matchResult, templateHead.getDateYearMatchGroup(),
204 templateHead.getDateYear());
206 if (year > 0 || month > 0 || day > 0) {
207 SimpleDate today = SimpleDate.today();
215 transactionDate = new SimpleDate(year, month, day);
217 Logger.debug("pattern", "setting transaction date to " + transactionDate);
221 List<Item> present = copyList();
223 TransactionHead head = new TransactionHead(present.get(0)
224 .toTransactionHead());
225 if (transactionDate != null)
226 head.setDate(transactionDate);
228 final String transactionDescription = extractStringFromMatches(matchResult,
229 templateHead.getTransactionDescriptionMatchGroup(),
230 templateHead.getTransactionDescription());
231 if (Misc.emptyIsNull(transactionDescription) != null)
232 head.setDescription(transactionDescription);
234 final String transactionComment = extractStringFromMatches(matchResult,
235 templateHead.getTransactionCommentMatchGroup(),
236 templateHead.getTransactionComment());
237 if (Misc.emptyIsNull(transactionComment) != null)
238 head.setComment(transactionComment);
240 List<Item> newItems = new ArrayList<>();
244 for (int i = 1; i < present.size(); i++) {
245 final TransactionAccount row = present.get(i)
246 .toTransactionAccount();
248 newItems.add(new TransactionAccount(row));
253 .getTemplateWithAccountsAsync(templateHead.getId(), entry -> {
255 final boolean accountsInInitialState = accountsInInitialState();
256 for (TemplateAccount acc : entry.accounts) {
260 extractStringFromMatches(matchResult, acc.getAccountNameMatchGroup(),
261 acc.getAccountName());
262 String accountComment =
263 extractStringFromMatches(matchResult, acc.getAccountCommentMatchGroup(),
264 acc.getAccountComment());
265 Float amount = extractFloatFromMatches(matchResult, acc.getAmountMatchGroup(),
267 if (amount != null && acc.getNegateAmount() != null && acc.getNegateAmount())
271 TransactionAccount accRow = new TransactionAccount(accountName);
272 accRow.setComment(accountComment);
274 accRow.setAmount(amount);
276 newItems.add(accRow);
279 items.postValue(newItems);
282 private int extractIntFromMatches(MatchResult m, Integer group, Integer literal) {
288 if (grp > 0 & grp <= m.groupCount())
290 return Integer.parseInt(m.group(grp));
292 catch (NumberFormatException e) {
293 Logger.debug("new-trans", "Error extracting matched number", e);
299 private String extractStringFromMatches(MatchResult m, Integer group, String literal) {
305 if (grp > 0 & grp <= m.groupCount())
311 private Float extractFloatFromMatches(MatchResult m, Integer group, Float literal) {
317 if (grp > 0 & grp <= m.groupCount())
319 return Float.valueOf(m.group(grp));
321 catch (NumberFormatException e) {
322 Logger.debug("new-trans", "Error extracting matched number", e);
328 void removeItem(int pos) {
329 List<Item> newList = shallowCopyListWithoutItem(pos);
332 void noteFocusChanged(int position, FocusedElement element) {
333 FocusInfo present = focusInfo.getValue();
334 if (present == null || present.position != position || present.element != element)
335 focusInfo.setValue(new FocusInfo(position, element));
337 public LiveData<FocusInfo> getFocusInfo() {
340 void moveItem(int fromIndex, int toIndex) {
341 List<Item> newList = shallowCopyList();
342 Item item = newList.remove(fromIndex);
343 newList.add(toIndex, item);
344 items.setValue(newList); // same count, same submittable state
346 void moveItemLast(List<Item> list, int index) {
350 3 <-- desired position
353 int itemCount = list.size();
355 if (index < itemCount - 1)
356 list.add(list.remove(index));
358 void toggleCurrencyVisible() {
359 showCurrency.setValue(!Objects.requireNonNull(showCurrency.getValue()));
361 void stopObservingBusyFlag(Observer<Boolean> observer) {
362 busyFlag.removeObserver(observer);
364 void incrementBusyCounter() {
365 int newValue = busyCounter.incrementAndGet();
367 busyFlag.postValue(true);
369 void decrementBusyCounter() {
370 int newValue = busyCounter.decrementAndGet();
372 busyFlag.postValue(false);
374 public LiveData<Boolean> getBusyFlag() {
377 public void toggleShowComments() {
378 showComments.setValue(!Objects.requireNonNull(showComments.getValue()));
380 public LedgerTransaction constructLedgerTransaction() {
381 List<Item> list = Objects.requireNonNull(items.getValue());
382 TransactionHead head = list.get(0)
383 .toTransactionHead();
384 SimpleDate date = head.getDate();
385 LedgerTransaction tr = head.asLedgerTransaction();
387 tr.setComment(head.getComment());
388 LedgerTransactionAccount emptyAmountAccount = null;
389 float emptyAmountAccountBalance = 0;
390 for (int i = 1; i < list.size(); i++) {
391 TransactionAccount item = list.get(i)
392 .toTransactionAccount();
393 LedgerTransactionAccount acc = new LedgerTransactionAccount(item.getAccountName()
396 if (acc.getAccountName()
400 acc.setComment(item.getComment());
402 if (item.isAmountSet()) {
403 acc.setAmount(item.getAmount());
404 emptyAmountAccountBalance += item.getAmount();
407 emptyAmountAccount = acc;
413 if (emptyAmountAccount != null)
414 emptyAmountAccount.setAmount(-emptyAmountAccountBalance);
418 void loadTransactionIntoModel(String profileUUID, int transactionId) {
419 List<Item> newList = new ArrayList<>();
420 LedgerTransaction tr;
421 MobileLedgerProfile profile = Data.getProfile(profileUUID);
423 throw new RuntimeException(String.format(
424 "Unable to find profile %s, which is supposed to contain transaction %d",
425 profileUUID, transactionId));
427 tr = profile.loadTransaction(transactionId);
428 TransactionHead head = new TransactionHead(tr.getDescription());
429 head.setComment(tr.getComment());
433 List<LedgerTransactionAccount> accounts = tr.getAccounts();
435 TransactionAccount firstNegative = null;
436 TransactionAccount firstPositive = null;
437 int singleNegativeIndex = -1;
438 int singlePositiveIndex = -1;
439 int negativeCount = 0;
440 for (int i = 0; i < accounts.size(); i++) {
441 LedgerTransactionAccount acc = accounts.get(i);
442 TransactionAccount item =
443 new TransactionAccount(acc.getAccountName(), acc.getCurrency());
446 item.setAccountName(acc.getAccountName());
447 item.setComment(acc.getComment());
448 if (acc.isAmountSet()) {
449 item.setAmount(acc.getAmount());
450 if (acc.getAmount() < 0) {
451 if (firstNegative == null) {
452 firstNegative = item;
453 singleNegativeIndex = i + 1;
456 singleNegativeIndex = -1;
459 if (firstPositive == null) {
460 firstPositive = item;
461 singlePositiveIndex = i + 1;
464 singlePositiveIndex = -1;
470 if (BuildConfig.DEBUG)
471 dumpItemList("Loaded previous transaction", newList);
473 if (singleNegativeIndex != -1) {
474 firstNegative.resetAmount();
475 moveItemLast(newList, singleNegativeIndex);
477 else if (singlePositiveIndex != -1) {
478 firstPositive.resetAmount();
479 moveItemLast(newList, singlePositiveIndex);
484 noteFocusChanged(1, FocusedElement.Amount);
487 * A transaction is submittable if:
489 * 1) has at least two account names
490 * 2) each row with amount has account name
491 * 3) for each commodity:
492 * 3a) amounts must balance to 0, or
493 * 3b) there must be exactly one empty amount (with account)
494 * 4) empty accounts with empty amounts are ignored
496 * 5) a row with an empty account name or empty amount is guaranteed to exist for each
498 * 6) at least two rows need to be present in the ledger
500 * @param list - the item list to check. Can be the displayed list or a list that will be
503 @SuppressLint("DefaultLocale")
504 void checkTransactionSubmittable(@Nullable List<Item> list) {
505 boolean workingWithLiveList = false;
506 boolean liveListCopied = false;
508 list = Objects.requireNonNull(items.getValue());
509 workingWithLiveList = true;
512 if (BuildConfig.DEBUG)
513 dumpItemList("Before submittable checks", list);
516 final BalanceForCurrency balance = new BalanceForCurrency();
517 final String descriptionText = list.get(0)
520 boolean submittable = true;
521 boolean listChanged = false;
522 final ItemsForCurrency itemsForCurrency = new ItemsForCurrency();
523 final ItemsForCurrency itemsWithEmptyAmountForCurrency = new ItemsForCurrency();
524 final ItemsForCurrency itemsWithAccountAndEmptyAmountForCurrency = new ItemsForCurrency();
525 final ItemsForCurrency itemsWithEmptyAccountForCurrency = new ItemsForCurrency();
526 final ItemsForCurrency itemsWithAmountForCurrency = new ItemsForCurrency();
527 final ItemsForCurrency itemsWithAccountForCurrency = new ItemsForCurrency();
528 final ItemsForCurrency emptyRowsForCurrency = new ItemsForCurrency();
529 final List<Item> emptyRows = new ArrayList<>();
532 if ((descriptionText == null) || descriptionText.trim()
535 Logger.debug("submittable", "Transaction not submittable: missing description");
539 for (int i = 1; i < list.size(); i++) {
540 TransactionAccount item = list.get(i)
541 .toTransactionAccount();
543 String accName = item.getAccountName()
545 String currName = item.getCurrency();
547 itemsForCurrency.add(currName, item);
549 if (accName.isEmpty()) {
550 itemsWithEmptyAccountForCurrency.add(currName, item);
552 if (item.isAmountSet()) {
553 // 2) each amount has account name
554 Logger.debug("submittable", String.format(
555 "Transaction not submittable: row %d has no account name, but" +
556 " has" + " amount %1.2f", i + 1, item.getAmount()));
560 emptyRowsForCurrency.add(currName, item);
565 itemsWithAccountForCurrency.add(currName, item);
568 if (!item.isAmountValid()) {
569 Logger.debug("submittable",
570 String.format("Not submittable: row %d has an invalid amount", i + 1));
573 else if (item.isAmountSet()) {
574 itemsWithAmountForCurrency.add(currName, item);
575 balance.add(currName, item.getAmount());
578 itemsWithEmptyAmountForCurrency.add(currName, item);
580 if (!accName.isEmpty())
581 itemsWithAccountAndEmptyAmountForCurrency.add(currName, item);
585 // 1) has at least two account names
588 Logger.debug("submittable", "Transaction not submittable: no account names");
589 else if (accounts == 1)
590 Logger.debug("submittable",
591 "Transaction not submittable: only one account name");
593 Logger.debug("submittable",
594 String.format("Transaction not submittable: only %d account names",
599 // 3) for each commodity:
600 // 3a) amount must balance to 0, or
601 // 3b) there must be exactly one empty amount (with account)
602 for (String balCurrency : itemsForCurrency.currencies()) {
603 float currencyBalance = balance.get(balCurrency);
604 if (Misc.isZero(currencyBalance)) {
605 // remove hints from all amount inputs in that currency
606 for (int i = 1; i < list.size(); i++) {
607 TransactionAccount acc = list.get(i)
608 .toTransactionAccount();
609 if (Misc.equalStrings(acc.getCurrency(), balCurrency)) {
610 if (BuildConfig.DEBUG)
611 Logger.debug("submittable",
612 String.format("Resetting hint of '%s' [%s]",
613 Misc.nullIsEmpty(acc.getAccountName()),
615 // skip if the amount is set, in which case the hint is not
617 if (!acc.isAmountSet() && acc.amountHintIsSet &&
618 !TextUtils.isEmpty(acc.getAmountHint()))
620 if (workingWithLiveList && !liveListCopied) {
621 list = copyList(list);
622 liveListCopied = true;
624 final TransactionAccount newAcc = new TransactionAccount(acc);
625 newAcc.setAmountHint(null);
626 if (!liveListCopied) {
627 list = copyList(list);
628 liveListCopied = true;
638 itemsWithAccountAndEmptyAmountForCurrency.getList(balCurrency);
639 int balanceReceiversCount = tmpList.size();
640 if (balanceReceiversCount != 1) {
641 if (BuildConfig.DEBUG) {
642 if (balanceReceiversCount == 0)
643 Logger.debug("submittable", String.format(
644 "Transaction not submittable [%s]: non-zero balance " +
645 "with no empty amounts with accounts", balCurrency));
647 Logger.debug("submittable", String.format(
648 "Transaction not submittable [%s]: non-zero balance " +
649 "with multiple empty amounts with accounts", balCurrency));
654 List<Item> emptyAmountList =
655 itemsWithEmptyAmountForCurrency.getList(balCurrency);
657 // suggest off-balance amount to a row and remove hints on other rows
658 Item receiver = null;
659 if (!tmpList.isEmpty())
660 receiver = tmpList.get(0);
661 else if (!emptyAmountList.isEmpty())
662 receiver = emptyAmountList.get(0);
664 for (int i = 0; i < list.size(); i++) {
665 Item item = list.get(i);
666 if (!(item instanceof TransactionAccount))
669 TransactionAccount acc = item.toTransactionAccount();
670 if (!Misc.equalStrings(acc.getCurrency(), balCurrency))
673 if (item == receiver) {
674 final String hint = String.format("%1.2f", -currencyBalance);
675 if (!acc.isAmountHintSet() ||
676 !TextUtils.equals(acc.getAmountHint(), hint))
678 Logger.debug("submittable",
679 String.format("Setting amount hint of {%s} to %s [%s]",
680 acc.toString(), hint, balCurrency));
681 if (workingWithLiveList & !liveListCopied) {
682 list = copyList(list);
683 liveListCopied = true;
685 final TransactionAccount newAcc = new TransactionAccount(acc);
686 newAcc.setAmountHint(hint);
692 if (BuildConfig.DEBUG)
693 Logger.debug("submittable",
694 String.format("Resetting hint of '%s' [%s]",
695 Misc.nullIsEmpty(acc.getAccountName()),
697 if (acc.amountHintIsSet && !TextUtils.isEmpty(acc.getAmountHint())) {
698 if (workingWithLiveList && !liveListCopied) {
699 list = copyList(list);
700 liveListCopied = true;
702 final TransactionAccount newAcc = new TransactionAccount(acc);
703 newAcc.setAmountHint(null);
712 // 5) a row with an empty account name or empty amount is guaranteed to exist for
714 for (String balCurrency : balance.currencies()) {
715 int currEmptyRows = itemsWithEmptyAccountForCurrency.size(balCurrency);
716 int currRows = itemsForCurrency.size(balCurrency);
717 int currAccounts = itemsWithAccountForCurrency.size(balCurrency);
718 int currAmounts = itemsWithAmountForCurrency.size(balCurrency);
719 if ((currEmptyRows == 0) &&
720 ((currRows == currAccounts) || (currRows == currAmounts)))
722 // perhaps there already is an unused empty row for another currency that
724 // boolean foundIt = false;
725 // for (Item item : emptyRows) {
726 // Currency itemCurrency = item.getCurrency();
727 // String itemCurrencyName =
728 // (itemCurrency == null) ? "" : itemCurrency.getName();
729 // if (Misc.isZero(balance.get(itemCurrencyName))) {
730 // item.setCurrency(Currency.loadByName(balCurrency));
731 // item.setAmountHint(
732 // String.format("%1.2f", -balance.get(balCurrency)));
739 if (workingWithLiveList && !liveListCopied) {
740 list = copyList(list);
741 liveListCopied = true;
743 final TransactionAccount newAcc = new TransactionAccount("", balCurrency);
744 final float bal = balance.get(balCurrency);
745 if (!Misc.isZero(bal) && currAmounts == currRows)
746 newAcc.setAmountHint(String.format("%4.2f", -bal));
747 Logger.debug("submittable",
748 String.format("Adding new item with %s for currency %s",
749 newAcc.getAmountHint(), balCurrency));
755 // drop extra empty rows, not needed
756 for (String currName : emptyRowsForCurrency.currencies()) {
757 List<Item> emptyItems = emptyRowsForCurrency.getList(currName);
758 while ((list.size() > MIN_ITEMS) && (emptyItems.size() > 1)) {
759 if (workingWithLiveList && !liveListCopied) {
760 list = copyList(list);
761 liveListCopied = true;
763 // the list is a copy, so the empty item is no longer present
764 Item itemToRemove = emptyItems.remove(1);
765 removeItemById(list, itemToRemove.id);
769 // unused currency, remove last item (which is also an empty one)
770 if ((list.size() > MIN_ITEMS) && (emptyItems.size() == 1)) {
771 List<Item> currItems = itemsForCurrency.getList(currName);
773 if (currItems.size() == 1) {
774 if (workingWithLiveList && !liveListCopied) {
775 list = copyList(list);
776 liveListCopied = true;
778 // the list is a copy, so the empty item is no longer present
779 removeItemById(list, emptyItems.get(0).id);
785 // 6) at least two rows need to be present in the ledger
786 // (the list also contains header and trailer)
787 while (list.size() < MIN_ITEMS) {
788 if (workingWithLiveList && !liveListCopied) {
789 list = copyList(list);
790 liveListCopied = true;
792 list.add(new TransactionAccount(""));
797 Logger.debug("submittable", submittable ? "YES" : "NO");
798 isSubmittable.setValue(submittable);
800 if (BuildConfig.DEBUG)
801 dumpItemList("After submittable checks", list);
803 catch (NumberFormatException e) {
804 Logger.debug("submittable", "NO (because of NumberFormatException)");
805 isSubmittable.setValue(false);
807 catch (Exception e) {
809 Logger.debug("submittable", "NO (because of an Exception)");
810 isSubmittable.setValue(false);
813 if (listChanged && workingWithLiveList) {
814 setItemsWithoutSubmittableChecks(list);
817 private void removeItemById(@NotNull List<Item> list, int id) {
818 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
819 list.removeIf(item -> item.id == id);
822 for (Item item : list) {
830 @SuppressLint("DefaultLocale")
831 private void dumpItemList(@NotNull String msg, @NotNull List<Item> list) {
832 Logger.debug("submittable", "== Dump of all items " + msg);
833 for (int i = 1; i < list.size(); i++) {
834 TransactionAccount item = list.get(i)
835 .toTransactionAccount();
836 Logger.debug("submittable", String.format("%d:%s", i, item.toString()));
839 public void setItemCurrency(int position, String newCurrency) {
840 TransactionAccount item = Objects.requireNonNull(items.getValue())
842 .toTransactionAccount();
843 final String oldCurrency = item.getCurrency();
845 if (Misc.equalStrings(oldCurrency, newCurrency))
848 List<Item> newList = copyList();
849 newList.get(position)
850 .toTransactionAccount()
851 .setCurrency(newCurrency);
855 public boolean accountListIsEmpty() {
856 List<Item> items = Objects.requireNonNull(this.items.getValue());
858 for (Item item : items) {
859 if (!(item instanceof TransactionAccount))
862 if (!((TransactionAccount) item).isEmpty())
869 public static class FocusInfo {
871 FocusedElement element;
872 public FocusInfo(int position, FocusedElement element) {
873 this.position = position;
874 this.element = element;
878 static abstract class Item {
879 private static int idDispenser = 0;
882 synchronized (Item.class) {
886 public static Item from(Item origin) {
887 if (origin instanceof TransactionHead)
888 return new TransactionHead((TransactionHead) origin);
889 if (origin instanceof TransactionAccount)
890 return new TransactionAccount((TransactionAccount) origin);
891 throw new RuntimeException("Don't know how to handle " + origin);
896 public abstract ItemType getType();
897 public TransactionHead toTransactionHead() {
898 if (this instanceof TransactionHead)
899 return (TransactionHead) this;
901 throw new IllegalStateException("Wrong item type " + this);
903 public TransactionAccount toTransactionAccount() {
904 if (this instanceof TransactionAccount)
905 return (TransactionAccount) this;
907 throw new IllegalStateException("Wrong item type " + this);
909 public boolean equalContents(@Nullable Object item) {
913 if (!getClass().equals(item.getClass()))
916 // shortcut - comparing same instance
920 if (this instanceof TransactionHead)
921 return ((TransactionHead) item).equalContents((TransactionHead) this);
922 if (this instanceof TransactionAccount)
923 return ((TransactionAccount) item).equalContents((TransactionAccount) this);
925 throw new RuntimeException("Don't know how to handle " + this);
930 //==========================================================================================
932 public static class TransactionHead extends Item {
933 private SimpleDate date;
934 private String description;
935 private String comment;
936 TransactionHead(String description) {
938 this.description = description;
940 public TransactionHead(TransactionHead origin) {
943 description = origin.description;
944 comment = origin.comment;
946 public SimpleDate getDate() {
949 public void setDate(SimpleDate date) {
952 public void setDate(String text) throws ParseException {
953 if (Misc.emptyIsNull(text) == null) {
958 date = Globals.parseLedgerDate(text);
963 * @return nicely formatted, shortest available date representation
965 String getFormattedDate() {
969 Calendar today = GregorianCalendar.getInstance();
971 if (today.get(Calendar.YEAR) != date.year) {
972 return String.format(Locale.US, "%d/%02d/%02d", date.year, date.month, date.day);
975 if (today.get(Calendar.MONTH) + 1 != date.month) {
976 return String.format(Locale.US, "%d/%02d", date.month, date.day);
979 return String.valueOf(date.day);
983 public String toString() {
984 @SuppressLint("DefaultLocale") StringBuilder b = new StringBuilder(
985 String.format("id:%d/%s", id, Integer.toHexString(hashCode())));
987 if (TextUtils.isEmpty(description))
988 b.append(" «no description»");
990 b.append(String.format(" descr'%s'", description));
993 b.append(String.format("@%s", date.toString()));
995 if (!TextUtils.isEmpty(comment))
996 b.append(String.format(" /%s/", comment));
1000 public String getDescription() {
1003 public void setDescription(String description) {
1004 this.description = description;
1006 public String getComment() {
1009 public void setComment(String comment) {
1010 this.comment = comment;
1013 public ItemType getType() {
1014 return ItemType.generalData;
1016 public LedgerTransaction asLedgerTransaction() {
1017 return new LedgerTransaction(null, date, description, Data.getProfile());
1019 public boolean equalContents(TransactionHead other) {
1023 return Objects.equals(date, other.date) &&
1024 TextUtils.equals(description, other.description) &&
1025 TextUtils.equals(comment, other.comment);
1029 public static class TransactionAccount extends Item {
1030 private String accountName;
1031 private String amountHint;
1032 private String comment;
1033 private String currency;
1034 private float amount;
1035 private boolean amountSet;
1036 private boolean amountValid = true;
1037 private FocusedElement focusedElement = FocusedElement.Account;
1038 private boolean amountHintIsSet = true;
1039 private boolean isLast = false;
1040 public TransactionAccount(TransactionAccount origin) {
1042 accountName = origin.accountName;
1043 amount = origin.amount;
1044 amountSet = origin.amountSet;
1045 amountHint = origin.amountHint;
1046 amountHintIsSet = origin.amountHintIsSet;
1047 comment = origin.comment;
1048 currency = origin.currency;
1049 amountValid = origin.amountValid;
1050 focusedElement = origin.focusedElement;
1051 isLast = origin.isLast;
1053 public TransactionAccount(LedgerTransactionAccount account) {
1055 currency = account.getCurrency();
1056 amount = account.getAmount();
1058 public TransactionAccount(String accountName) {
1060 this.accountName = accountName;
1062 public TransactionAccount(String accountName, String currency) {
1064 this.accountName = accountName;
1065 this.currency = currency;
1067 public boolean isLast() {
1070 public boolean isAmountSet() {
1073 public String getAccountName() {
1076 public void setAccountName(String accountName) {
1077 this.accountName = accountName;
1079 public float getAmount() {
1081 throw new IllegalStateException("Amount is not set");
1084 public void setAmount(float amount) {
1085 this.amount = amount;
1088 public void resetAmount() {
1092 public ItemType getType() {
1093 return ItemType.transactionRow;
1095 public String getAmountHint() {
1098 public void setAmountHint(String amountHint) {
1099 this.amountHint = amountHint;
1100 amountHintIsSet = !TextUtils.isEmpty(amountHint);
1102 public String getComment() {
1105 public void setComment(String comment) {
1106 this.comment = comment;
1108 public String getCurrency() {
1111 public void setCurrency(String currency) {
1112 this.currency = currency;
1114 public boolean isAmountValid() {
1117 public void setAmountValid(boolean amountValid) {
1118 this.amountValid = amountValid;
1120 public FocusedElement getFocusedElement() {
1121 return focusedElement;
1123 public void setFocusedElement(FocusedElement focusedElement) {
1124 this.focusedElement = focusedElement;
1126 public boolean isAmountHintSet() {
1127 return amountHintIsSet;
1129 public void setAmountHintIsSet(boolean amountHintIsSet) {
1130 this.amountHintIsSet = amountHintIsSet;
1132 public boolean isEmpty() {
1133 return !amountSet && Misc.emptyIsNull(accountName) == null &&
1134 Misc.emptyIsNull(comment) == null;
1136 @SuppressLint("DefaultLocale")
1138 public String toString() {
1139 StringBuilder b = new StringBuilder();
1140 b.append(String.format("id:%d/%s", id, Integer.toHexString(hashCode())));
1141 if (!TextUtils.isEmpty(accountName))
1142 b.append(String.format(" acc'%s'", accountName));
1145 b.append(String.format(" %4.2f", amount));
1146 else if (amountHintIsSet)
1147 b.append(String.format(" (%s)", amountHint));
1149 if (!TextUtils.isEmpty(currency))
1153 if (!TextUtils.isEmpty(comment))
1154 b.append(String.format(" /%s/", comment));
1156 return b.toString();
1158 public boolean equalContents(TransactionAccount other) {
1162 boolean equal = TextUtils.equals(accountName, other.accountName);
1163 equal = equal && TextUtils.equals(comment, other.comment) &&
1164 (amountSet ? other.amountSet && amount == other.amount : !other.amountSet);
1166 // compare amount hint only if there is no amount
1168 equal = equal && (amountHintIsSet ? other.amountHintIsSet &&
1169 TextUtils.equals(amountHint, other.amountHint)
1170 : !other.amountHintIsSet);
1171 equal = equal && TextUtils.equals(currency, other.currency) && isLast == other.isLast;
1173 Logger.debug("new-trans",
1174 String.format("Comparing {%s} and {%s}: %s", this.toString(), other.toString(),
1180 private static class BalanceForCurrency {
1181 private final HashMap<String, Float> hashMap = new HashMap<>();
1182 float get(String currencyName) {
1183 Float f = hashMap.get(currencyName);
1186 hashMap.put(currencyName, f);
1190 void add(String currencyName, float amount) {
1191 hashMap.put(currencyName, get(currencyName) + amount);
1193 Set<String> currencies() {
1194 return hashMap.keySet();
1196 boolean containsCurrency(String currencyName) {
1197 return hashMap.containsKey(currencyName);
1201 private static class ItemsForCurrency {
1202 private final HashMap<String, List<Item>> hashMap = new HashMap<>();
1204 List<NewTransactionModel.Item> getList(@Nullable String currencyName) {
1205 List<NewTransactionModel.Item> list = hashMap.get(currencyName);
1207 list = new ArrayList<>();
1208 hashMap.put(currencyName, list);
1212 void add(@Nullable String currencyName, @NonNull NewTransactionModel.Item item) {
1213 getList(currencyName).add(item);
1215 int size(@Nullable String currencyName) {
1216 return this.getList(currencyName)
1219 Set<String> currencies() {
1220 return hashMap.keySet();