]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
New transaction: rework empty amounts hints, broken after RecyclerView migration
[mobile-ledger.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionModel.java
1 /*
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.
8  *
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.
13  *
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/>.
16  */
17
18 package net.ktnx.mobileledger.ui.activity;
19
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;
25
26 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
27 import net.ktnx.mobileledger.utils.Misc;
28
29 import org.jetbrains.annotations.NotNull;
30
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;
38
39 import static net.ktnx.mobileledger.utils.Logger.debug;
40 import static net.ktnx.mobileledger.utils.Misc.isZero;
41
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() {
54         return items.size();
55     }
56     public Date getDate() {
57         return header.date.getValue();
58     }
59     public String getDescription() {
60         return header.description.getValue();
61     }
62     public LiveData<Boolean> isSubmittable() {
63         return this.isSubmittable;
64     }
65     void reset() {
66         header.date.setValue(null);
67         header.description.setValue(null);
68         items.clear();
69         items.add(new Item(this, new LedgerTransactionAccount("")));
70         items.add(new Item(this, new LedgerTransactionAccount("")));
71         focusedItem.setValue(0);
72     }
73     public void observeFocusedItem(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
74                                    @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
75         this.focusedItem.observe(owner, observer);
76     }
77     public void stopObservingFocusedItem(
78             @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
79         this.focusedItem.removeObserver(observer);
80     }
81     public void observeAccountCount(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
82                                     @NonNull
83                                             androidx.lifecycle.Observer<? super Integer> observer) {
84         this.accountCount.observe(owner, observer);
85     }
86     public void stopObservingAccountCount(
87             @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
88         this.accountCount.removeObserver(observer);
89     }
90     public void setFocusedItem(int position) {
91         focusedItem.setValue(position);
92     }
93     public int addAccount(LedgerTransactionAccount acc) {
94         items.add(new Item(this, acc));
95         accountCount.setValue(getAccountCount());
96         return items.size();
97     }
98     boolean accountsInInitialState() {
99         for (Item item : items) {
100             LedgerTransactionAccount acc = item.getAccount();
101             if (acc.isAmountSet()) return false;
102             if (!acc.getAccountName()
103                     .trim()
104                     .isEmpty()) return false;
105         }
106
107         return true;
108     }
109     LedgerTransactionAccount getAccount(int index) {
110         return items.get(index)
111                     .getAccount();
112     }
113     public Item getItem(int index) {
114         if (index == 0) {
115             return header;
116         }
117
118         if (index <= items.size()) return items.get(index - 1);
119
120         return trailer;
121     }
122     // rules:
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) {
130         int accounts = 0;
131         int accounts_with_values = 0;
132         int amounts = 0;
133         int amounts_with_accounts = 0;
134         int empty_rows = 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());
141
142         try {
143             for (int i = 0; i < this.items.size(); i++) {
144                 Item item = this.items.get(i);
145
146                 LedgerTransactionAccount acc = item.getAccount();
147                 String acc_name = acc.getAccountName()
148                                      .trim();
149                 if (acc_name.isEmpty()) {
150                     empty_rows++;
151                 }
152                 else {
153                     accounts++;
154
155                     if (acc.isAmountSet()) {
156                         accounts_with_values++;
157                     }
158                 }
159
160                 if (acc.isAmountSet()) {
161                     amounts++;
162                     if (!acc_name.isEmpty())
163                         amounts_with_accounts++;
164                     running_total += acc.getAmount();
165                 }
166                 else {
167                     if (empty_amount == null) {
168                         empty_amount = item;
169                         single_empty_amount = true;
170                         single_empty_amount_has_account = !acc_name.isEmpty();
171                     }
172                     else if (!acc_name.isEmpty())
173                         single_empty_amount = false;
174                 }
175             }
176
177             if ((empty_rows == 0) &&
178                 ((this.items.size() == accounts) || (this.items.size() == amounts)))
179             {
180                 adapter.addRow();
181             }
182
183             for (NewTransactionModel.Item item : items) {
184
185                 final LedgerTransactionAccount acc = item.getAccount();
186                 if (acc.isAmountSet())
187                     continue;
188
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));
193                         continue;
194                     }
195                 }
196                 else {
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);
202                     }
203                 }
204
205             }
206
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"));
213
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)))
217             {
218                 debug("submittable", "YES");
219                 isSubmittable.setValue(true);
220             }
221             else {
222                 debug("submittable", "NO");
223                 isSubmittable.setValue(false);
224             }
225
226         }
227         catch (NumberFormatException e) {
228             debug("submittable", "NO (because of NumberFormatException)");
229             isSubmittable.setValue(false);
230         }
231         catch (Exception e) {
232             e.printStackTrace();
233             debug("submittable", "NO (because of an Exception)");
234             isSubmittable.setValue(false);
235         }
236     }
237     public void removeItem(int pos) {
238         items.remove(pos);
239         accountCount.setValue(getAccountCount());
240     }
241     public void sendCountNotifications() {
242         accountCount.setValue(getAccountCount());
243     }
244     enum ItemType {generalData, transactionRow, bottomFiller}
245
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) {
255             this.model = model;
256             type = ItemType.bottomFiller;
257             editable.setValue(false);
258         }
259         public Item(NewTransactionModel model, Date date, String description) {
260             this.model = model;
261             this.type = ItemType.generalData;
262             this.date.setValue(date);
263             this.description.setValue(description);
264             this.editable.setValue(true);
265         }
266         public Item(NewTransactionModel model, LedgerTransactionAccount account) {
267             this.model = model;
268             this.type = ItemType.transactionRow;
269             this.account = account;
270             this.editable.setValue(true);
271         }
272         public NewTransactionModel getModel() {
273             return model;
274         }
275         public boolean isEditable() {
276             ensureType(ItemType.transactionRow);
277             return this.editable.getValue();
278         }
279         public void setEditable(boolean editable) {
280             ensureType(ItemType.transactionRow);
281             this.editable.setValue(editable);
282         }
283         public String getAmountHint() {
284             ensureType(ItemType.transactionRow);
285             return amountHint.getValue();
286         }
287         public void setAmountHint(String amountHint) {
288             ensureType(ItemType.transactionRow);
289             this.amountHint.setValue(amountHint);
290         }
291         public void observeAmountHint(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
292                                       @NonNull
293                                               androidx.lifecycle.Observer<? super String> observer) {
294             this.amountHint.observe(owner, observer);
295         }
296         public void stopObservingAmountHint(
297                 @NonNull androidx.lifecycle.Observer<? super String> observer) {
298             this.amountHint.removeObserver(observer);
299         }
300         public ItemType getType() {
301             return type;
302         }
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,
307                                 wantedType));
308             }
309         }
310         public Date getDate() {
311             ensureType(ItemType.generalData);
312             return date.getValue();
313         }
314         public void setDate(Date date) {
315             ensureType(ItemType.generalData);
316             this.date.setValue(date);
317         }
318         public void setDate(String text) {
319             int year, month, day;
320             final Calendar c = GregorianCalendar.getInstance();
321             Matcher m = reYMD.matcher(text);
322             if (m.matches()) {
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));
326             }
327             else {
328                 year = c.get(Calendar.YEAR);
329                 m = reMD.matcher(text);
330                 if (m.matches()) {
331                     month = Integer.parseInt(m.group(1)) - 1;
332                     day = Integer.parseInt(m.group(2));
333                 }
334                 else {
335                     month = c.get(Calendar.MONTH);
336                     m = reD.matcher(text);
337                     if (m.matches()) {
338                         day = Integer.parseInt(m.group(1));
339                     }
340                     else {
341                         day = c.get(Calendar.DAY_OF_MONTH);
342                     }
343                 }
344             }
345
346             c.set(year, month, day);
347
348             this.setDate(c.getTime());
349         }
350         public void observeDate(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
351                                 @NonNull androidx.lifecycle.Observer<? super Date> observer) {
352             this.date.observe(owner, observer);
353         }
354         public void stopObservingDate(@NonNull androidx.lifecycle.Observer<? super Date> observer) {
355             this.date.removeObserver(observer);
356         }
357         public String getDescription() {
358             ensureType(ItemType.generalData);
359             return description.getValue();
360         }
361         public void setDescription(String description) {
362             ensureType(ItemType.generalData);
363             this.description.setValue(description);
364         }
365         public void observeDescription(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
366                                        @NonNull
367                                                androidx.lifecycle.Observer<? super String> observer) {
368             this.description.observe(owner, observer);
369         }
370         public void stopObservingDescription(
371                 @NonNull androidx.lifecycle.Observer<? super String> observer) {
372             this.description.removeObserver(observer);
373         }
374         public LedgerTransactionAccount getAccount() {
375             ensureType(ItemType.transactionRow);
376             return account;
377         }
378         public void setAccountName(String name) {
379             account.setAccountName(name);
380         }
381         /**
382          * getFormattedDate()
383          *
384          * @return nicely formatted, shortest available date representation
385          */
386         public String getFormattedDate() {
387             if (date == null) return null;
388             Date time = date.getValue();
389             if (time == null) return null;
390
391             Calendar c = GregorianCalendar.getInstance();
392             c.setTime(time);
393             Calendar today = GregorianCalendar.getInstance();
394
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);
398
399             if (today.get(Calendar.YEAR) != myYear) {
400                 return String.format(Locale.US, "%d/%02d/%02d", myYear, myMonth, myDay);
401             }
402
403             if (today.get(Calendar.MONTH) != myMonth) {
404                 return String.format(Locale.US, "%d/%02d", myMonth, myDay);
405             }
406
407             return String.valueOf(myDay);
408         }
409         public void observeEditableFlag(NewTransactionActivity activity,
410                                         Observer<Boolean> observer) {
411             editable.observe(activity, observer);
412         }
413         public void stopObservingEditableFlag(Observer<Boolean> observer) {
414             editable.removeObserver(observer);
415         }
416     }
417 }