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 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;
32 import org.jetbrains.annotations.NotNull;
34 import java.util.ArrayList;
35 import java.util.Calendar;
36 import java.util.Collections;
37 import java.util.Date;
38 import java.util.GregorianCalendar;
39 import java.util.Locale;
40 import java.util.regex.Matcher;
41 import java.util.regex.Pattern;
43 public class NewTransactionModel extends ViewModel {
44 private static final Pattern reYMD =
45 Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
46 private static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
47 private static final Pattern reD = Pattern.compile("\\s*(\\d+)\\s*$");
48 final MutableLiveData<Boolean> showCurrency = new MutableLiveData<>(false);
49 final ArrayList<Item> items = new ArrayList<>();
50 final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
51 private final Item header = new Item(this, null, "");
52 private final Item trailer = new Item(this);
53 private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(0);
54 private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
55 private final MutableLiveData<Boolean> simulateSave = new MutableLiveData<>(false);
56 private boolean observingDataProfile;
57 private Observer<MobileLedgerProfile> profileObserver =
58 profile -> showCurrency.postValue(profile.getShowCommodityByDefault());
59 void observeDataProfile(LifecycleOwner activity) {
60 if (!observingDataProfile)
61 Data.profile.observe(activity, profileObserver);
62 observingDataProfile = true;
64 boolean getSimulateSave() {
65 return simulateSave.getValue();
67 public void setSimulateSave(boolean simulateSave) {
68 this.simulateSave.setValue(simulateSave);
70 void toggleSimulateSave() {
71 simulateSave.setValue(!simulateSave.getValue());
73 void observeSimulateSave(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
74 @NonNull androidx.lifecycle.Observer<? super Boolean> observer) {
75 this.simulateSave.observe(owner, observer);
77 int getAccountCount() {
80 public Date getDate() {
81 return header.date.getValue();
83 public String getDescription() {
84 return header.description.getValue();
86 LiveData<Boolean> isSubmittable() {
87 return this.isSubmittable;
90 header.date.setValue(null);
91 header.description.setValue(null);
93 items.add(new Item(this, new LedgerTransactionAccount("")));
94 items.add(new Item(this, new LedgerTransactionAccount("")));
95 focusedItem.setValue(0);
97 void observeFocusedItem(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
98 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
99 this.focusedItem.observe(owner, observer);
101 void stopObservingFocusedItem(@NonNull androidx.lifecycle.Observer<? super Integer> observer) {
102 this.focusedItem.removeObserver(observer);
104 void observeAccountCount(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
105 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
106 this.accountCount.observe(owner, observer);
108 void stopObservingAccountCount(@NonNull androidx.lifecycle.Observer<? super Integer> observer) {
109 this.accountCount.removeObserver(observer);
111 int getFocusedItem() { return focusedItem.getValue(); }
112 void setFocusedItem(int position) {
113 focusedItem.setValue(position);
115 int addAccount(LedgerTransactionAccount acc) {
116 items.add(new Item(this, acc));
117 accountCount.setValue(getAccountCount());
120 boolean accountsInInitialState() {
121 for (Item item : items) {
122 LedgerTransactionAccount acc = item.getAccount();
123 if (acc.isAmountSet())
125 if (!acc.getAccountName()
133 LedgerTransactionAccount getAccount(int index) {
134 return items.get(index)
137 Item getItem(int index) {
142 if (index <= items.size())
143 return items.get(index - 1);
147 void removeRow(Item item, NewTransactionItemsAdapter adapter) {
148 int pos = items.indexOf(item);
150 if (adapter != null) {
151 adapter.notifyItemRemoved(pos + 1);
152 sendCountNotifications();
155 void removeItem(int pos) {
157 accountCount.setValue(getAccountCount());
159 void sendCountNotifications() {
160 accountCount.setValue(getAccountCount());
162 public void sendFocusedNotification() {
163 focusedItem.setValue(focusedItem.getValue());
165 void updateFocusedItem(int position) {
166 focusedItem.setValue(position);
168 void noteFocusChanged(int position, FocusedElement element) {
169 getItem(position).setFocusedElement(element);
171 void swapItems(int one, int two) {
172 Collections.swap(items, one - 1, two - 1);
174 void moveItemLast(int index) {
178 3 <-- desired position
180 int itemCount = items.size();
182 if (index < itemCount - 1) {
183 Item acc = items.remove(index);
184 items.add(itemCount - 1, acc);
187 void toggleCurrencyVisible() {
188 showCurrency.setValue(!showCurrency.getValue());
190 enum ItemType {generalData, transactionRow, bottomFiller}
192 enum FocusedElement {Account, Comment, Amount}
195 //==========================================================================================
199 private ItemType type;
200 private MutableLiveData<Date> date = new MutableLiveData<>();
201 private MutableLiveData<String> description = new MutableLiveData<>();
202 private LedgerTransactionAccount account;
203 private MutableLiveData<String> amountHint = new MutableLiveData<>(null);
204 private NewTransactionModel model;
205 private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
206 private FocusedElement focusedElement = FocusedElement.Account;
207 private MutableLiveData<String> comment = new MutableLiveData<>(null);
208 private MutableLiveData<Currency> currency = new MutableLiveData<>(null);
209 private boolean amountHintIsSet = false;
210 Item(NewTransactionModel model) {
212 type = ItemType.bottomFiller;
213 editable.setValue(false);
215 Item(NewTransactionModel model, Date date, String description) {
217 this.type = ItemType.generalData;
218 this.date.setValue(date);
219 this.description.setValue(description);
220 this.editable.setValue(true);
222 Item(NewTransactionModel model, LedgerTransactionAccount account) {
224 this.type = ItemType.transactionRow;
225 this.account = account;
226 String currName = account.getCurrency();
227 Currency curr = null;
228 if ((currName != null) && !currName.isEmpty())
229 curr = Currency.loadByName(currName);
230 this.currency.setValue(curr);
231 this.editable.setValue(true);
233 FocusedElement getFocusedElement() {
234 return focusedElement;
236 void setFocusedElement(FocusedElement focusedElement) {
237 this.focusedElement = focusedElement;
239 public NewTransactionModel getModel() {
242 void setEditable(boolean editable) {
243 ensureType(ItemType.generalData, ItemType.transactionRow);
244 this.editable.setValue(editable);
246 private void ensureType(ItemType type1, ItemType type2) {
247 if ((type != type1) && (type != type2)) {
248 throw new RuntimeException(
249 String.format("Actual type (%s) differs from wanted (%s or %s)", type,
253 String getAmountHint() {
254 ensureType(ItemType.transactionRow);
255 return amountHint.getValue();
257 void setAmountHint(String amountHint) {
258 ensureType(ItemType.transactionRow);
260 // avoid unnecessary triggers
261 if (amountHint == null) {
262 if (this.amountHint.getValue() == null)
264 amountHintIsSet = false;
267 if (amountHint.equals(this.amountHint.getValue()))
269 amountHintIsSet = true;
272 this.amountHint.setValue(amountHint);
274 void observeAmountHint(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
275 @NonNull androidx.lifecycle.Observer<? super String> observer) {
276 this.amountHint.observe(owner, observer);
278 void stopObservingAmountHint(
279 @NonNull androidx.lifecycle.Observer<? super String> observer) {
280 this.amountHint.removeObserver(observer);
285 void ensureType(ItemType wantedType) {
286 if (type != wantedType) {
287 throw new RuntimeException(
288 String.format("Actual type (%s) differs from wanted (%s)", type,
292 public Date getDate() {
293 ensureType(ItemType.generalData);
294 return date.getValue();
296 public void setDate(Date date) {
297 ensureType(ItemType.generalData);
298 this.date.setValue(date);
300 public void setDate(String text) {
301 if ((text == null) || text.trim()
304 setDate((Date) null);
308 int year, month, day;
309 final Calendar c = GregorianCalendar.getInstance();
310 Matcher m = reYMD.matcher(text);
312 year = Integer.parseInt(m.group(1));
313 month = Integer.parseInt(m.group(2)) - 1; // month is 0-based
314 day = Integer.parseInt(m.group(3));
317 year = c.get(Calendar.YEAR);
318 m = reMD.matcher(text);
320 month = Integer.parseInt(m.group(1)) - 1;
321 day = Integer.parseInt(m.group(2));
324 month = c.get(Calendar.MONTH);
325 m = reD.matcher(text);
327 day = Integer.parseInt(m.group(1));
330 day = c.get(Calendar.DAY_OF_MONTH);
335 c.set(year, month, day);
337 this.setDate(c.getTime());
339 void observeDate(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
340 @NonNull androidx.lifecycle.Observer<? super Date> observer) {
341 this.date.observe(owner, observer);
343 void stopObservingDate(@NonNull androidx.lifecycle.Observer<? super Date> observer) {
344 this.date.removeObserver(observer);
346 public String getDescription() {
347 ensureType(ItemType.generalData);
348 return description.getValue();
350 public void setDescription(String description) {
351 ensureType(ItemType.generalData);
352 this.description.setValue(description);
354 void observeDescription(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
355 @NonNull androidx.lifecycle.Observer<? super String> observer) {
356 this.description.observe(owner, observer);
358 void stopObservingDescription(
359 @NonNull androidx.lifecycle.Observer<? super String> observer) {
360 this.description.removeObserver(observer);
362 public LedgerTransactionAccount getAccount() {
363 ensureType(ItemType.transactionRow);
366 public void setAccountName(String name) {
367 account.setAccountName(name);
372 * @return nicely formatted, shortest available date representation
374 String getFormattedDate() {
377 Date time = date.getValue();
381 Calendar c = GregorianCalendar.getInstance();
383 Calendar today = GregorianCalendar.getInstance();
385 final int myYear = c.get(Calendar.YEAR);
386 final int myMonth = c.get(Calendar.MONTH);
387 final int myDay = c.get(Calendar.DAY_OF_MONTH);
389 if (today.get(Calendar.YEAR) != myYear) {
390 return String.format(Locale.US, "%d/%02d/%02d", myYear, myMonth + 1, myDay);
393 if (today.get(Calendar.MONTH) != myMonth) {
394 return String.format(Locale.US, "%d/%02d", myMonth + 1, myDay);
397 return String.valueOf(myDay);
399 void observeEditableFlag(NewTransactionActivity activity, Observer<Boolean> observer) {
400 editable.observe(activity, observer);
402 void stopObservingEditableFlag(Observer<Boolean> observer) {
403 editable.removeObserver(observer);
405 void observeComment(NewTransactionActivity activity, Observer<String> observer) {
406 comment.observe(activity, observer);
408 void stopObservingComment(Observer<String> observer) {
409 comment.removeObserver(observer);
411 public void setComment(String comment) {
412 getAccount().setComment(comment);
413 this.comment.postValue(comment);
415 public Currency getCurrency() {
416 return this.currency.getValue();
418 public void setCurrency(Currency currency) {
419 Currency present = this.currency.getValue();
420 if ((currency == null) && (present != null) ||
421 (currency != null) && !currency.equals(present))
423 getAccount().setCurrency((currency != null && !currency.getName()
425 ? currency.getName() : null);
426 this.currency.setValue(currency);
429 void observeCurrency(NewTransactionActivity activity, Observer<Currency> observer) {
430 currency.observe(activity, observer);
432 void stopObservingCurrency(Observer<Currency> observer) {
433 currency.removeObserver(observer);
435 boolean isOfType(ItemType type) {
436 return this.type == type;
438 boolean isAmountHintSet() {
439 return amountHintIsSet;