]> git.ktnx.net Git - mobile-ledger-staging.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
fc17560614e9d85e8e72a30ce8d9cc59c678d861
[mobile-ledger-staging.git] / app / src / main / java / net / ktnx / mobileledger / ui / activity / NewTransactionItemHolder.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 android.annotation.SuppressLint;
21 import android.os.Build;
22 import android.text.Editable;
23 import android.text.TextWatcher;
24 import android.text.method.DigitsKeyListener;
25 import android.view.View;
26 import android.view.inputmethod.EditorInfo;
27 import android.widget.AutoCompleteTextView;
28 import android.widget.FrameLayout;
29 import android.widget.LinearLayout;
30 import android.widget.TextView;
31
32 import androidx.annotation.NonNull;
33 import androidx.lifecycle.Observer;
34 import androidx.recyclerview.widget.RecyclerView;
35
36 import net.ktnx.mobileledger.R;
37 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
38 import net.ktnx.mobileledger.model.Data;
39 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
40 import net.ktnx.mobileledger.model.MobileLedgerProfile;
41 import net.ktnx.mobileledger.ui.DatePickerFragment;
42 import net.ktnx.mobileledger.utils.Logger;
43 import net.ktnx.mobileledger.utils.MLDB;
44 import net.ktnx.mobileledger.utils.Misc;
45
46 import java.text.DecimalFormatSymbols;
47 import java.util.Calendar;
48 import java.util.Date;
49 import java.util.GregorianCalendar;
50 import java.util.Locale;
51
52 class NewTransactionItemHolder extends RecyclerView.ViewHolder
53         implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
54     private final String decimalSeparator;
55     private final String decimalDot;
56     private NewTransactionModel.Item item;
57     private TextView tvDate;
58     private AutoCompleteTextView tvDescription;
59     private AutoCompleteTextView tvAccount;
60     private TextView tvAmount;
61     private LinearLayout lHead;
62     private LinearLayout lAccount;
63     private FrameLayout lPadding;
64     private MobileLedgerProfile mProfile;
65     private Date date;
66     private Observer<Date> dateObserver;
67     private Observer<String> descriptionObserver;
68     private Observer<String> hintObserver;
69     private Observer<Integer> focusedAccountObserver;
70     private Observer<Integer> accountCountObserver;
71     private Observer<Boolean> editableObserver;
72     private boolean inUpdate = false;
73     private boolean syncingData = false;
74     NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
75         super(itemView);
76         tvAccount = itemView.findViewById(R.id.account_row_acc_name);
77         tvAmount = itemView.findViewById(R.id.account_row_acc_amounts);
78         tvDate = itemView.findViewById(R.id.new_transaction_date);
79         tvDescription = itemView.findViewById(R.id.new_transaction_description);
80         lHead = itemView.findViewById(R.id.ntr_data);
81         lAccount = itemView.findViewById(R.id.ntr_account);
82         lPadding = itemView.findViewById(R.id.ntr_padding);
83
84         tvDescription.setNextFocusForwardId(View.NO_ID);
85         tvAccount.setNextFocusForwardId(View.NO_ID);
86         tvAmount.setNextFocusForwardId(View.NO_ID); // magic!
87
88         tvDate.setOnClickListener(v -> pickTransactionDate());
89
90         mProfile = Data.profile.getValue();
91         if (mProfile == null)
92             throw new AssertionError();
93
94         View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
95             if (hasFocus) {
96                 boolean wasSyncing = syncingData;
97                 syncingData = true;
98                 try {
99                     adapter.updateFocusedItem(getAdapterPosition());
100                 }
101                 finally {
102                     syncingData = wasSyncing;
103                 }
104             }
105         };
106
107         tvDescription.setOnFocusChangeListener(focusMonitor);
108         tvAccount.setOnFocusChangeListener(focusMonitor);
109         tvAmount.setOnFocusChangeListener(focusMonitor);
110
111         MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription,
112                 MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
113         MLDB.hookAutocompletionAdapter(tvAccount.getContext(), tvAccount, MLDB.ACCOUNTS_TABLE,
114                 "name", true, this, mProfile);
115
116         // FIXME: react on configuration (locale) changes
117         decimalSeparator = String.valueOf(DecimalFormatSymbols.getInstance()
118                                                               .getMonetaryDecimalSeparator());
119         decimalDot = ".";
120
121         final TextWatcher tw = new TextWatcher() {
122             @Override
123             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
124             }
125
126             @Override
127             public void onTextChanged(CharSequence s, int start, int before, int count) {
128             }
129
130             @Override
131             public void afterTextChanged(Editable s) {
132 //                debug("input", "text changed");
133                 if (inUpdate)
134                     return;
135
136                 Logger.debug("textWatcher", "calling syncData()");
137                 syncData();
138                 Logger.debug("textWatcher",
139                         "syncData() returned, checking if transaction is submittable");
140                 adapter.model.checkTransactionSubmittable(adapter);
141                 Logger.debug("textWatcher", "done");
142             }
143         };
144         final TextWatcher amountWatcher = new TextWatcher() {
145             @Override
146             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
147             }
148             @Override
149             public void onTextChanged(CharSequence s, int start, int before, int count) {
150
151             }
152             @Override
153             public void afterTextChanged(Editable s) {
154                 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
155                     // only one decimal separator is allowed
156                     // plus and minus are allowed only at the beginning
157                     String val = s.toString();
158                     if (val.isEmpty())
159                         tvAmount.setKeyListener(DigitsKeyListener.getInstance(
160                                 "0123456789+-" + decimalSeparator + decimalDot));
161                     else if (val.contains(decimalSeparator) || val.contains(decimalDot))
162                         tvAmount.setKeyListener(DigitsKeyListener.getInstance("0123456789"));
163                     else
164                         tvAmount.setKeyListener(DigitsKeyListener.getInstance(
165                                 "0123456789" + decimalSeparator + decimalDot));
166
167                     syncData();
168                     adapter.model.checkTransactionSubmittable(adapter);
169                 }
170             }
171         };
172         tvDescription.addTextChangedListener(tw);
173         tvAccount.addTextChangedListener(tw);
174         tvAmount.addTextChangedListener(amountWatcher);
175
176         // FIXME: react on locale changes
177         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
178             tvAmount.setKeyListener(DigitsKeyListener.getInstance(Locale.getDefault(), true, true));
179         else
180             tvAmount.setKeyListener(
181                     DigitsKeyListener.getInstance("0123456789+-" + decimalSeparator + decimalDot));
182
183         dateObserver = date -> {
184             if (syncingData)
185                 return;
186             syncingData = true;
187             try {
188                 tvDate.setText(item.getFormattedDate());
189             }
190             finally {
191                 syncingData = false;
192             }
193         };
194         descriptionObserver = description -> {
195             if (syncingData)
196                 return;
197             syncingData = true;
198             try {
199                 tvDescription.setText(description);
200             }
201             finally {
202                 syncingData = false;
203             }
204         };
205         hintObserver = hint -> {
206             if (syncingData)
207                 return;
208             syncingData = true;
209             try {
210                 if (hint == null)
211                     tvAmount.setHint(R.string.zero_amount);
212                 else
213                     tvAmount.setHint(hint);
214             }
215             finally {
216                 syncingData = false;
217             }
218         };
219         editableObserver = this::setEditable;
220         focusedAccountObserver = index -> {
221             if ((index != null) && index.equals(getAdapterPosition())) {
222                 switch (item.getType()) {
223                     case generalData:
224                         // bad idea - double pop-up, and not really necessary.
225                         // the user can tap the input to get the calendar
226                         //if (!tvDate.hasFocus()) tvDate.requestFocus();
227                         boolean focused = tvDescription.requestFocus();
228                         tvDescription.dismissDropDown();
229                         if (focused)
230                             Misc.showSoftKeyboard(
231                                     (NewTransactionActivity) tvDescription.getContext());
232                         break;
233                     case transactionRow:
234                         // do nothing if a row element already has the focus
235                         if (!itemView.hasFocus()) {
236                             focused = tvAccount.requestFocus();
237                             tvAccount.dismissDropDown();
238                             if (focused)
239                                 Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
240                         }
241
242                         break;
243                 }
244             }
245         };
246         accountCountObserver = count -> {
247             final int adapterPosition = getAdapterPosition();
248             final int layoutPosition = getLayoutPosition();
249             Logger.debug("holder",
250                     String.format(Locale.US, "count=%d; pos=%d, layoutPos=%d [%s]", count,
251                             adapterPosition, layoutPosition, item.getType()
252                                                                  .toString()
253                                                                  .concat(item.getType() ==
254                                                                          NewTransactionModel.ItemType.transactionRow
255                                                                          ? String.format(Locale.US,
256                                                                          "'%s'=%s",
257                                                                          item.getAccount()
258                                                                              .getAccountName(),
259                                                                          item.getAccount()
260                                                                              .isAmountSet()
261                                                                          ? String.format(Locale.US,
262                                                                                  "%.2f",
263                                                                                  item.getAccount()
264                                                                                      .getAmount())
265                                                                          : "unset") : "")));
266             if (adapterPosition == count)
267                 tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE);
268             else
269                 tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
270         };
271     }
272     private void setEditable(Boolean editable) {
273         tvDate.setEnabled(editable);
274         tvDescription.setEnabled(editable);
275         tvAccount.setEnabled(editable);
276         tvAmount.setEnabled(editable);
277     }
278     private void beginUpdates() {
279         if (inUpdate)
280             throw new RuntimeException("Already in update mode");
281         inUpdate = true;
282     }
283     private void endUpdates() {
284         if (!inUpdate)
285             throw new RuntimeException("Not in update mode");
286         inUpdate = false;
287     }
288     /**
289      * syncData()
290      * <p>
291      * Stores the data from the UI elements into the model item
292      */
293     private void syncData() {
294         if (item == null)
295             return;
296
297         if (syncingData) {
298             Logger.debug("new-trans", "skipping syncData() loop");
299             return;
300         }
301
302         syncingData = true;
303
304         try {
305             switch (item.getType()) {
306                 case generalData:
307                     item.setDate(String.valueOf(tvDate.getText()));
308                     item.setDescription(String.valueOf(tvDescription.getText()));
309                     break;
310                 case transactionRow:
311                     item.getAccount()
312                         .setAccountName(String.valueOf(tvAccount.getText()));
313
314                     // TODO: handle multiple amounts
315                     String amount = String.valueOf(tvAmount.getText());
316                     amount = amount.trim();
317
318                     if (amount.isEmpty()) {
319                         item.getAccount()
320                             .resetAmount();
321                     }
322                     else {
323                         try {
324                             amount = amount.replace(decimalSeparator, decimalDot);
325                             item.getAccount()
326                                 .setAmount(Float.parseFloat(amount));
327                         }
328                         catch (NumberFormatException e) {
329                             Logger.debug("new-trans", String.format(
330                                     "assuming amount is not set due to number format exception. " +
331                                     "input was '%s'", amount));
332                             item.getAccount()
333                                 .resetAmount();
334                         }
335                     }
336
337                     break;
338                 case bottomFiller:
339                     throw new RuntimeException("Should not happen");
340             }
341         }
342         finally {
343             syncingData = false;
344         }
345     }
346     private void pickTransactionDate() {
347         DatePickerFragment picker = new DatePickerFragment();
348         picker.setOnDatePickedListener(this);
349         picker.show(((NewTransactionActivity) tvDate.getContext()).getSupportFragmentManager(),
350                 "datePicker");
351     }
352     /**
353      * setData
354      *
355      * @param item updates the UI elements with the data from the model item
356      */
357     @SuppressLint("DefaultLocale")
358     public void setData(NewTransactionModel.Item item) {
359         beginUpdates();
360         try {
361             if (this.item != null && !this.item.equals(item)) {
362                 this.item.stopObservingDate(dateObserver);
363                 this.item.stopObservingDescription(descriptionObserver);
364                 this.item.stopObservingAmountHint(hintObserver);
365                 this.item.stopObservingEditableFlag(editableObserver);
366                 this.item.getModel()
367                          .stopObservingFocusedItem(focusedAccountObserver);
368                 this.item.getModel()
369                          .stopObservingAccountCount(accountCountObserver);
370
371                 this.item = null;
372             }
373
374             switch (item.getType()) {
375                 case generalData:
376                     tvDate.setText(item.getFormattedDate());
377                     tvDescription.setText(item.getDescription());
378                     lHead.setVisibility(View.VISIBLE);
379                     lAccount.setVisibility(View.GONE);
380                     lPadding.setVisibility(View.GONE);
381                     setEditable(true);
382                     break;
383                 case transactionRow:
384                     LedgerTransactionAccount acc = item.getAccount();
385                     tvAccount.setText(acc.getAccountName());
386                     if (acc.isAmountSet()) {
387                         tvAmount.setText(String.format("%1.2f", acc.getAmount()));
388                     }
389                     else {
390                         tvAmount.setText("");
391 //                        tvAmount.setHint(R.string.zero_amount);
392                     }
393                     tvAmount.setHint(item.getAmountHint());
394                     lHead.setVisibility(View.GONE);
395                     lAccount.setVisibility(View.VISIBLE);
396                     lPadding.setVisibility(View.GONE);
397                     setEditable(true);
398                     break;
399                 case bottomFiller:
400                     lHead.setVisibility(View.GONE);
401                     lAccount.setVisibility(View.GONE);
402                     lPadding.setVisibility(View.VISIBLE);
403                     setEditable(false);
404                     break;
405             }
406
407             if (this.item == null) { // was null or has changed
408                 this.item = item;
409                 final NewTransactionActivity activity =
410                         (NewTransactionActivity) tvDescription.getContext();
411                 item.observeDate(activity, dateObserver);
412                 item.observeDescription(activity, descriptionObserver);
413                 item.observeAmountHint(activity, hintObserver);
414                 item.observeEditableFlag(activity, editableObserver);
415                 item.getModel()
416                     .observeFocusedItem(activity, focusedAccountObserver);
417                 item.getModel()
418                     .observeAccountCount(activity, accountCountObserver);
419             }
420         }
421         finally {
422             endUpdates();
423         }
424     }
425     @Override
426     public void onDatePicked(int year, int month, int day) {
427         final Calendar c = GregorianCalendar.getInstance();
428         c.set(year, month, day);
429         item.setDate(c.getTime());
430         boolean focused = tvDescription.requestFocus();
431         if (focused)
432             Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
433
434     }
435     @Override
436     public void descriptionSelected(String description) {
437         tvAccount.setText(description);
438         tvAmount.requestFocus(View.FOCUS_FORWARD);
439     }
440 }