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.ViewModel;
25 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
27 import org.jetbrains.annotations.NotNull;
29 import java.util.ArrayList;
30 import java.util.Calendar;
31 import java.util.Date;
32 import java.util.GregorianCalendar;
33 import java.util.Locale;
34 import java.util.regex.Matcher;
35 import java.util.regex.Pattern;
37 import static net.ktnx.mobileledger.utils.Logger.debug;
38 import static net.ktnx.mobileledger.utils.Misc.isZero;
40 public class NewTransactionModel extends ViewModel {
41 static final Pattern reYMD = Pattern.compile("^\\s*(\\d+)\\d*/\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
42 static final Pattern reMD = Pattern.compile("^\\s*(\\d+)\\s*/\\s*(\\d+)\\s*$");
43 static final Pattern reD = Pattern.compile("\\s*(\\d+)\\s*$");
44 private final Item header = new Item(this, null, "");
45 private final Item trailer = new Item(this);
46 private final ArrayList<Item> items = new ArrayList<>();
47 private final MutableLiveData<Boolean> isSubmittable = new MutableLiveData<>(false);
48 private final MutableLiveData<Integer> focusedItem = new MutableLiveData<>(null);
49 private final MutableLiveData<Integer> accountCount = new MutableLiveData<>(0);
50 public int getAccountCount() {
53 public Date getDate() {
54 return header.date.getValue();
56 public String getDescription() {
57 return header.description.getValue();
59 public LiveData<Boolean> isSubmittable() {
60 return this.isSubmittable;
63 header.date.setValue(null);
64 header.description.setValue(null);
66 items.add(new Item(this, new LedgerTransactionAccount("")));
67 items.add(new Item(this, new LedgerTransactionAccount("")));
68 focusedItem.setValue(0);
70 public void observeFocusedItem(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
71 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
72 this.focusedItem.observe(owner, observer);
74 public void stopObservingFocusedItem(
75 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
76 this.focusedItem.removeObserver(observer);
78 public void observeAccountCount(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
80 androidx.lifecycle.Observer<? super Integer> observer) {
81 this.accountCount.observe(owner, observer);
83 public void stopObservingAccountCount(
84 @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
85 this.accountCount.removeObserver(observer);
87 public void setFocusedItem(int position) {
88 focusedItem.setValue(position);
90 public int addAccount(LedgerTransactionAccount acc) {
91 items.add(new Item(this, acc));
92 accountCount.setValue(getAccountCount());
95 boolean accountsInInitialState() {
96 for (Item item : items) {
97 LedgerTransactionAccount acc = item.getAccount();
98 if (acc.isAmountSet()) return false;
99 if (!acc.getAccountName()
101 .isEmpty()) return false;
106 LedgerTransactionAccount getAccount(int index) {
107 return items.get(index)
110 public Item getItem(int index) {
114 else if (index <= items.size()) return items.get(index - 1);
118 // 1) at least two account names
119 // 2) each amount must have account name
120 // 3) amounts must balance to 0, or
121 // 3a) there must be exactly one empty amount
122 // 4) empty accounts with empty amounts are ignored
123 // 5) a row with an empty account name or empty amount is guaranteed to exist
124 public void checkTransactionSubmittable(NewTransactionItemsAdapter adapter) {
126 int accounts_with_values = 0;
128 int amounts_with_accounts = 0;
130 Item empty_amount = null;
131 boolean single_empty_amount = false;
132 boolean single_empty_amount_has_account = false;
133 float running_total = 0f;
134 final String descriptionText = getDescription();
135 final boolean have_description = ((descriptionText != null) && !descriptionText.isEmpty());
138 for (int i = 0; i < this.items.size(); i++) {
139 Item item = this.items.get(i);
141 LedgerTransactionAccount acc = item.getAccount();
142 String acc_name = acc.getAccountName()
144 if (!acc_name.isEmpty()) {
147 if (acc.isAmountSet()) {
148 accounts_with_values++;
153 if (!acc.isAmountSet()) {
154 if (empty_amount == null) {
156 single_empty_amount = true;
157 single_empty_amount_has_account = !acc_name.isEmpty();
159 else if (!acc_name.isEmpty()) single_empty_amount = false;
163 if (!acc_name.isEmpty()) amounts_with_accounts++;
164 running_total += acc.getAmount();
168 if ((empty_rows == 0) &&
169 ((this.items.size() == accounts) || (this.items.size() == amounts)))
174 if (single_empty_amount) {
175 empty_amount.setAmountHint(String.format(Locale.US, "%1.2f",
176 (Math.abs(running_total) > 0.005) ? -running_total : 0f));
179 debug("submittable", String.format(Locale.US,
180 "%s, accounts=%d, accounts_with_values=%s, " +
181 "amounts_with_accounts=%d, amounts=%d, running_total=%1.2f, " +
182 "single_empty_with_acc=%s", have_description ? "description" : "NO description",
183 accounts, accounts_with_values, amounts_with_accounts, amounts, running_total,
184 (single_empty_amount && single_empty_amount_has_account) ? "true" : "false"));
186 if (have_description && (accounts >= 2) && (accounts_with_values >= (accounts - 1)) &&
187 (amounts_with_accounts == amounts) &&
188 (single_empty_amount && single_empty_amount_has_account || isZero(running_total)))
190 debug("submittable", "YES");
191 isSubmittable.setValue(true);
194 debug("submittable", "NO");
195 isSubmittable.setValue(false);
199 catch (NumberFormatException e) {
200 debug("submittable", "NO (because of NumberFormatException)");
201 isSubmittable.setValue(false);
203 catch (Exception e) {
205 debug("submittable", "NO (because of an Exception)");
206 isSubmittable.setValue(false);
209 public void removeItem(int pos, NewTransactionItemsAdapter adapter) {
211 accountCount.setValue(getAccountCount());
212 checkTransactionSubmittable(adapter);
214 enum ItemType {generalData, transactionRow, bottomFiller}
216 class Item extends Object {
217 private ItemType type;
218 private MutableLiveData<Date> date = new MutableLiveData<>();
219 private MutableLiveData<String> description = new MutableLiveData<>();
220 private LedgerTransactionAccount account;
221 private MutableLiveData<String> amountHint = new MutableLiveData<>();
222 private NewTransactionModel model;
223 private boolean editable = true;
224 public Item(NewTransactionModel model) {
226 type = ItemType.bottomFiller;
228 public Item(NewTransactionModel model, Date date, String description) {
230 this.type = ItemType.generalData;
231 this.date.setValue(date);
232 this.description.setValue(description);
234 public Item(NewTransactionModel model, LedgerTransactionAccount account) {
236 this.type = ItemType.transactionRow;
237 this.account = account;
239 public NewTransactionModel getModel() {
242 public boolean isEditable() {
243 ensureType(ItemType.transactionRow);
246 public void setEditable(boolean editable) {
247 ensureType(ItemType.transactionRow);
248 this.editable = editable;
250 public String getAmountHint() {
251 ensureType(ItemType.transactionRow);
252 return amountHint.getValue();
254 public void setAmountHint(String amountHint) {
255 ensureType(ItemType.transactionRow);
256 this.amountHint.setValue(amountHint);
258 public void observeAmountHint(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
260 androidx.lifecycle.Observer<? super String> observer) {
261 this.amountHint.observe(owner, observer);
263 public void stopObservingAmountHint(
264 @NonNull androidx.lifecycle.Observer<? super String> observer) {
265 this.amountHint.removeObserver(observer);
267 public ItemType getType() {
270 public void ensureType(ItemType wantedType) {
271 if (type != wantedType) {
272 throw new RuntimeException(
273 String.format("Actual type (%d) differs from wanted (%s)", type,
277 public Date getDate() {
278 ensureType(ItemType.generalData);
279 return date.getValue();
281 public void setDate(Date date) {
282 ensureType(ItemType.generalData);
283 this.date.setValue(date);
285 public void setDate(String text) {
286 int year, month, day;
287 final Calendar c = GregorianCalendar.getInstance();
288 Matcher m = reYMD.matcher(text);
290 year = Integer.parseInt(m.group(1));
291 month = Integer.parseInt(m.group(2)) - 1; // month is 0-based
292 day = Integer.parseInt(m.group(3));
295 year = c.get(Calendar.YEAR);
296 m = reMD.matcher(text);
298 month = Integer.parseInt(m.group(1)) - 1;
299 day = Integer.parseInt(m.group(2));
302 month = c.get(Calendar.MONTH);
303 m = reD.matcher(text);
305 day = Integer.parseInt(m.group(1));
308 day = c.get(Calendar.DAY_OF_MONTH);
313 c.set(year, month, day);
315 this.setDate(c.getTime());
317 public void observeDate(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
318 @NonNull androidx.lifecycle.Observer<? super Date> observer) {
319 this.date.observe(owner, observer);
321 public void stopObservingDate(@NonNull androidx.lifecycle.Observer<? super Date> observer) {
322 this.date.removeObserver(observer);
324 public String getDescription() {
325 ensureType(ItemType.generalData);
326 return description.getValue();
328 public void setDescription(String description) {
329 ensureType(ItemType.generalData);
330 this.description.setValue(description);
332 public void observeDescription(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
334 androidx.lifecycle.Observer<? super String> observer) {
335 this.description.observe(owner, observer);
337 public void stopObservingDescription(
338 @NonNull androidx.lifecycle.Observer<? super String> observer) {
339 this.description.removeObserver(observer);
341 public LedgerTransactionAccount getAccount() {
342 ensureType(ItemType.transactionRow);
345 public void setAccountName(String name) {
346 account.setAccountName(name);
351 * @return nicely formatted, shortest available date representation
353 public String getFormattedDate() {
354 if (date == null) return null;
355 Date time = date.getValue();
356 if (time == null) return null;
358 Calendar c = GregorianCalendar.getInstance();
360 Calendar today = GregorianCalendar.getInstance();
362 final int myYear = c.get(Calendar.YEAR);
363 final int myMonth = c.get(Calendar.MONTH);
364 final int myDay = c.get(Calendar.DAY_OF_MONTH);
366 if (today.get(Calendar.YEAR) != myYear) {
367 return String.format(Locale.US, "%d/%02d/%02d", myYear, myMonth, myDay);
370 if (today.get(Calendar.MONTH) != myMonth) {
371 return String.format(Locale.US, "%d/%02d", myMonth, myDay);
374 return String.valueOf(myDay);