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;
22 import androidx.annotation.NonNull;
23 import androidx.annotation.Nullable;
24 import androidx.lifecycle.LiveData;
25 import androidx.lifecycle.MutableLiveData;
26 import androidx.lifecycle.Observer;
27 import androidx.lifecycle.ViewModel;
29 import net.ktnx.mobileledger.BuildConfig;
30 import net.ktnx.mobileledger.model.Currency;
31 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
32 import net.ktnx.mobileledger.utils.Logger;
33 import net.ktnx.mobileledger.utils.Misc;
35 import org.jetbrains.annotations.NotNull;
37 import java.util.ArrayList;
38 import java.util.Calendar;
39 import java.util.Collections;
40 import java.util.Date;
41 import java.util.GregorianCalendar;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Locale;
46 import java.util.regex.Matcher;
47 import java.util.regex.Pattern;
49 import static net.ktnx.mobileledger.utils.Logger.debug;
51 public class NewTransactionModel extends ViewModel {
52 private static final Pattern reYMD =
53 Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
54 private static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
55 private static final Pattern reD = Pattern.compile("\\s*(\\d+)\\s*$");
56 final MutableLiveData<Boolean> showCurrency = new MutableLiveData<>(false);
57 private final Item header = new Item(this, null, "");
58 private final Item trailer = new Item(this);
59 private final ArrayList<Item> items = new ArrayList<>();
60 private final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
61 private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(0);
62 private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
63 private final MutableLiveData<Boolean> simulateSave = new MutableLiveData<>(false);
65 Slots contain lists of items, all using the same currency, including the possible
66 item with no account/amount that is used to help balancing the transaction
68 There is one slot per currency
70 private final HashMap<String, List<Item>> slots = new HashMap<>();
71 private int checkHoldCounter = 0;
72 void holdSubmittableChecks() {
75 void releaseSubmittableChecks() {
76 if (checkHoldCounter == 0)
77 throw new RuntimeException("Asymmetrical call to releaseSubmittableChecks");
80 boolean getSimulateSave() {
81 return simulateSave.getValue();
83 public void setSimulateSave(boolean simulateSave) {
84 this.simulateSave.setValue(simulateSave);
86 void toggleSimulateSave() {
87 simulateSave.setValue(!simulateSave.getValue());
89 void observeSimulateSave(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
90 @NonNull androidx.lifecycle.Observer<? super Boolean> observer) {
91 this.simulateSave.observe(owner, observer);
93 int getAccountCount() {
96 public Date getDate() {
97 return header.date.getValue();
99 public String getDescription() {
100 return header.description.getValue();
102 LiveData<Boolean> isSubmittable() {
103 return this.isSubmittable;
106 header.date.setValue(null);
107 header.description.setValue(null);
109 items.add(new Item(this, new LedgerTransactionAccount("")));
110 items.add(new Item(this, new LedgerTransactionAccount("")));
111 focusedItem.setValue(0);
113 void observeFocusedItem(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
114 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
115 this.focusedItem.observe(owner, observer);
117 void stopObservingFocusedItem(@NonNull androidx.lifecycle.Observer<? super Integer> observer) {
118 this.focusedItem.removeObserver(observer);
120 void observeAccountCount(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
121 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
122 this.accountCount.observe(owner, observer);
124 void stopObservingAccountCount(@NonNull androidx.lifecycle.Observer<? super Integer> observer) {
125 this.accountCount.removeObserver(observer);
127 int getFocusedItem() { return focusedItem.getValue(); }
128 void setFocusedItem(int position) {
129 focusedItem.setValue(position);
131 int addAccount(LedgerTransactionAccount acc) {
132 items.add(new Item(this, acc));
133 accountCount.setValue(getAccountCount());
136 boolean accountsInInitialState() {
137 for (Item item : items) {
138 LedgerTransactionAccount acc = item.getAccount();
139 if (acc.isAmountSet())
141 if (!acc.getAccountName()
149 LedgerTransactionAccount getAccount(int index) {
150 return items.get(index)
153 Item getItem(int index) {
158 if (index <= items.size())
159 return items.get(index - 1);
164 A transaction is submittable if:
166 1) has at least two account names
167 2) each row with amount has account name
168 3) for each commodity:
169 3a) amounts must balance to 0, or
170 3b) there must be exactly one empty amount (with account)
171 4) empty accounts with empty amounts are ignored
173 5) a row with an empty account name or empty amount is guaranteed to exist for each commodity
174 6) at least two rows need to be present in the ledger
177 @SuppressLint("DefaultLocale")
178 void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) {
179 if (checkHoldCounter > 0)
183 final BalanceForCurrency balance = new BalanceForCurrency();
184 final String descriptionText = getDescription();
185 boolean submittable = true;
186 final ItemsForCurrency itemsForCurrency = new ItemsForCurrency();
187 final ItemsForCurrency itemsWithEmptyAmountForCurrency = new ItemsForCurrency();
188 final ItemsForCurrency itemsWithAccountAndEmptyAmountForCurrency = new ItemsForCurrency();
189 final ItemsForCurrency itemsWithEmptyAccountForCurrency = new ItemsForCurrency();
190 final ItemsForCurrency itemsWithAmountForCurrency = new ItemsForCurrency();
191 final ItemsForCurrency itemsWithAccountForCurrency = new ItemsForCurrency();
192 final ItemsForCurrency emptyRowsForCurrency = new ItemsForCurrency();
193 final List<Item> emptyRows = new ArrayList<>();
196 if ((descriptionText == null) || descriptionText.trim()
199 Logger.debug("submittable", "Transaction not submittable: missing description");
203 for (int i = 0; i < this.items.size(); i++) {
204 Item item = this.items.get(i);
206 LedgerTransactionAccount acc = item.getAccount();
207 String acc_name = acc.getAccountName()
209 String currName = acc.getCurrency();
211 itemsForCurrency.add(currName, item);
213 if (acc_name.isEmpty()) {
214 itemsWithEmptyAccountForCurrency.add(currName, item);
216 if (acc.isAmountSet()) {
217 // 2) each amount has account name
218 Logger.debug("submittable", String.format(
219 "Transaction not submittable: row %d has no account name, but" +
220 " has" + " amount %1.2f", i + 1, acc.getAmount()));
224 emptyRowsForCurrency.add(currName, item);
229 itemsWithAccountForCurrency.add(currName, item);
232 if (acc.isAmountSet()) {
233 itemsWithAmountForCurrency.add(currName, item);
234 balance.add(currName, acc.getAmount());
237 itemsWithEmptyAmountForCurrency.add(currName, item);
239 if (!acc_name.isEmpty())
240 itemsWithAccountAndEmptyAmountForCurrency.add(currName, item);
244 // 1) has at least two account names
247 Logger.debug("submittable",
248 "Transaction not submittable: no account " + "names");
249 else if (accounts == 1)
250 Logger.debug("submittable",
251 "Transaction not submittable: only one account name");
253 Logger.debug("submittable",
254 String.format("Transaction not submittable: only %d account names",
259 // 3) for each commodity:
260 // 3a) amount must balance to 0, or
261 // 3b) there must be exactly one empty amount (with account)
262 for (String balCurrency : itemsForCurrency.currencies()) {
263 float currencyBalance = balance.get(balCurrency);
264 if (Misc.isZero(currencyBalance)) {
265 // remove hints from all amount inputs in that currency
266 for (Item item : items) {
267 if (Currency.equal(item.getCurrency(), balCurrency))
268 item.setAmountHint(null);
273 itemsWithAccountAndEmptyAmountForCurrency.getList(balCurrency);
274 int balanceReceiversCount = list.size();
275 if (balanceReceiversCount != 1) {
276 if (BuildConfig.DEBUG) {
277 if (balanceReceiversCount == 0)
278 Logger.debug("submittable", String.format(
279 "Transaction not submittable [%s]: non-zero balance " +
280 "with no empty amounts with accounts", balCurrency));
282 Logger.debug("submittable", String.format(
283 "Transaction not submittable [%s]: non-zero balance " +
284 "with multiple empty amounts with accounts", balCurrency));
289 List<Item> emptyAmountList =
290 itemsWithEmptyAmountForCurrency.getList(balCurrency);
292 // suggest off-balance amount to a row and remove hints on other rows
293 Item receiver = null;
295 receiver = list.get(0);
296 else if (!emptyAmountList.isEmpty())
297 receiver = emptyAmountList.get(0);
299 for (Item item : items) {
300 if (!Currency.equal(item.getCurrency(), balCurrency))
303 if (item.equals(receiver)) {
304 if (BuildConfig.DEBUG)
305 Logger.debug("submittable",
306 String.format("Setting amount hint to %1.2f [%s]",
307 -currencyBalance, balCurrency));
308 item.setAmountHint(String.format("%1.2f", -currencyBalance));
311 if (BuildConfig.DEBUG)
312 Logger.debug("submittable",
313 String.format("Resetting hint of '%s' [%s]",
314 (item.getAccount() == null) ? "" : item.getAccount()
317 item.setAmountHint(null);
323 // 5) a row with an empty account name or empty amount is guaranteed to exist for
325 for (String balCurrency : balance.currencies()) {
326 int currEmptyRows = itemsWithEmptyAccountForCurrency.size(balCurrency);
327 int currRows = itemsForCurrency.size(balCurrency);
328 int currAccounts = itemsWithAccountForCurrency.size(balCurrency);
329 int currAmounts = itemsWithAmountForCurrency.size(balCurrency);
330 if ((currEmptyRows == 0) &&
331 ((currRows == currAccounts) || (currRows == currAmounts)))
333 // perhaps there already is an unused empty row for another currency that
335 // boolean foundIt = false;
336 // for (Item item : emptyRows) {
337 // Currency itemCurrency = item.getCurrency();
338 // String itemCurrencyName =
339 // (itemCurrency == null) ? "" : itemCurrency.getName();
340 // if (Misc.isZero(balance.get(itemCurrencyName))) {
341 // item.setCurrency(Currency.loadByName(balCurrency));
342 // item.setAmountHint(
343 // String.format("%1.2f", -balance.get(balCurrency)));
350 adapter.addRow(balCurrency);
354 // drop extra empty rows, not needed
355 for (String currName : emptyRowsForCurrency.currencies()) {
356 List<Item> emptyItems = emptyRowsForCurrency.getList(currName);
357 while ((this.items.size() > 2) && (emptyItems.size() > 1)) {
358 Item item = emptyItems.get(1);
359 emptyItems.remove(1);
360 removeRow(item, adapter);
363 // unused currency, remove last item (which is also an empty one)
364 if ((items.size() > 2) && (emptyItems.size() == 1)) {
365 List<Item> currItems = itemsForCurrency.getList(currName);
367 if (currItems.size() == 1){
368 Item item = emptyItems.get(0);
369 removeRow(item, adapter);
374 // 6) at least two rows need to be present in the ledger
375 while (this.items.size() < 2)
379 debug("submittable", submittable ? "YES" : "NO");
380 isSubmittable.setValue(submittable);
382 if (BuildConfig.DEBUG) {
383 debug("submittable", "== Dump of all items");
384 for (int i = 0; i < items.size(); i++) {
385 Item item = items.get(i);
386 LedgerTransactionAccount acc = item.getAccount();
387 debug("submittable", String.format("Item %2d: [%4.2f(%s) %s] %s ; %s", i,
388 acc.isAmountSet() ? acc.getAmount() : 0,
389 item.isAmountHintSet() ? item.getAmountHint() : "ø", acc.getCurrency(),
390 acc.getAccountName(), acc.getComment()));
394 catch (NumberFormatException e) {
395 debug("submittable", "NO (because of NumberFormatException)");
396 isSubmittable.setValue(false);
398 catch (Exception e) {
400 debug("submittable", "NO (because of an Exception)");
401 isSubmittable.setValue(false);
404 private void removeRow(Item item, NewTransactionItemsAdapter adapter) {
405 int pos = items.indexOf(item);
407 if (adapter != null) {
408 adapter.notifyItemRemoved(pos + 1);
409 sendCountNotifications();
412 void removeItem(int pos) {
414 accountCount.setValue(getAccountCount());
416 void sendCountNotifications() {
417 accountCount.setValue(getAccountCount());
419 public void sendFocusedNotification() {
420 focusedItem.setValue(focusedItem.getValue());
422 void updateFocusedItem(int position) {
423 focusedItem.setValue(position);
425 void noteFocusChanged(int position, FocusedElement element) {
426 getItem(position).setFocusedElement(element);
428 void swapItems(int one, int two) {
429 Collections.swap(items, one - 1, two - 1);
431 void toggleComment(int position) {
432 final MutableLiveData<Boolean> commentVisible = getItem(position).commentVisible;
433 commentVisible.postValue(!commentVisible.getValue());
435 void moveItemLast(int index) {
439 3 <-- desired position
441 int itemCount = items.size();
443 if (index < itemCount - 1) {
444 Item acc = items.remove(index);
445 items.add(itemCount - 1, acc);
448 void toggleCurrencyVisible() {
449 showCurrency.setValue(!showCurrency.getValue());
451 public void setItemCurrency(Item item, Currency newCurrency,
452 NewTransactionItemsAdapter adapter) {
453 Currency oldCurrency = item.getCurrency();
454 if (!Currency.equal(newCurrency, oldCurrency)) {
455 holdSubmittableChecks();
457 item.setCurrency(newCurrency);
458 // for (Item i : items) {
459 // if (Currency.equal(i.getCurrency(), oldCurrency))
460 // i.setCurrency(newCurrency);
464 releaseSubmittableChecks();
467 checkTransactionSubmittable(adapter);
470 enum ItemType {generalData, transactionRow, bottomFiller}
472 enum FocusedElement {Account, Comment, Amount}
474 private class ItemsForCurrency {
475 private HashMap<String, List<Item>> hashMap = new HashMap<>();
477 List<Item> getList(@Nullable String currencyName) {
478 List<Item> list = hashMap.get(currencyName);
480 list = new ArrayList<>();
481 hashMap.put(currencyName, list);
485 void add(@Nullable String currencyName, @NonNull Item item) {
486 getList(currencyName).add(item);
488 int size(@Nullable String currencyName) {
489 return this.getList(currencyName)
492 Set<String> currencies() {
493 return hashMap.keySet();
497 //==========================================================================================
499 private class BalanceForCurrency {
500 private HashMap<String, Float> hashMap = new HashMap<>();
501 float get(String currencyName) {
502 Float f = hashMap.get(currencyName);
505 hashMap.put(currencyName, f);
509 void add(String currencyName, float amount) {
510 hashMap.put(currencyName, get(currencyName) + amount);
512 Set<String> currencies() {
513 return hashMap.keySet();
515 boolean containsCurrency(String currencyName) {
516 return hashMap.containsKey(currencyName);
521 private ItemType type;
522 private MutableLiveData<Date> date = new MutableLiveData<>();
523 private MutableLiveData<String> description = new MutableLiveData<>();
524 private LedgerTransactionAccount account;
525 private MutableLiveData<String> amountHint = new MutableLiveData<>(null);
526 private NewTransactionModel model;
527 private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
528 private FocusedElement focusedElement = FocusedElement.Account;
529 private MutableLiveData<String> comment = new MutableLiveData<>(null);
530 private MutableLiveData<Boolean> commentVisible = new MutableLiveData<>(false);
531 private MutableLiveData<Currency> currency = new MutableLiveData<>(null);
532 private boolean amountHintIsSet = false;
533 Item(NewTransactionModel model) {
535 type = ItemType.bottomFiller;
536 editable.setValue(false);
538 Item(NewTransactionModel model, Date date, String description) {
540 this.type = ItemType.generalData;
541 this.date.setValue(date);
542 this.description.setValue(description);
543 this.editable.setValue(true);
545 Item(NewTransactionModel model, LedgerTransactionAccount account) {
547 this.type = ItemType.transactionRow;
548 this.account = account;
549 String currName = account.getCurrency();
550 Currency curr = null;
551 if ((currName != null) && !currName.isEmpty())
552 curr = Currency.loadByName(currName);
553 this.currency.setValue(curr);
554 this.editable.setValue(true);
556 FocusedElement getFocusedElement() {
557 return focusedElement;
559 void setFocusedElement(FocusedElement focusedElement) {
560 this.focusedElement = focusedElement;
562 public NewTransactionModel getModel() {
565 void setEditable(boolean editable) {
566 ensureType(ItemType.generalData, ItemType.transactionRow);
567 this.editable.setValue(editable);
569 private void ensureType(ItemType type1, ItemType type2) {
570 if ((type != type1) && (type != type2)) {
571 throw new RuntimeException(
572 String.format("Actual type (%s) differs from wanted (%s or %s)", type,
576 String getAmountHint() {
577 ensureType(ItemType.transactionRow);
578 return amountHint.getValue();
580 void setAmountHint(String amountHint) {
581 ensureType(ItemType.transactionRow);
583 // avoid unnecessary triggers
584 if (amountHint == null) {
585 if (this.amountHint.getValue() == null)
587 amountHintIsSet = false;
590 if (amountHint.equals(this.amountHint.getValue()))
592 amountHintIsSet = true;
595 this.amountHint.setValue(amountHint);
597 void observeAmountHint(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
598 @NonNull androidx.lifecycle.Observer<? super String> observer) {
599 this.amountHint.observe(owner, observer);
601 void stopObservingAmountHint(
602 @NonNull androidx.lifecycle.Observer<? super String> observer) {
603 this.amountHint.removeObserver(observer);
608 void ensureType(ItemType wantedType) {
609 if (type != wantedType) {
610 throw new RuntimeException(
611 String.format("Actual type (%s) differs from wanted (%s)", type,
615 public Date getDate() {
616 ensureType(ItemType.generalData);
617 return date.getValue();
619 public void setDate(Date date) {
620 ensureType(ItemType.generalData);
621 this.date.setValue(date);
623 public void setDate(String text) {
624 if ((text == null) || text.trim()
627 setDate((Date) null);
631 int year, month, day;
632 final Calendar c = GregorianCalendar.getInstance();
633 Matcher m = reYMD.matcher(text);
635 year = Integer.parseInt(m.group(1));
636 month = Integer.parseInt(m.group(2)) - 1; // month is 0-based
637 day = Integer.parseInt(m.group(3));
640 year = c.get(Calendar.YEAR);
641 m = reMD.matcher(text);
643 month = Integer.parseInt(m.group(1)) - 1;
644 day = Integer.parseInt(m.group(2));
647 month = c.get(Calendar.MONTH);
648 m = reD.matcher(text);
650 day = Integer.parseInt(m.group(1));
653 day = c.get(Calendar.DAY_OF_MONTH);
658 c.set(year, month, day);
660 this.setDate(c.getTime());
662 void observeDate(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
663 @NonNull androidx.lifecycle.Observer<? super Date> observer) {
664 this.date.observe(owner, observer);
666 void stopObservingDate(@NonNull androidx.lifecycle.Observer<? super Date> observer) {
667 this.date.removeObserver(observer);
669 public String getDescription() {
670 ensureType(ItemType.generalData);
671 return description.getValue();
673 public void setDescription(String description) {
674 ensureType(ItemType.generalData);
675 this.description.setValue(description);
677 void observeDescription(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
678 @NonNull androidx.lifecycle.Observer<? super String> observer) {
679 this.description.observe(owner, observer);
681 void stopObservingDescription(
682 @NonNull androidx.lifecycle.Observer<? super String> observer) {
683 this.description.removeObserver(observer);
685 public LedgerTransactionAccount getAccount() {
686 ensureType(ItemType.transactionRow);
689 public void setAccountName(String name) {
690 account.setAccountName(name);
695 * @return nicely formatted, shortest available date representation
697 String getFormattedDate() {
700 Date time = date.getValue();
704 Calendar c = GregorianCalendar.getInstance();
706 Calendar today = GregorianCalendar.getInstance();
708 final int myYear = c.get(Calendar.YEAR);
709 final int myMonth = c.get(Calendar.MONTH);
710 final int myDay = c.get(Calendar.DAY_OF_MONTH);
712 if (today.get(Calendar.YEAR) != myYear) {
713 return String.format(Locale.US, "%d/%02d/%02d", myYear, myMonth + 1, myDay);
716 if (today.get(Calendar.MONTH) != myMonth) {
717 return String.format(Locale.US, "%d/%02d", myMonth + 1, myDay);
720 return String.valueOf(myDay);
722 void observeEditableFlag(NewTransactionActivity activity, Observer<Boolean> observer) {
723 editable.observe(activity, observer);
725 void stopObservingEditableFlag(Observer<Boolean> observer) {
726 editable.removeObserver(observer);
728 void observeCommentVisible(NewTransactionActivity activity, Observer<Boolean> observer) {
729 commentVisible.observe(activity, observer);
731 void stopObservingCommentVisible(Observer<Boolean> observer) {
732 commentVisible.removeObserver(observer);
734 void observeComment(NewTransactionActivity activity, Observer<String> observer) {
735 comment.observe(activity, observer);
737 void stopObservingComment(Observer<String> observer) {
738 comment.removeObserver(observer);
740 public void setComment(String comment) {
741 getAccount().setComment(comment);
742 this.comment.postValue(comment);
744 public Currency getCurrency() {
745 return this.currency.getValue();
747 public void setCurrency(Currency currency) {
748 Currency present = this.currency.getValue();
749 if ((currency == null) && (present != null) ||
750 (currency != null) && !currency.equals(present))
752 getAccount().setCurrency((currency != null && !currency.getName()
754 ? currency.getName() : null);
755 this.currency.setValue(currency);
758 void observeCurrency(NewTransactionActivity activity, Observer<Currency> observer) {
759 currency.observe(activity, observer);
761 void stopObservingCurrency(Observer<Currency> observer) {
762 currency.removeObserver(observer);
764 boolean isOfType(ItemType type) {
765 return this.type == type;
767 boolean isAmountHintSet() {
768 return amountHintIsSet;