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 final Item header = new Item(this, null, "");
47 private final Item trailer = new Item(this);
48 private final ArrayList<Item> items = new ArrayList<>();
49 private final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
50 private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(null);
51 private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
52 public int getAccountCount() {
55 public Date getDate() {
56 return header.date.getValue();
58 public String getDescription() {
59 return header.description.getValue();
61 public LiveData<Boolean> isSubmittable() {
62 return this.isSubmittable;
65 header.date.setValue(null);
66 header.description.setValue(null);
68 items.add(new Item(this, new LedgerTransactionAccount("")));
69 items.add(new Item(this, new LedgerTransactionAccount("")));
70 focusedItem.setValue(0);
72 public void observeFocusedItem(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
73 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
74 this.focusedItem.observe(owner, observer);
76 public void stopObservingFocusedItem(
77 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
78 this.focusedItem.removeObserver(observer);
80 public void observeAccountCount(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
82 androidx.lifecycle.Observer<? super Integer> observer) {
83 this.accountCount.observe(owner, observer);
85 public void stopObservingAccountCount(
86 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
87 this.accountCount.removeObserver(observer);
89 public void setFocusedItem(int position) {
90 focusedItem.setValue(position);
92 public int addAccount(LedgerTransactionAccount acc) {
93 items.add(new Item(this, acc));
94 accountCount.setValue(getAccountCount());
97 boolean accountsInInitialState() {
98 for (Item item : items) {
99 LedgerTransactionAccount acc = item.getAccount();
100 if (acc.isAmountSet()) return false;
101 if (!acc.getAccountName()
103 .isEmpty()) return false;
108 LedgerTransactionAccount getAccount(int index) {
109 return items.get(index)
112 public Item getItem(int index) {
117 if (index <= items.size()) return items.get(index - 1);
122 // 1) at least two account names
123 // 2) each amount must have account name
124 // 3) amounts must balance to 0, or
125 // 3a) there must be exactly one empty amount
126 // 4) empty accounts with empty amounts are ignored
127 // 5) a row with an empty account name or empty amount is guaranteed to exist
128 public void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) {
130 int accounts_with_values = 0;
132 int amounts_with_accounts = 0;
134 Item empty_amount = null;
135 boolean single_empty_amount = false;
136 boolean single_empty_amount_has_account = false;
137 float running_total = 0f;
138 final String descriptionText = getDescription();
139 final boolean have_description = ((descriptionText != null) && !descriptionText.isEmpty());
142 for (int i = 0; i < this.items.size(); i++) {
143 Item item = this.items.get(i);
145 LedgerTransactionAccount acc = item.getAccount();
146 String acc_name = acc.getAccountName()
148 if (!acc_name.isEmpty()) {
151 if (acc.isAmountSet()) {
152 accounts_with_values++;
157 if (!acc.isAmountSet()) {
158 if (empty_amount == null) {
160 single_empty_amount = true;
161 single_empty_amount_has_account = !acc_name.isEmpty();
163 else if (!acc_name.isEmpty()) single_empty_amount = false;
167 if (!acc_name.isEmpty()) amounts_with_accounts++;
168 running_total += acc.getAmount();
172 if ((empty_rows == 0) &&
173 ((this.items.size() == accounts) || (this.items.size() == amounts)))
178 if (single_empty_amount) {
179 empty_amount.setAmountHint(String.format(Locale.US, "%1.2f",
180 Misc.isZero(running_total) ? 0f : -running_total));
183 debug("submittable", String.format(Locale.US,
184 "%s, accounts=%d, accounts_with_values=%s, " +
185 "amounts_with_accounts=%d, amounts=%d, running_total=%1.2f, " +
186 "single_empty_with_acc=%s", have_description ? "description" : "NO description",
187 accounts, accounts_with_values, amounts_with_accounts, amounts, running_total,
188 (single_empty_amount && single_empty_amount_has_account) ? "true" : "false"));
190 if (have_description && (accounts >= 2) && (accounts_with_values >= (accounts - 1)) &&
191 (amounts_with_accounts == amounts) &&
192 (single_empty_amount && single_empty_amount_has_account || isZero(running_total)))
194 debug("submittable", "YES");
195 isSubmittable.setValue(true);
198 debug("submittable", "NO");
199 isSubmittable.setValue(false);
203 catch (NumberFormatException e) {
204 debug("submittable", "NO (because of NumberFormatException)");
205 isSubmittable.setValue(false);
207 catch (Exception e) {
209 debug("submittable", "NO (because of an Exception)");
210 isSubmittable.setValue(false);
213 public void removeItem(int pos) {
215 accountCount.setValue(getAccountCount());
217 public void sendCountNotifications() {
218 accountCount.setValue(getAccountCount());
220 enum ItemType {generalData, transactionRow, bottomFiller}
222 class Item extends Object {
223 private ItemType type;
224 private MutableLiveData<Date> date = new MutableLiveData<>();
225 private MutableLiveData<String> description = new MutableLiveData<>();
226 private LedgerTransactionAccount account;
227 private MutableLiveData<String> amountHint = new MutableLiveData<>();
228 private NewTransactionModel model;
229 private MutableLiveData<Boolean> editable = new MutableLiveData<>(true);
230 public Item(NewTransactionModel model) {
232 type = ItemType.bottomFiller;
233 editable.setValue(false);
235 public Item(NewTransactionModel model, Date date, String description) {
237 this.type = ItemType.generalData;
238 this.date.setValue(date);
239 this.description.setValue(description);
240 this.editable.setValue(true);
242 public Item(NewTransactionModel model, LedgerTransactionAccount account) {
244 this.type = ItemType.transactionRow;
245 this.account = account;
246 this.editable.setValue(true);
248 public NewTransactionModel getModel() {
251 public boolean isEditable() {
252 ensureType(ItemType.transactionRow);
253 return this.editable.getValue();
255 public void setEditable(boolean editable) {
256 ensureType(ItemType.transactionRow);
257 this.editable.setValue(editable);
259 public String getAmountHint() {
260 ensureType(ItemType.transactionRow);
261 return amountHint.getValue();
263 public void setAmountHint(String amountHint) {
264 ensureType(ItemType.transactionRow);
265 this.amountHint.setValue(amountHint);
267 public void observeAmountHint(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
269 androidx.lifecycle.Observer<? super String> observer) {
270 this.amountHint.observe(owner, observer);
272 public void stopObservingAmountHint(
273 @NonNull androidx.lifecycle.Observer<? super String> observer) {
274 this.amountHint.removeObserver(observer);
276 public ItemType getType() {
279 public void ensureType(ItemType wantedType) {
280 if (type != wantedType) {
281 throw new RuntimeException(
282 String.format("Actual type (%s) differs from wanted (%s)", type,
286 public Date getDate() {
287 ensureType(ItemType.generalData);
288 return date.getValue();
290 public void setDate(Date date) {
291 ensureType(ItemType.generalData);
292 this.date.setValue(date);
294 public void setDate(String text) {
295 int year, month, day;
296 final Calendar c = GregorianCalendar.getInstance();
297 Matcher m = reYMD.matcher(text);
299 year = Integer.parseInt(m.group(1));
300 month = Integer.parseInt(m.group(2)) - 1; // month is 0-based
301 day = Integer.parseInt(m.group(3));
304 year = c.get(Calendar.YEAR);
305 m = reMD.matcher(text);
307 month = Integer.parseInt(m.group(1)) - 1;
308 day = Integer.parseInt(m.group(2));
311 month = c.get(Calendar.MONTH);
312 m = reD.matcher(text);
314 day = Integer.parseInt(m.group(1));
317 day = c.get(Calendar.DAY_OF_MONTH);
322 c.set(year, month, day);
324 this.setDate(c.getTime());
326 public void observeDate(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
327 @NonNull androidx.lifecycle.Observer<? super Date> observer) {
328 this.date.observe(owner, observer);
330 public void stopObservingDate(@NonNull androidx.lifecycle.Observer<? super Date> observer) {
331 this.date.removeObserver(observer);
333 public String getDescription() {
334 ensureType(ItemType.generalData);
335 return description.getValue();
337 public void setDescription(String description) {
338 ensureType(ItemType.generalData);
339 this.description.setValue(description);
341 public void observeDescription(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
343 androidx.lifecycle.Observer<? super String> observer) {
344 this.description.observe(owner, observer);
346 public void stopObservingDescription(
347 @NonNull androidx.lifecycle.Observer<? super String> observer) {
348 this.description.removeObserver(observer);
350 public LedgerTransactionAccount getAccount() {
351 ensureType(ItemType.transactionRow);
354 public void setAccountName(String name) {
355 account.setAccountName(name);
360 * @return nicely formatted, shortest available date representation
362 public String getFormattedDate() {
363 if (date == null) return null;
364 Date time = date.getValue();
365 if (time == null) return null;
367 Calendar c = GregorianCalendar.getInstance();
369 Calendar today = GregorianCalendar.getInstance();
371 final int myYear = c.get(Calendar.YEAR);
372 final int myMonth = c.get(Calendar.MONTH);
373 final int myDay = c.get(Calendar.DAY_OF_MONTH);
375 if (today.get(Calendar.YEAR) != myYear) {
376 return String.format(Locale.US, "%d/%02d/%02d", myYear, myMonth, myDay);
379 if (today.get(Calendar.MONTH) != myMonth) {
380 return String.format(Locale.US, "%d/%02d", myMonth, myDay);
383 return String.valueOf(myDay);
385 public void observeEditableFlag(NewTransactionActivity activity,
386 Observer<Boolean> observer) {
387 editable.observe(activity, observer);
389 public void stopObservingEditableFlag(Observer<Boolean> observer) {
390 editable.removeObserver(observer);