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.LiveData;
22 import androidx.lifecycle.MutableLiveData;
23 import androidx.lifecycle.Observer;
24 import androidx.lifecycle.ViewModel;
26 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
27 import net.ktnx.mobileledger.utils.Misc;
29 import org.jetbrains.annotations.NotNull;
31 import java.util.ArrayList;
32 import java.util.Calendar;
33 import java.util.Date;
34 import java.util.GregorianCalendar;
35 import java.util.Locale;
36 import java.util.regex.Matcher;
37 import java.util.regex.Pattern;
39 import static net.ktnx.mobileledger.utils.Logger.debug;
40 import static net.ktnx.mobileledger.utils.Misc.isZero;
42 public class NewTransactionModel extends ViewModel {
43 static final Pattern reYMD = Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
44 static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
45 static final Pattern reD = Pattern.compile("\\s*(\\d+)\\s*$");
46 private static final String ZERO_AMOUNT_HINT = "0.00";
47 private final Item header = new Item(this, null, "");
48 private final Item trailer = new Item(this);
49 private final ArrayList<Item> items = new ArrayList<>();
50 private final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
51 private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(null);
52 private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
53 public int getAccountCount() {
56 public Date getDate() {
57 return header.date.getValue();
59 public String getDescription() {
60 return header.description.getValue();
62 public LiveData<Boolean> isSubmittable() {
63 return this.isSubmittable;
66 header.date.setValue(null);
67 header.description.setValue(null);
69 items.add(new Item(this, new LedgerTransactionAccount("")));
70 items.add(new Item(this, new LedgerTransactionAccount("")));
71 focusedItem.setValue(0);
73 public void observeFocusedItem(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
74 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
75 this.focusedItem.observe(owner, observer);
77 public void stopObservingFocusedItem(
78 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
79 this.focusedItem.removeObserver(observer);
81 public void observeAccountCount(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
83 androidx.lifecycle.Observer<? super Integer> observer) {
84 this.accountCount.observe(owner, observer);
86 public void stopObservingAccountCount(
87 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
88 this.accountCount.removeObserver(observer);
90 public void setFocusedItem(int position) {
91 focusedItem.setValue(position);
93 public int addAccount(LedgerTransactionAccount acc) {
94 items.add(new Item(this, acc));
95 accountCount.setValue(getAccountCount());
98 boolean accountsInInitialState() {
99 for (Item item : items) {
100 LedgerTransactionAccount acc = item.getAccount();
101 if (acc.isAmountSet()) return false;
102 if (!acc.getAccountName()
104 .isEmpty()) return false;
109 LedgerTransactionAccount getAccount(int index) {
110 return items.get(index)
113 public Item getItem(int index) {
118 if (index <= items.size()) return items.get(index - 1);
123 // 1) at least two account names
124 // 2) each amount must have account name
125 // 3) amounts must balance to 0, or
126 // 3a) there must be exactly one empty amount
127 // 4) empty accounts with empty amounts are ignored
128 // 5) a row with an empty account name or empty amount is guaranteed to exist
129 public void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) {
131 int accounts_with_values = 0;
133 int amounts_with_accounts = 0;
135 Item empty_amount = null;
136 boolean single_empty_amount = false;
137 boolean single_empty_amount_has_account = false;
138 float running_total = 0f;
139 final String descriptionText = getDescription();
140 final boolean have_description = ((descriptionText != null) && !descriptionText.isEmpty());
143 for (int i = 0; i < this.items.size(); i++) {
144 Item item = this.items.get(i);
146 LedgerTransactionAccount acc = item.getAccount();
147 String acc_name = acc.getAccountName()
149 if (acc_name.isEmpty()) {
155 if (acc.isAmountSet()) {
156 accounts_with_values++;
160 if (acc.isAmountSet()) {
162 if (!acc_name.isEmpty())
163 amounts_with_accounts++;
164 running_total += acc.getAmount();
167 if (empty_amount == null) {
169 single_empty_amount = true;
170 single_empty_amount_has_account = !acc_name.isEmpty();
172 else if (!acc_name.isEmpty())
173 single_empty_amount = false;
177 if ((empty_rows == 0) &&
178 ((this.items.size() == accounts) || (this.items.size() == amounts)))
183 for (NewTransactionModel.Item item : items) {
185 final LedgerTransactionAccount acc = item.getAccount();
186 if (acc.isAmountSet())
189 if (single_empty_amount) {
190 if (item.equals(empty_amount)) {
191 empty_amount.setAmountHint(String.format(Locale.US, "%1.2f",
192 Misc.isZero(running_total) ? 0f : -running_total));
197 // no single empty account and this account's amount is not set
198 // => hint should be '0.00'
199 String hint = item.getAmountHint();
200 if ((hint == null) || !hint.equals(ZERO_AMOUNT_HINT)) {
201 item.setAmountHint(ZERO_AMOUNT_HINT);
207 debug("submittable", String.format(Locale.US,
208 "%s, accounts=%d, accounts_with_values=%s, " +
209 "amounts_with_accounts=%d, amounts=%d, running_total=%1.2f, " +
210 "single_empty_with_acc=%s", have_description ? "description" : "NO description",
211 accounts, accounts_with_values, amounts_with_accounts, amounts, running_total,
212 (single_empty_amount && single_empty_amount_has_account) ? "true" : "false"));
214 if (have_description && (accounts >= 2) && (accounts_with_values >= (accounts - 1)) &&
215 (amounts_with_accounts == amounts) &&
216 (single_empty_amount && single_empty_amount_has_account || isZero(running_total)))
218 debug("submittable", "YES");
219 isSubmittable.setValue(true);
222 debug("submittable", "NO");
223 isSubmittable.setValue(false);
227 catch (NumberFormatException e) {
228 debug("submittable", "NO (because of NumberFormatException)");
229 isSubmittable.setValue(false);
231 catch (Exception e) {
233 debug("submittable", "NO (because of an Exception)");
234 isSubmittable.setValue(false);
237 public void removeItem(int pos) {
239 accountCount.setValue(getAccountCount());
241 public void sendCountNotifications() {
242 accountCount.setValue(getAccountCount());
244 enum ItemType {generalData, transactionRow, bottomFiller}
246 class Item extends Object {
247 private ItemType type;
248 private MutableLiveData<Date> date = new MutableLiveData<>();
249 private MutableLiveData<String> description = new MutableLiveData<>();
250 private LedgerTransactionAccount account;
251 private MutableLiveData<String> amountHint = new MutableLiveData<>();
252 private NewTransactionModel model;
253 private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
254 public Item(NewTransactionModel model) {
256 type = ItemType.bottomFiller;
257 editable.setValue(false);
259 public Item(NewTransactionModel model, Date date, String description) {
261 this.type = ItemType.generalData;
262 this.date.setValue(date);
263 this.description.setValue(description);
264 this.editable.setValue(true);
266 public Item(NewTransactionModel model, LedgerTransactionAccount account) {
268 this.type = ItemType.transactionRow;
269 this.account = account;
270 this.editable.setValue(true);
272 public NewTransactionModel getModel() {
275 public boolean isEditable() {
276 ensureType(ItemType.transactionRow);
277 return this.editable.getValue();
279 public void setEditable(boolean editable) {
280 ensureType(ItemType.transactionRow);
281 this.editable.setValue(editable);
283 public String getAmountHint() {
284 ensureType(ItemType.transactionRow);
285 return amountHint.getValue();
287 public void setAmountHint(String amountHint) {
288 ensureType(ItemType.transactionRow);
289 this.amountHint.setValue(amountHint);
291 public void observeAmountHint(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
293 androidx.lifecycle.Observer<? super String> observer) {
294 this.amountHint.observe(owner, observer);
296 public void stopObservingAmountHint(
297 @NonNull androidx.lifecycle.Observer<? super String> observer) {
298 this.amountHint.removeObserver(observer);
300 public ItemType getType() {
303 public void ensureType(ItemType wantedType) {
304 if (type != wantedType) {
305 throw new RuntimeException(
306 String.format("Actual type (%s) differs from wanted (%s)", type,
310 public Date getDate() {
311 ensureType(ItemType.generalData);
312 return date.getValue();
314 public void setDate(Date date) {
315 ensureType(ItemType.generalData);
316 this.date.setValue(date);
318 public void setDate(String text) {
319 int year, month, day;
320 final Calendar c = GregorianCalendar.getInstance();
321 Matcher m = reYMD.matcher(text);
323 year = Integer.parseInt(m.group(1));
324 month = Integer.parseInt(m.group(2)) - 1; // month is 0-based
325 day = Integer.parseInt(m.group(3));
328 year = c.get(Calendar.YEAR);
329 m = reMD.matcher(text);
331 month = Integer.parseInt(m.group(1)) - 1;
332 day = Integer.parseInt(m.group(2));
335 month = c.get(Calendar.MONTH);
336 m = reD.matcher(text);
338 day = Integer.parseInt(m.group(1));
341 day = c.get(Calendar.DAY_OF_MONTH);
346 c.set(year, month, day);
348 this.setDate(c.getTime());
350 public void observeDate(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
351 @NonNull androidx.lifecycle.Observer<? super Date> observer) {
352 this.date.observe(owner, observer);
354 public void stopObservingDate(@NonNull androidx.lifecycle.Observer<? super Date> observer) {
355 this.date.removeObserver(observer);
357 public String getDescription() {
358 ensureType(ItemType.generalData);
359 return description.getValue();
361 public void setDescription(String description) {
362 ensureType(ItemType.generalData);
363 this.description.setValue(description);
365 public void observeDescription(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
367 androidx.lifecycle.Observer<? super String> observer) {
368 this.description.observe(owner, observer);
370 public void stopObservingDescription(
371 @NonNull androidx.lifecycle.Observer<? super String> observer) {
372 this.description.removeObserver(observer);
374 public LedgerTransactionAccount getAccount() {
375 ensureType(ItemType.transactionRow);
378 public void setAccountName(String name) {
379 account.setAccountName(name);
384 * @return nicely formatted, shortest available date representation
386 public String getFormattedDate() {
387 if (date == null) return null;
388 Date time = date.getValue();
389 if (time == null) return null;
391 Calendar c = GregorianCalendar.getInstance();
393 Calendar today = GregorianCalendar.getInstance();
395 final int myYear = c.get(Calendar.YEAR);
396 final int myMonth = c.get(Calendar.MONTH);
397 final int myDay = c.get(Calendar.DAY_OF_MONTH);
399 if (today.get(Calendar.YEAR) != myYear) {
400 return String.format(Locale.US, "%d/%02d/%02d", myYear, myMonth, myDay);
403 if (today.get(Calendar.MONTH) != myMonth) {
404 return String.format(Locale.US, "%d/%02d", myMonth, myDay);
407 return String.valueOf(myDay);
409 public void observeEditableFlag(NewTransactionActivity activity,
410 Observer<Boolean> observer) {
411 editable.observe(activity, observer);
413 public void stopObservingEditableFlag(Observer<Boolean> observer) {
414 editable.removeObserver(observer);