]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionModel.java
44327cbe5b4522bfb874f44782424304b693522f
[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 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() {
53         return items.size();
54     }
55     public Date getDate() {
56         return header.date.getValue();
57     }
58     public String getDescription() {
59         return header.description.getValue();
60     }
61     public LiveData<Boolean> isSubmittable() {
62         return this.isSubmittable;
63     }
64     void reset() {
65         header.date.setValue(null);
66         header.description.setValue(null);
67         items.clear();
68         items.add(new Item(this, new LedgerTransactionAccount("")));
69         items.add(new Item(this, new LedgerTransactionAccount("")));
70         focusedItem.setValue(0);
71     }
72     public void observeFocusedItem(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
73                                    @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
74         this.focusedItem.observe(owner, observer);
75     }
76     public void stopObservingFocusedItem(
77             @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
78         this.focusedItem.removeObserver(observer);
79     }
80     public void observeAccountCount(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
81                                     @NonNull
82                                             androidx.lifecycle.Observer<? super Integer> observer) {
83         this.accountCount.observe(owner, observer);
84     }
85     public void stopObservingAccountCount(
86             @NonNull androidx.lifecycle.Observer<? super Integer> observer) {
87         this.accountCount.removeObserver(observer);
88     }
89     public void setFocusedItem(int position) {
90         focusedItem.setValue(position);
91     }
92     public int addAccount(LedgerTransactionAccount acc) {
93         items.add(new Item(this, acc));
94         accountCount.setValue(getAccountCount());
95         return items.size();
96     }
97     boolean accountsInInitialState() {
98         for (Item item : items) {
99             LedgerTransactionAccount acc = item.getAccount();
100             if (acc.isAmountSet()) return false;
101             if (!acc.getAccountName()
102                     .trim()
103                     .isEmpty()) return false;
104         }
105
106         return true;
107     }
108     LedgerTransactionAccount getAccount(int index) {
109         return items.get(index)
110                     .getAccount();
111     }
112     public Item getItem(int index) {
113         if (index == 0) {
114             return header;
115         }
116
117         if (index <= items.size()) return items.get(index - 1);
118
119         return trailer;
120     }
121     // rules:
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) {
129         int accounts = 0;
130         int accounts_with_values = 0;
131         int amounts = 0;
132         int amounts_with_accounts = 0;
133         int empty_rows = 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());
140
141         try {
142             for (int i = 0; i < this.items.size(); i++) {
143                 Item item = this.items.get(i);
144
145                 LedgerTransactionAccount acc = item.getAccount();
146                 String acc_name = acc.getAccountName()
147                                      .trim();
148                 if (!acc_name.isEmpty()) {
149                     accounts++;
150
151                     if (acc.isAmountSet()) {
152                         accounts_with_values++;
153                     }
154                 }
155                 else empty_rows++;
156
157                 if (!acc.isAmountSet()) {
158                     if (empty_amount == null) {
159                         empty_amount = item;
160                         single_empty_amount = true;
161                         single_empty_amount_has_account = !acc_name.isEmpty();
162                     }
163                     else if (!acc_name.isEmpty()) single_empty_amount = false;
164                 }
165                 else {
166                     amounts++;
167                     if (!acc_name.isEmpty()) amounts_with_accounts++;
168                     running_total += acc.getAmount();
169                 }
170             }
171
172             if ((empty_rows == 0) &&
173                 ((this.items.size() == accounts) || (this.items.size() == amounts)))
174             {
175                 adapter.addRow();
176             }
177
178             if (single_empty_amount) {
179                 empty_amount.setAmountHint(String.format(Locale.US, "%1.2f",
180                         Misc.isZero(running_total) ? 0f : -running_total));
181             }
182
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"));
189
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)))
193             {
194                 debug("submittable", "YES");
195                 isSubmittable.setValue(true);
196             }
197             else {
198                 debug("submittable", "NO");
199                 isSubmittable.setValue(false);
200             }
201
202         }
203         catch (NumberFormatException e) {
204             debug("submittable", "NO (because of NumberFormatException)");
205             isSubmittable.setValue(false);
206         }
207         catch (Exception e) {
208             e.printStackTrace();
209             debug("submittable", "NO (because of an Exception)");
210             isSubmittable.setValue(false);
211         }
212     }
213     public void removeItem(int pos) {
214         items.remove(pos);
215         accountCount.setValue(getAccountCount());
216     }
217     public void sendCountNotifications() {
218         accountCount.setValue(getAccountCount());
219     }
220     enum ItemType {generalData, transactionRow, bottomFiller}
221
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) {
231             this.model = model;
232             type = ItemType.bottomFiller;
233             editable.setValue(false);
234         }
235         public Item(NewTransactionModel model, Date date, String description) {
236             this.model = model;
237             this.type = ItemType.generalData;
238             this.date.setValue(date);
239             this.description.setValue(description);
240             this.editable.setValue(true);
241         }
242         public Item(NewTransactionModel model, LedgerTransactionAccount account) {
243             this.model = model;
244             this.type = ItemType.transactionRow;
245             this.account = account;
246             this.editable.setValue(true);
247         }
248         public NewTransactionModel getModel() {
249             return model;
250         }
251         public boolean isEditable() {
252             ensureType(ItemType.transactionRow);
253             return this.editable.getValue();
254         }
255         public void setEditable(boolean editable) {
256             ensureType(ItemType.transactionRow);
257             this.editable.setValue(editable);
258         }
259         public String getAmountHint() {
260             ensureType(ItemType.transactionRow);
261             return amountHint.getValue();
262         }
263         public void setAmountHint(String amountHint) {
264             ensureType(ItemType.transactionRow);
265             this.amountHint.setValue(amountHint);
266         }
267         public void observeAmountHint(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
268                                       @NonNull
269                                               androidx.lifecycle.Observer<? super String> observer) {
270             this.amountHint.observe(owner, observer);
271         }
272         public void stopObservingAmountHint(
273                 @NonNull androidx.lifecycle.Observer<? super String> observer) {
274             this.amountHint.removeObserver(observer);
275         }
276         public ItemType getType() {
277             return type;
278         }
279         public void ensureType(ItemType wantedType) {
280             if (type != wantedType) {
281                 throw new RuntimeException(
282                         String.format("Actual type (%d) differs from wanted (%s)", type,
283                                 wantedType));
284             }
285         }
286         public Date getDate() {
287             ensureType(ItemType.generalData);
288             return date.getValue();
289         }
290         public void setDate(Date date) {
291             ensureType(ItemType.generalData);
292             this.date.setValue(date);
293         }
294         public void setDate(String text) {
295             int year, month, day;
296             final Calendar c = GregorianCalendar.getInstance();
297             Matcher m = reYMD.matcher(text);
298             if (m.matches()) {
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));
302             }
303             else {
304                 year = c.get(Calendar.YEAR);
305                 m = reMD.matcher(text);
306                 if (m.matches()) {
307                     month = Integer.parseInt(m.group(1)) - 1;
308                     day = Integer.parseInt(m.group(2));
309                 }
310                 else {
311                     month = c.get(Calendar.MONTH);
312                     m = reD.matcher(text);
313                     if (m.matches()) {
314                         day = Integer.parseInt(m.group(1));
315                     }
316                     else {
317                         day = c.get(Calendar.DAY_OF_MONTH);
318                     }
319                 }
320             }
321
322             c.set(year, month, day);
323
324             this.setDate(c.getTime());
325         }
326         public void observeDate(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
327                                 @NonNull androidx.lifecycle.Observer<? super Date> observer) {
328             this.date.observe(owner, observer);
329         }
330         public void stopObservingDate(@NonNull androidx.lifecycle.Observer<? super Date> observer) {
331             this.date.removeObserver(observer);
332         }
333         public String getDescription() {
334             ensureType(ItemType.generalData);
335             return description.getValue();
336         }
337         public void setDescription(String description) {
338             ensureType(ItemType.generalData);
339             this.description.setValue(description);
340         }
341         public void observeDescription(@NonNull @NotNull androidx.lifecycle.LifecycleOwner owner,
342                                        @NonNull
343                                                androidx.lifecycle.Observer<? super String> observer) {
344             this.description.observe(owner, observer);
345         }
346         public void stopObservingDescription(
347                 @NonNull androidx.lifecycle.Observer<? super String> observer) {
348             this.description.removeObserver(observer);
349         }
350         public LedgerTransactionAccount getAccount() {
351             ensureType(ItemType.transactionRow);
352             return account;
353         }
354         public void setAccountName(String name) {
355             account.setAccountName(name);
356         }
357         /**
358          * getFormattedDate()
359          *
360          * @return nicely formatted, shortest available date representation
361          */
362         public String getFormattedDate() {
363             if (date == null) return null;
364             Date time = date.getValue();
365             if (time == null) return null;
366
367             Calendar c = GregorianCalendar.getInstance();
368             c.setTime(time);
369             Calendar today = GregorianCalendar.getInstance();
370
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);
374
375             if (today.get(Calendar.YEAR) != myYear) {
376                 return String.format(Locale.US, "%d/%02d/%02d", myYear, myMonth, myDay);
377             }
378
379             if (today.get(Calendar.MONTH) != myMonth) {
380                 return String.format(Locale.US, "%d/%02d", myMonth, myDay);
381             }
382
383             return String.valueOf(myDay);
384         }
385         public void observeEditableFlag(NewTransactionActivity activity,
386                                         Observer<Boolean> observer) {
387             editable.observe(activity, observer);
388         }
389         public void stopObservingEditableFlag(Observer<Boolean> observer) {
390             editable.removeObserver(observer);
391         }
392     }
393 }