2 * Copyright © 2020 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 androidx.annotation.NonNull;
21 import androidx.lifecycle.LifecycleOwner;
22 import androidx.lifecycle.LiveData;
23 import androidx.lifecycle.MutableLiveData;
24 import androidx.lifecycle.Observer;
25 import androidx.lifecycle.ViewModel;
27 import net.ktnx.mobileledger.model.Currency;
28 import net.ktnx.mobileledger.model.Data;
29 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
30 import net.ktnx.mobileledger.model.MobileLedgerProfile;
31 import net.ktnx.mobileledger.utils.Globals;
32 import net.ktnx.mobileledger.utils.SimpleDate;
34 import org.jetbrains.annotations.NotNull;
36 import java.text.ParseException;
37 import java.util.ArrayList;
38 import java.util.Calendar;
39 import java.util.Collections;
40 import java.util.GregorianCalendar;
41 import java.util.Locale;
42 import java.util.concurrent.atomic.AtomicInteger;
44 public class NewTransactionModel extends ViewModel {
45 final MutableLiveData<Boolean> showCurrency = new MutableLiveData<>(false);
46 final ArrayList<Item> items = new ArrayList<>();
47 final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
48 final MutableLiveData<Boolean> showComments = new MutableLiveData<>(true);
49 private final Item header = new Item(this, "");
50 private final Item trailer = new Item(this);
51 private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(0);
52 private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
53 private final MutableLiveData<Boolean> simulateSave = new MutableLiveData<>(false);
54 private final AtomicInteger busyCounter = new AtomicInteger(0);
55 private final MutableLiveData<Boolean> busyFlag = new MutableLiveData<>(false);
56 private final Observer<MobileLedgerProfile> profileObserver = profile -> {
57 showCurrency.postValue(profile.getShowCommodityByDefault());
58 showComments.postValue(profile.getShowCommentsByDefault());
60 private boolean observingDataProfile;
61 void observeShowComments(LifecycleOwner owner, Observer<? super Boolean> observer) {
62 showComments.observe(owner, observer);
64 void observeBusyFlag(@NonNull LifecycleOwner owner, Observer<? super Boolean> observer) {
65 busyFlag.observe(owner, observer);
67 void observeDataProfile(LifecycleOwner activity) {
68 if (!observingDataProfile)
69 Data.observeProfile(activity, profileObserver);
70 observingDataProfile = true;
72 boolean getSimulateSave() {
73 return simulateSave.getValue();
75 public void setSimulateSave(boolean simulateSave) {
76 this.simulateSave.setValue(simulateSave);
78 void toggleSimulateSave() {
79 simulateSave.setValue(!simulateSave.getValue());
81 void observeSimulateSave(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
82 @NonNull androidx.lifecycle.Observer<? super Boolean> observer) {
83 this.simulateSave.observe(owner, observer);
85 int getAccountCount() {
88 public SimpleDate getDate() {
89 return header.date.getValue();
91 public String getDescription() {
92 return header.description.getValue();
94 public String getComment() {
95 return header.comment.getValue();
97 LiveData<Boolean> isSubmittable() {
98 return this.isSubmittable;
101 header.date.setValue(null);
102 header.description.setValue(null);
103 header.comment.setValue(null);
105 items.add(new Item(this, new LedgerTransactionAccount("")));
106 items.add(new Item(this, new LedgerTransactionAccount("")));
107 focusedItem.setValue(0);
109 void observeFocusedItem(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
110 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
111 this.focusedItem.observe(owner, observer);
113 void stopObservingFocusedItem(@NonNull androidx.lifecycle.Observer<? super Integer> observer) {
114 this.focusedItem.removeObserver(observer);
116 void observeAccountCount(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
117 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
118 this.accountCount.observe(owner, observer);
120 void stopObservingAccountCount(@NonNull androidx.lifecycle.Observer<? super Integer> observer) {
121 this.accountCount.removeObserver(observer);
123 int getFocusedItem() { return focusedItem.getValue(); }
124 void setFocusedItem(int position) {
125 focusedItem.setValue(position);
127 int addAccount(LedgerTransactionAccount acc) {
128 items.add(new Item(this, acc));
129 accountCount.setValue(getAccountCount());
132 boolean accountsInInitialState() {
133 for (Item item : items) {
134 LedgerTransactionAccount acc = item.getAccount();
135 if (acc.isAmountSet())
137 if (!acc.getAccountName()
145 LedgerTransactionAccount getAccount(int index) {
146 return items.get(index)
149 Item getItem(int index) {
154 if (index <= items.size())
155 return items.get(index - 1);
159 void removeRow(Item item, NewTransactionItemsAdapter adapter) {
160 int pos = items.indexOf(item);
162 if (adapter != null) {
163 adapter.notifyItemRemoved(pos + 1);
164 sendCountNotifications();
167 void removeItem(int pos) {
169 accountCount.setValue(getAccountCount());
171 void sendCountNotifications() {
172 accountCount.setValue(getAccountCount());
174 public void sendFocusedNotification() {
175 focusedItem.setValue(focusedItem.getValue());
177 void updateFocusedItem(int position) {
178 focusedItem.setValue(position);
180 void noteFocusChanged(int position, FocusedElement element) {
181 getItem(position).setFocusedElement(element);
183 void swapItems(int one, int two) {
184 Collections.swap(items, one - 1, two - 1);
186 void moveItemLast(int index) {
190 3 <-- desired position
192 int itemCount = items.size();
194 if (index < itemCount - 1) {
195 Item acc = items.remove(index);
196 items.add(itemCount - 1, acc);
199 void toggleCurrencyVisible() {
200 showCurrency.setValue(!showCurrency.getValue());
202 void stopObservingBusyFlag(Observer<Boolean> observer) {
203 busyFlag.removeObserver(observer);
205 void incrementBusyCounter() {
206 int newValue = busyCounter.incrementAndGet();
208 busyFlag.postValue(true);
210 void decrementBusyCounter() {
211 int newValue = busyCounter.decrementAndGet();
213 busyFlag.postValue(false);
215 public boolean getBusyFlag() {
216 return busyFlag.getValue();
218 public void toggleShowComments() {
219 showComments.setValue(!showComments.getValue());
221 enum ItemType {generalData, transactionRow, bottomFiller}
223 enum FocusedElement {Account, Comment, Amount, Description, TransactionComment}
226 //==========================================================================================
230 private final ItemType type;
231 private final MutableLiveData<SimpleDate> date = new MutableLiveData<>();
232 private final MutableLiveData<String> description = new MutableLiveData<>();
233 private final MutableLiveData<String> amountHint = new MutableLiveData<>(null);
234 private final NewTransactionModel model;
235 private final MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
236 private final MutableLiveData<String> comment = new MutableLiveData<>(null);
237 private final MutableLiveData<Currency> currency = new MutableLiveData<>(null);
238 private final MutableLiveData<Boolean> amountValid = new MutableLiveData<>(true);
239 private LedgerTransactionAccount account;
240 private FocusedElement focusedElement = FocusedElement.Account;
241 private boolean amountHintIsSet = false;
242 Item(NewTransactionModel model) {
244 type = ItemType.bottomFiller;
245 editable.setValue(false);
247 Item(NewTransactionModel model, String description) {
249 this.type = ItemType.generalData;
250 this.description.setValue(description);
251 this.editable.setValue(true);
253 Item(NewTransactionModel model, LedgerTransactionAccount account) {
255 this.type = ItemType.transactionRow;
256 this.account = account;
257 String currName = account.getCurrency();
258 Currency curr = null;
259 if ((currName != null) && !currName.isEmpty())
260 curr = Currency.loadByName(currName);
261 this.currency.setValue(curr);
262 this.editable.setValue(true);
264 FocusedElement getFocusedElement() {
265 return focusedElement;
267 void setFocusedElement(FocusedElement focusedElement) {
268 this.focusedElement = focusedElement;
270 public NewTransactionModel getModel() {
273 void setEditable(boolean editable) {
274 ensureTypeIsGeneralDataOrTransactionRow();
275 this.editable.setValue(editable);
277 private void ensureTypeIsGeneralDataOrTransactionRow() {
278 if ((type != ItemType.generalData) && (type != ItemType.transactionRow)) {
279 throw new RuntimeException(
280 String.format("Actual type (%s) differs from wanted (%s or %s)", type,
281 ItemType.generalData, ItemType.transactionRow));
284 String getAmountHint() {
285 ensureType(ItemType.transactionRow);
286 return amountHint.getValue();
288 void setAmountHint(String amountHint) {
289 ensureType(ItemType.transactionRow);
291 // avoid unnecessary triggers
292 if (amountHint == null) {
293 if (this.amountHint.getValue() == null)
295 amountHintIsSet = false;
298 if (amountHint.equals(this.amountHint.getValue()))
300 amountHintIsSet = true;
303 this.amountHint.setValue(amountHint);
305 void observeAmountHint(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
306 @NonNull androidx.lifecycle.Observer<? super String> observer) {
307 this.amountHint.observe(owner, observer);
309 void stopObservingAmountHint(
310 @NonNull androidx.lifecycle.Observer<? super String> observer) {
311 this.amountHint.removeObserver(observer);
316 void ensureType(ItemType wantedType) {
317 if (type != wantedType) {
318 throw new RuntimeException(
319 String.format("Actual type (%s) differs from wanted (%s)", type,
323 public SimpleDate getDate() {
324 ensureType(ItemType.generalData);
325 return date.getValue();
327 public void setDate(SimpleDate date) {
328 ensureType(ItemType.generalData);
329 this.date.setValue(date);
331 public void setDate(String text) throws ParseException {
332 if ((text == null) || text.trim()
335 setDate((SimpleDate) null);
339 SimpleDate date = Globals.parseLedgerDate(text);
342 void observeDate(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
343 @NonNull androidx.lifecycle.Observer<? super SimpleDate> observer) {
344 this.date.observe(owner, observer);
346 void stopObservingDate(@NonNull androidx.lifecycle.Observer<? super SimpleDate> observer) {
347 this.date.removeObserver(observer);
349 public String getDescription() {
350 ensureType(ItemType.generalData);
351 return description.getValue();
353 public void setDescription(String description) {
354 ensureType(ItemType.generalData);
355 this.description.setValue(description);
357 void observeDescription(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
358 @NonNull androidx.lifecycle.Observer<? super String> observer) {
359 this.description.observe(owner, observer);
361 void stopObservingDescription(
362 @NonNull androidx.lifecycle.Observer<? super String> observer) {
363 this.description.removeObserver(observer);
365 public String getTransactionComment() {
366 ensureType(ItemType.generalData);
367 return comment.getValue();
369 public void setTransactionComment(String transactionComment) {
370 ensureType(ItemType.generalData);
371 this.comment.setValue(transactionComment);
373 void observeTransactionComment(@NonNull @NotNull LifecycleOwner owner,
374 @NonNull Observer<? super String> observer) {
375 ensureType(ItemType.generalData);
376 this.comment.observe(owner, observer);
378 void stopObservingTransactionComment(@NonNull Observer<? super String> observer) {
379 this.comment.removeObserver(observer);
381 public LedgerTransactionAccount getAccount() {
382 ensureType(ItemType.transactionRow);
385 public void setAccountName(String name) {
386 account.setAccountName(name);
391 * @return nicely formatted, shortest available date representation
393 String getFormattedDate() {
396 SimpleDate d = date.getValue();
400 Calendar today = GregorianCalendar.getInstance();
402 if (today.get(Calendar.YEAR) != d.year) {
403 return String.format(Locale.US, "%d/%02d/%02d", d.year, d.month, d.day);
406 if (today.get(Calendar.MONTH) != d.month - 1) {
407 return String.format(Locale.US, "%d/%02d", d.month, d.day);
410 return String.valueOf(d.day);
412 void observeEditableFlag(NewTransactionActivity activity, Observer<Boolean> observer) {
413 editable.observe(activity, observer);
415 void stopObservingEditableFlag(Observer<Boolean> observer) {
416 editable.removeObserver(observer);
418 void observeComment(NewTransactionActivity activity, Observer<String> observer) {
419 comment.observe(activity, observer);
421 void stopObservingComment(Observer<String> observer) {
422 comment.removeObserver(observer);
424 public void setComment(String comment) {
425 getAccount().setComment(comment);
426 this.comment.postValue(comment);
428 public Currency getCurrency() {
429 return this.currency.getValue();
431 public void setCurrency(Currency currency) {
432 Currency present = this.currency.getValue();
433 if ((currency == null) && (present != null) ||
434 (currency != null) && !currency.equals(present))
436 getAccount().setCurrency((currency != null && !currency.getName()
438 ? currency.getName() : null);
439 this.currency.setValue(currency);
442 void observeCurrency(NewTransactionActivity activity, Observer<Currency> observer) {
443 currency.observe(activity, observer);
445 void stopObservingCurrency(Observer<Currency> observer) {
446 currency.removeObserver(observer);
448 boolean isBottomFiller() {
449 return this.type == ItemType.bottomFiller;
451 boolean isAmountHintSet() {
452 return amountHintIsSet;
454 void validateAmount() {
455 amountValid.setValue(true);
457 void invalidateAmount() {
458 amountValid.setValue(false);
460 void observeAmountValidity(NewTransactionActivity activity, Observer<Boolean> observer) {
461 amountValid.observe(activity, observer);
463 void stopObservingAmountValidity(Observer<Boolean> observer) {
464 amountValid.removeObserver(observer);