X-Git-Url: https://git.ktnx.net/?p=mobile-ledger.git;a=blobdiff_plain;f=app%2Fsrc%2Fmain%2Fjava%2Fnet%2Fktnx%2Fmobileledger%2Fui%2Factivity%2FNewTransactionModel.java;h=b647eb85e2ab48c60f4735a268a51db20006ed6e;hp=f5568081c6a88ba98e74f2a142c9864655a68334;hb=7ba876d1237f7ceb636035e0d6753195922c8b39;hpb=ee3c0ffdb714b35759198466f955e2333fdd8c8b diff --git a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java index f5568081..b647eb85 100644 --- a/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java +++ b/app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java @@ -1,5 +1,5 @@ /* - * Copyright © 2019 Damyan Ivanov. + * Copyright © 2021 Damyan Ivanov. * This file is part of MoLe. * MoLe is free software: you can distribute it and/or modify it * under the term of the GNU General Public License as published by @@ -28,38 +28,36 @@ import net.ktnx.mobileledger.model.Currency; import net.ktnx.mobileledger.model.Data; import net.ktnx.mobileledger.model.LedgerTransactionAccount; import net.ktnx.mobileledger.model.MobileLedgerProfile; +import net.ktnx.mobileledger.utils.Globals; +import net.ktnx.mobileledger.utils.SimpleDate; import org.jetbrains.annotations.NotNull; +import java.text.ParseException; import java.util.ArrayList; import java.util.Calendar; import java.util.Collections; -import java.util.Date; import java.util.GregorianCalendar; import java.util.Locale; import java.util.concurrent.atomic.AtomicInteger; -import java.util.regex.Matcher; -import java.util.regex.Pattern; public class NewTransactionModel extends ViewModel { - private static final Pattern reYMD = - Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$"); - private static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$"); - private static final Pattern reD = Pattern.compile("\\s*(\\d+)\\s*$"); final MutableLiveData showCurrency = new MutableLiveData<>(false); final ArrayList items = new ArrayList<>(); final MutableLiveData isSubmittable = new MutableLiveData<>(false); final MutableLiveData showComments = new MutableLiveData<>(true); - private final Item header = new Item(this, null, ""); + private final Item header = new Item(this, ""); private final Item trailer = new Item(this); private final MutableLiveData focusedItem = new MutableLiveData<>(0); private final MutableLiveData accountCount = new MutableLiveData<>(0); private final MutableLiveData simulateSave = new MutableLiveData<>(false); private final AtomicInteger busyCounter = new AtomicInteger(0); private final MutableLiveData busyFlag = new MutableLiveData<>(false); + private final Observer profileObserver = profile -> { + showCurrency.postValue(profile.getShowCommodityByDefault()); + showComments.postValue(profile.getShowCommentsByDefault()); + }; private boolean observingDataProfile; - private Observer profileObserver = - profile -> showCurrency.postValue(profile.getShowCommodityByDefault()); void observeShowComments(LifecycleOwner owner, Observer observer) { showComments.observe(owner, observer); } @@ -68,7 +66,7 @@ public class NewTransactionModel extends ViewModel { } void observeDataProfile(LifecycleOwner activity) { if (!observingDataProfile) - Data.profile.observe(activity, profileObserver); + Data.observeProfile(activity, profileObserver); observingDataProfile = true; } boolean getSimulateSave() { @@ -87,9 +85,12 @@ public class NewTransactionModel extends ViewModel { int getAccountCount() { return items.size(); } - public Date getDate() { + public SimpleDate getDate() { return header.date.getValue(); } + public void setDate(SimpleDate date) { + header.date.setValue(date); + } public String getDescription() { return header.description.getValue(); } @@ -229,26 +230,26 @@ public class NewTransactionModel extends ViewModel { static class Item { - private ItemType type; - private MutableLiveData date = new MutableLiveData<>(); - private MutableLiveData description = new MutableLiveData<>(); + private final ItemType type; + private final MutableLiveData date = new MutableLiveData<>(); + private final MutableLiveData description = new MutableLiveData<>(); + private final MutableLiveData amountHint = new MutableLiveData<>(null); + private final NewTransactionModel model; + private final MutableLiveData editable = new MutableLiveData<>(true); + private final MutableLiveData comment = new MutableLiveData<>(null); + private final MutableLiveData currency = new MutableLiveData<>(null); + private final MutableLiveData amountValid = new MutableLiveData<>(true); private LedgerTransactionAccount account; - private MutableLiveData amountHint = new MutableLiveData<>(null); - private NewTransactionModel model; - private MutableLiveData editable = new MutableLiveData<>(true); private FocusedElement focusedElement = FocusedElement.Account; - private MutableLiveData comment = new MutableLiveData<>(null); - private MutableLiveData currency = new MutableLiveData<>(null); private boolean amountHintIsSet = false; Item(NewTransactionModel model) { this.model = model; type = ItemType.bottomFiller; editable.setValue(false); } - Item(NewTransactionModel model, Date date, String description) { + Item(NewTransactionModel model, String description) { this.model = model; this.type = ItemType.generalData; - this.date.setValue(date); this.description.setValue(description); this.editable.setValue(true); } @@ -273,14 +274,14 @@ public class NewTransactionModel extends ViewModel { return model; } void setEditable(boolean editable) { - ensureType(ItemType.generalData, ItemType.transactionRow); + ensureTypeIsGeneralDataOrTransactionRow(); this.editable.setValue(editable); } - private void ensureType(ItemType type1, ItemType type2) { - if ((type != type1) && (type != type2)) { + private void ensureTypeIsGeneralDataOrTransactionRow() { + if ((type != ItemType.generalData) && (type != ItemType.transactionRow)) { throw new RuntimeException( String.format("Actual type (%s) differs from wanted (%s or %s)", type, - type1, type2)); + ItemType.generalData, ItemType.transactionRow)); } } String getAmountHint() { @@ -322,58 +323,30 @@ public class NewTransactionModel extends ViewModel { wantedType)); } } - public Date getDate() { + public SimpleDate getDate() { ensureType(ItemType.generalData); return date.getValue(); } - public void setDate(Date date) { + public void setDate(SimpleDate date) { ensureType(ItemType.generalData); this.date.setValue(date); } - public void setDate(String text) { + public void setDate(String text) throws ParseException { if ((text == null) || text.trim() .isEmpty()) { - setDate((Date) null); + setDate((SimpleDate) null); return; } - int year, month, day; - final Calendar c = GregorianCalendar.getInstance(); - Matcher m = reYMD.matcher(text); - if (m.matches()) { - year = Integer.parseInt(m.group(1)); - month = Integer.parseInt(m.group(2)) - 1; // month is 0-based - day = Integer.parseInt(m.group(3)); - } - else { - year = c.get(Calendar.YEAR); - m = reMD.matcher(text); - if (m.matches()) { - month = Integer.parseInt(m.group(1)) - 1; - day = Integer.parseInt(m.group(2)); - } - else { - month = c.get(Calendar.MONTH); - m = reD.matcher(text); - if (m.matches()) { - day = Integer.parseInt(m.group(1)); - } - else { - day = c.get(Calendar.DAY_OF_MONTH); - } - } - } - - c.set(year, month, day); - - this.setDate(c.getTime()); + SimpleDate date = Globals.parseLedgerDate(text); + this.setDate(date); } void observeDate(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner, - @NonNull androidx.lifecycle.Observer observer) { + @NonNull androidx.lifecycle.Observer observer) { this.date.observe(owner, observer); } - void stopObservingDate(@NonNull androidx.lifecycle.Observer observer) { + void stopObservingDate(@NonNull androidx.lifecycle.Observer observer) { this.date.removeObserver(observer); } public String getDescription() { @@ -406,7 +379,6 @@ public class NewTransactionModel extends ViewModel { this.comment.observe(owner, observer); } void stopObservingTransactionComment(@NonNull Observer observer) { - ensureType(ItemType.generalData); this.comment.removeObserver(observer); } public LedgerTransactionAccount getAccount() { @@ -424,27 +396,21 @@ public class NewTransactionModel extends ViewModel { String getFormattedDate() { if (date == null) return null; - Date time = date.getValue(); - if (time == null) + SimpleDate d = date.getValue(); + if (d == null) return null; - Calendar c = GregorianCalendar.getInstance(); - c.setTime(time); Calendar today = GregorianCalendar.getInstance(); - final int myYear = c.get(Calendar.YEAR); - final int myMonth = c.get(Calendar.MONTH); - final int myDay = c.get(Calendar.DAY_OF_MONTH); - - if (today.get(Calendar.YEAR) != myYear) { - return String.format(Locale.US, "%d/%02d/%02d", myYear, myMonth + 1, myDay); + if (today.get(Calendar.YEAR) != d.year) { + return String.format(Locale.US, "%d/%02d/%02d", d.year, d.month, d.day); } - if (today.get(Calendar.MONTH) != myMonth) { - return String.format(Locale.US, "%d/%02d", myMonth + 1, myDay); + if (today.get(Calendar.MONTH) != d.month - 1) { + return String.format(Locale.US, "%d/%02d", d.month, d.day); } - return String.valueOf(myDay); + return String.valueOf(d.day); } void observeEditableFlag(NewTransactionActivity activity, Observer observer) { editable.observe(activity, observer); @@ -482,11 +448,23 @@ public class NewTransactionModel extends ViewModel { void stopObservingCurrency(Observer observer) { currency.removeObserver(observer); } - boolean isOfType(ItemType type) { - return this.type == type; + boolean isBottomFiller() { + return this.type == ItemType.bottomFiller; } boolean isAmountHintSet() { return amountHintIsSet; } + void validateAmount() { + amountValid.setValue(true); + } + void invalidateAmount() { + amountValid.setValue(false); + } + void observeAmountValidity(NewTransactionActivity activity, Observer observer) { + amountValid.observe(activity, observer); + } + void stopObservingAmountValidity(Observer observer) { + amountValid.removeObserver(observer); + } } }