]> git.ktnx.net Git - mobile-ledger.git/blob - new_transaction/NewTransactionItemHolder.java
bump androidx.constraintlayout library version
[mobile-ledger.git] / new_transaction / NewTransactionItemHolder.java
1 /*
2  * Copyright © 2021 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.new_transaction;
19
20 import android.annotation.SuppressLint;
21 import android.graphics.Typeface;
22 import android.text.Editable;
23 import android.text.TextUtils;
24 import android.text.TextWatcher;
25 import android.view.Gravity;
26 import android.view.View;
27 import android.view.inputmethod.EditorInfo;
28 import android.widget.EditText;
29 import android.widget.SimpleCursorAdapter;
30 import android.widget.TextView;
31
32 import androidx.annotation.ColorInt;
33 import androidx.annotation.NonNull;
34 import androidx.constraintlayout.widget.ConstraintLayout;
35 import androidx.recyclerview.widget.RecyclerView;
36
37 import net.ktnx.mobileledger.R;
38 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
39 import net.ktnx.mobileledger.databinding.NewTransactionRowBinding;
40 import net.ktnx.mobileledger.db.AccountAutocompleteAdapter;
41 import net.ktnx.mobileledger.model.Currency;
42 import net.ktnx.mobileledger.model.Data;
43 import net.ktnx.mobileledger.model.MobileLedgerProfile;
44 import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
45 import net.ktnx.mobileledger.ui.DatePickerFragment;
46 import net.ktnx.mobileledger.ui.TextViewClearHelper;
47 import net.ktnx.mobileledger.utils.DimensionUtils;
48 import net.ktnx.mobileledger.utils.Logger;
49 import net.ktnx.mobileledger.utils.MLDB;
50 import net.ktnx.mobileledger.utils.Misc;
51 import net.ktnx.mobileledger.utils.SimpleDate;
52
53 import java.text.DecimalFormatSymbols;
54 import java.text.ParseException;
55 import java.util.Objects;
56
57 class NewTransactionItemHolder extends RecyclerView.ViewHolder
58         implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback {
59     private final String decimalDot = ".";
60     private final MobileLedgerProfile mProfile;
61     private final NewTransactionRowBinding b;
62     private final NewTransactionItemsAdapter mAdapter;
63     private boolean ignoreFocusChanges = false;
64     private String decimalSeparator;
65     private boolean inUpdate = false;
66     private boolean syncingData = false;
67     //TODO multiple amounts with different currencies per posting?
68     NewTransactionItemHolder(@NonNull NewTransactionRowBinding b,
69                              NewTransactionItemsAdapter adapter) {
70         super(b.getRoot());
71         this.b = b;
72         this.mAdapter = adapter;
73         new TextViewClearHelper().attachToTextView(b.comment);
74
75         b.newTransactionDescription.setNextFocusForwardId(View.NO_ID);
76         b.accountRowAccName.setNextFocusForwardId(View.NO_ID);
77         b.accountRowAccAmounts.setNextFocusForwardId(View.NO_ID); // magic!
78
79         b.newTransactionDate.setOnClickListener(v -> pickTransactionDate());
80
81         b.accountCommentButton.setOnClickListener(v -> {
82             b.comment.setVisibility(View.VISIBLE);
83             b.comment.requestFocus();
84         });
85
86         b.transactionCommentButton.setOnClickListener(v -> {
87             b.transactionComment.setVisibility(View.VISIBLE);
88             b.transactionComment.requestFocus();
89         });
90
91         mProfile = Data.getProfile();
92
93         @SuppressLint("DefaultLocale") View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
94             final int id = v.getId();
95             if (hasFocus) {
96                 boolean wasSyncing = syncingData;
97                 syncingData = true;
98                 try {
99                     final int pos = getAdapterPosition();
100                     if (id == R.id.account_row_acc_name) {
101                         adapter.noteFocusIsOnAccount(pos);
102                     }
103                     else if (id == R.id.account_row_acc_amounts) {
104                         adapter.noteFocusIsOnAmount(pos);
105                     }
106                     else if (id == R.id.comment) {
107                         adapter.noteFocusIsOnComment(pos);
108                     }
109                     else if (id == R.id.transaction_comment) {
110                         adapter.noteFocusIsOnTransactionComment(pos);
111                     }
112                     else if (id == R.id.new_transaction_description) {
113                         adapter.noteFocusIsOnDescription(pos);
114                     }
115                     else
116                         throw new IllegalStateException("Where is the focus?");
117                 }
118                 finally {
119                     syncingData = wasSyncing;
120                 }
121             }
122             else {  // lost focus
123                 if (id == R.id.account_row_acc_amounts) {
124                     try {
125                         String input = String.valueOf(b.accountRowAccAmounts.getText());
126                         input = input.replace(decimalSeparator, decimalDot);
127                         final String newText = String.format("%4.2f", Float.parseFloat(input));
128                         if (!newText.equals(input)) {
129                             boolean wasSyncingData = syncingData;
130                             syncingData = true;
131                             try {
132                                 b.accountRowAccAmounts.setText(newText);
133                             }
134                             finally {
135                                 syncingData = wasSyncingData;
136                             }
137                         }
138                     }
139                     catch (NumberFormatException ex) {
140                         // ignored
141                     }
142                 }
143             }
144
145             if (id == R.id.comment) {
146                 commentFocusChanged(b.comment, hasFocus);
147             }
148             else if (id == R.id.transaction_comment) {
149                 commentFocusChanged(b.transactionComment, hasFocus);
150             }
151         };
152
153         b.newTransactionDescription.setOnFocusChangeListener(focusMonitor);
154         b.accountRowAccName.setOnFocusChangeListener(focusMonitor);
155         b.accountRowAccAmounts.setOnFocusChangeListener(focusMonitor);
156         b.comment.setOnFocusChangeListener(focusMonitor);
157         b.transactionComment.setOnFocusChangeListener(focusMonitor);
158
159         NewTransactionActivity activity = (NewTransactionActivity) b.getRoot()
160                                                                     .getContext();
161
162         MLDB.hookAutocompletionAdapter(activity, b.newTransactionDescription,
163                 MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, activity, mProfile);
164         b.accountRowAccName.setAdapter(new AccountAutocompleteAdapter(b.getRoot()
165                                                                        .getContext(), mProfile));
166
167         decimalSeparator = "";
168         Data.locale.observe(activity, locale -> decimalSeparator = String.valueOf(
169                 DecimalFormatSymbols.getInstance(locale)
170                                     .getMonetaryDecimalSeparator()));
171
172         final TextWatcher tw = new TextWatcher() {
173             @Override
174             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
175             }
176
177             @Override
178             public void onTextChanged(CharSequence s, int start, int before, int count) {
179             }
180
181             @Override
182             public void afterTextChanged(Editable s) {
183 //                debug("input", "text changed");
184                 if (inUpdate)
185                     return;
186
187                 Logger.debug("textWatcher", "calling syncData()");
188                 if (syncData()) {
189                     Logger.debug("textWatcher",
190                             "syncData() returned, checking if transaction is submittable");
191                     adapter.model.checkTransactionSubmittable(null);
192                 }
193                 Logger.debug("textWatcher", "done");
194             }
195         };
196         final TextWatcher amountWatcher = new TextWatcher() {
197             @Override
198             public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
199             @Override
200             public void onTextChanged(CharSequence s, int start, int before, int count) {}
201             @Override
202             public void afterTextChanged(Editable s) {
203                 checkAmountValid(s.toString());
204
205                 if (syncData())
206                     adapter.model.checkTransactionSubmittable(null);
207             }
208         };
209         b.newTransactionDescription.addTextChangedListener(tw);
210         monitorComment(b.transactionComment);
211         b.accountRowAccName.addTextChangedListener(tw);
212         monitorComment(b.comment);
213         b.accountRowAccAmounts.addTextChangedListener(amountWatcher);
214
215         b.currencyButton.setOnClickListener(v -> {
216             CurrencySelectorFragment cpf = new CurrencySelectorFragment();
217             cpf.showPositionAndPadding();
218             cpf.setOnCurrencySelectedListener(c -> adapter.setItemCurrency(getAdapterPosition(),
219                     (c == null) ? null : c.getName()));
220             cpf.show(activity.getSupportFragmentManager(), "currency-selector");
221         });
222
223         commentFocusChanged(b.transactionComment, false);
224         commentFocusChanged(b.comment, false);
225
226         adapter.model.getFocusInfo()
227                      .observe(activity, this::applyFocus);
228
229         Data.currencyGap.observe(activity,
230                 hasGap -> updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(),
231                         hasGap));
232
233         Data.currencySymbolPosition.observe(activity,
234                 position -> updateCurrencyPositionAndPadding(position,
235                         Data.currencyGap.getValue()));
236
237         adapter.model.getShowCurrency()
238                      .observe(activity, showCurrency -> {
239                          if (showCurrency) {
240                              b.currency.setVisibility(View.VISIBLE);
241                              b.currencyButton.setVisibility(View.VISIBLE);
242                              setCurrencyString(mProfile.getDefaultCommodity());
243                          }
244                          else {
245                              b.currency.setVisibility(View.GONE);
246                              b.currencyButton.setVisibility(View.GONE);
247                              setCurrencyString(null);
248                          }
249                      });
250
251         adapter.model.getShowComments()
252                      .observe(activity, show -> {
253                          ConstraintLayout.LayoutParams amountLayoutParams =
254                                  (ConstraintLayout.LayoutParams) b.amountLayout.getLayoutParams();
255                          ConstraintLayout.LayoutParams accountParams =
256                                  (ConstraintLayout.LayoutParams) b.accountRowAccName.getLayoutParams();
257
258                          if (show) {
259                              accountParams.endToStart = ConstraintLayout.LayoutParams.UNSET;
260                              accountParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
261
262                              amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.UNSET;
263                              amountLayoutParams.topToBottom = b.accountRowAccName.getId();
264
265                              b.commentLayout.setVisibility(View.VISIBLE);
266                          }
267                          else {
268                              accountParams.endToStart = b.amountLayout.getId();
269                              accountParams.endToEnd = ConstraintLayout.LayoutParams.UNSET;
270
271                              amountLayoutParams.topToBottom = ConstraintLayout.LayoutParams.UNSET;
272                              amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
273
274                              b.commentLayout.setVisibility(View.GONE);
275                          }
276
277                          b.accountRowAccName.setLayoutParams(accountParams);
278                          b.amountLayout.setLayoutParams(amountLayoutParams);
279
280                          b.transactionCommentLayout.setVisibility(show ? View.VISIBLE : View.GONE);
281                      });
282     }
283     private void applyFocus(NewTransactionModel.FocusInfo focusInfo) {
284         if (ignoreFocusChanges) {
285             Logger.debug("new-trans", "Ignoring focus change");
286             return;
287         }
288         ignoreFocusChanges = true;
289         try {
290             if (((focusInfo == null) || (focusInfo.element == null) ||
291                  focusInfo.position != getAdapterPosition()))
292                 return;
293
294             NewTransactionModel.Item item = getItem();
295             if (item instanceof NewTransactionModel.TransactionHead) {
296                 NewTransactionModel.TransactionHead head = item.toTransactionHead();
297                 // bad idea - double pop-up, and not really necessary.
298                 // the user can tap the input to get the calendar
299                 //if (!tvDate.hasFocus()) tvDate.requestFocus();
300                 switch (focusInfo.element) {
301                     case TransactionComment:
302                         b.transactionComment.setVisibility(View.VISIBLE);
303                         b.transactionComment.requestFocus();
304                         break;
305                     case Description:
306                         boolean focused = b.newTransactionDescription.requestFocus();
307 //                            tvDescription.dismissDropDown();
308                         if (focused)
309                             Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot()
310                                                                             .getContext());
311                         break;
312                 }
313             }
314             else if (item instanceof NewTransactionModel.TransactionAccount) {
315                 NewTransactionModel.TransactionAccount acc = item.toTransactionAccount();
316                 switch (focusInfo.element) {
317                     case Amount:
318                         b.accountRowAccAmounts.requestFocus();
319                         break;
320                     case Comment:
321                         b.comment.setVisibility(View.VISIBLE);
322                         b.comment.requestFocus();
323                         break;
324                     case Account:
325                         boolean focused = b.accountRowAccName.requestFocus();
326 //                                         b.accountRowAccName.dismissDropDown();
327                         if (focused)
328                             Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot()
329                                                                             .getContext());
330                         break;
331                 }
332             }
333         }
334         finally {
335             ignoreFocusChanges = false;
336         }
337     }
338     public void checkAmountValid(String s) {
339         boolean valid = true;
340         try {
341             if (s.length() > 0) {
342                 float ignored = Float.parseFloat(s.replace(decimalSeparator, decimalDot));
343             }
344         }
345         catch (NumberFormatException ex) {
346             valid = false;
347         }
348
349         displayAmountValidity(valid);
350     }
351     private void displayAmountValidity(boolean valid) {
352         b.accountRowAccAmounts.setCompoundDrawablesRelativeWithIntrinsicBounds(
353                 valid ? 0 : R.drawable.ic_error_outline_black_24dp, 0, 0, 0);
354         b.accountRowAccAmounts.setMinEms(valid ? 4 : 5);
355     }
356     private void monitorComment(EditText editText) {
357         editText.addTextChangedListener(new TextWatcher() {
358             @Override
359             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
360             }
361             @Override
362             public void onTextChanged(CharSequence s, int start, int before, int count) {
363             }
364             @Override
365             public void afterTextChanged(Editable s) {
366 //                debug("input", "text changed");
367                 if (inUpdate)
368                     return;
369
370                 Logger.debug("textWatcher", "calling syncData()");
371                 syncData();
372                 Logger.debug("textWatcher",
373                         "syncData() returned, checking if transaction is submittable");
374                 styleComment(editText, s.toString());
375                 Logger.debug("textWatcher", "done");
376             }
377         });
378     }
379     private void commentFocusChanged(TextView textView, boolean hasFocus) {
380         @ColorInt int textColor;
381         textColor = b.dummyText.getTextColors()
382                                .getDefaultColor();
383         if (hasFocus) {
384             textView.setTypeface(null, Typeface.NORMAL);
385             textView.setHint(R.string.transaction_account_comment_hint);
386         }
387         else {
388             int alpha = (textColor >> 24 & 0xff);
389             alpha = 3 * alpha / 4;
390             textColor = (alpha << 24) | (0x00ffffff & textColor);
391             textView.setTypeface(null, Typeface.ITALIC);
392             textView.setHint("");
393             if (TextUtils.isEmpty(textView.getText())) {
394                 textView.setVisibility(View.INVISIBLE);
395             }
396         }
397         textView.setTextColor(textColor);
398
399     }
400     private void updateCurrencyPositionAndPadding(Currency.Position position, boolean hasGap) {
401         ConstraintLayout.LayoutParams amountLP =
402                 (ConstraintLayout.LayoutParams) b.accountRowAccAmounts.getLayoutParams();
403         ConstraintLayout.LayoutParams currencyLP =
404                 (ConstraintLayout.LayoutParams) b.currency.getLayoutParams();
405
406         if (position == Currency.Position.before) {
407             currencyLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
408             currencyLP.endToEnd = ConstraintLayout.LayoutParams.UNSET;
409
410             amountLP.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
411             amountLP.endToStart = ConstraintLayout.LayoutParams.UNSET;
412             amountLP.startToStart = ConstraintLayout.LayoutParams.UNSET;
413             amountLP.startToEnd = b.currency.getId();
414
415             b.currency.setGravity(Gravity.END);
416         }
417         else {
418             currencyLP.startToStart = ConstraintLayout.LayoutParams.UNSET;
419             currencyLP.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
420
421             amountLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
422             amountLP.startToEnd = ConstraintLayout.LayoutParams.UNSET;
423             amountLP.endToEnd = ConstraintLayout.LayoutParams.UNSET;
424             amountLP.endToStart = b.currency.getId();
425
426             b.currency.setGravity(Gravity.START);
427         }
428
429         amountLP.resolveLayoutDirection(b.accountRowAccAmounts.getLayoutDirection());
430         currencyLP.resolveLayoutDirection(b.currency.getLayoutDirection());
431
432         b.accountRowAccAmounts.setLayoutParams(amountLP);
433         b.currency.setLayoutParams(currencyLP);
434
435         // distance between the amount and the currency symbol
436         int gapSize = DimensionUtils.sp2px(b.currency.getContext(), 5);
437
438         if (position == Currency.Position.before) {
439             b.currency.setPaddingRelative(0, 0, hasGap ? gapSize : 0, 0);
440         }
441         else {
442             b.currency.setPaddingRelative(hasGap ? gapSize : 0, 0, 0, 0);
443         }
444     }
445     private void setCurrencyString(String currency) {
446         @ColorInt int textColor = b.dummyText.getTextColors()
447                                              .getDefaultColor();
448         if (TextUtils.isEmpty(currency)) {
449             b.currency.setText(R.string.currency_symbol);
450             int alpha = (textColor >> 24) & 0xff;
451             alpha = alpha * 3 / 4;
452             b.currency.setTextColor((alpha << 24) | (0x00ffffff & textColor));
453         }
454         else {
455             b.currency.setText(currency);
456             b.currency.setTextColor(textColor);
457         }
458     }
459     private void setCurrency(Currency currency) {
460         setCurrencyString((currency == null) ? null : currency.getName());
461     }
462     private void setEditable(Boolean editable) {
463         b.newTransactionDate.setEnabled(editable);
464         b.newTransactionDescription.setEnabled(editable);
465         b.accountRowAccName.setEnabled(editable);
466         b.accountRowAccAmounts.setEnabled(editable);
467     }
468     private void beginUpdates() {
469         if (inUpdate)
470             throw new RuntimeException("Already in update mode");
471         inUpdate = true;
472     }
473     private void endUpdates() {
474         if (!inUpdate)
475             throw new RuntimeException("Not in update mode");
476         inUpdate = false;
477     }
478     /**
479      * syncData()
480      * <p>
481      * Stores the data from the UI elements into the model item
482      * Returns true if there were changes made that suggest transaction has to be
483      * checked for being submittable
484      */
485     private boolean syncData() {
486         if (syncingData) {
487             Logger.debug("new-trans", "skipping syncData() loop");
488             return false;
489         }
490
491         if (getAdapterPosition() < 0) {
492             // probably the row was swiped out
493             Logger.debug("new-trans", "Ignoring request to suncData(): adapter position negative");
494             return false;
495         }
496
497         NewTransactionModel.Item item = getItem();
498
499         syncingData = true;
500
501         boolean significantChange = false;
502
503         try {
504             if (item instanceof NewTransactionModel.TransactionHead) {
505                 NewTransactionModel.TransactionHead head = item.toTransactionHead();
506
507                 head.setDate(String.valueOf(b.newTransactionDate.getText()));
508
509                 // transaction description is required
510                 if (TextUtils.isEmpty(head.getDescription()) !=
511                     TextUtils.isEmpty(b.newTransactionDescription.getText()))
512                     significantChange = true;
513
514                 head.setDescription(String.valueOf(b.newTransactionDescription.getText()));
515                 head.setComment(String.valueOf(b.transactionComment.getText()));
516             }
517             else if (item instanceof NewTransactionModel.TransactionAccount) {
518                 NewTransactionModel.TransactionAccount acc = item.toTransactionAccount();
519
520                 // having account name is important
521                 final Editable incomingAccountName = b.accountRowAccName.getText();
522                 if (TextUtils.isEmpty(acc.getAccountName()) !=
523                     TextUtils.isEmpty(incomingAccountName))
524                     significantChange = true;
525
526                 acc.setAccountName(String.valueOf(incomingAccountName));
527                 final int accNameSelEnd = b.accountRowAccName.getSelectionEnd();
528                 final int accNameSelStart = b.accountRowAccName.getSelectionStart();
529                 acc.setAccountNameCursorPosition(accNameSelEnd);
530
531                 acc.setComment(String.valueOf(b.comment.getText()));
532
533                 String amount = String.valueOf(b.accountRowAccAmounts.getText());
534                 amount = amount.trim();
535
536                 if (amount.isEmpty()) {
537                     if (acc.isAmountSet())
538                         significantChange = true;
539                     acc.resetAmount();
540                     acc.setAmountValid(true);
541                 }
542                 else {
543                     try {
544                         amount = amount.replace(decimalSeparator, decimalDot);
545                         final float parsedAmount = Float.parseFloat(amount);
546                         if (!acc.isAmountSet() || !Misc.equalFloats(parsedAmount, acc.getAmount()))
547                             significantChange = true;
548                         acc.setAmount(parsedAmount);
549                         acc.setAmountValid(true);
550                     }
551                     catch (NumberFormatException e) {
552                         Logger.debug("new-trans", String.format(
553                                 "assuming amount is not set due to number format exception. " +
554                                 "input was '%s'", amount));
555                         if (acc.isAmountValid())
556                             significantChange = true;
557                         acc.setAmountValid(false);
558                     }
559                     final String curr = String.valueOf(b.currency.getText());
560                     final String currValue;
561                     if (curr.equals(b.currency.getContext()
562                                               .getResources()
563                                               .getString(R.string.currency_symbol)) ||
564                         curr.isEmpty())
565                         currValue = null;
566                     else
567                         currValue = curr;
568
569                     if (!significantChange && !TextUtils.equals(acc.getCurrency(), currValue))
570                         significantChange = true;
571                     acc.setCurrency(currValue);
572                 }
573             }
574             else {
575                 throw new RuntimeException("Should not happen");
576             }
577
578             return significantChange;
579         }
580         catch (ParseException e) {
581             throw new RuntimeException("Should not happen", e);
582         }
583         finally {
584             syncingData = false;
585         }
586     }
587     private void pickTransactionDate() {
588         DatePickerFragment picker = new DatePickerFragment();
589         picker.setFutureDates(mProfile.getFutureDates());
590         picker.setOnDatePickedListener(this);
591         picker.setCurrentDateFromText(b.newTransactionDate.getText());
592         picker.show(((NewTransactionActivity) b.getRoot()
593                                                .getContext()).getSupportFragmentManager(), null);
594     }
595     /**
596      * bind
597      *
598      * @param item updates the UI elements with the data from the model item
599      */
600     @SuppressLint("DefaultLocale")
601     public void bind(@NonNull NewTransactionModel.Item item) {
602         beginUpdates();
603         try {
604             syncingData = true;
605             try {
606                 if (item instanceof NewTransactionModel.TransactionHead) {
607                     NewTransactionModel.TransactionHead head = item.toTransactionHead();
608                     b.newTransactionDate.setText(head.getFormattedDate());
609
610                     // avoid triggering completion pop-up
611                     SimpleCursorAdapter a =
612                             (SimpleCursorAdapter) b.newTransactionDescription.getAdapter();
613                     try {
614                         b.newTransactionDescription.setAdapter(null);
615                         b.newTransactionDescription.setText(head.getDescription());
616                     }
617                     finally {
618                         b.newTransactionDescription.setAdapter(a);
619                     }
620
621                     b.transactionComment.setText(head.getComment());
622                     //styleComment(b.transactionComment, head.getComment());
623
624                     b.ntrData.setVisibility(View.VISIBLE);
625                     b.ntrAccount.setVisibility(View.GONE);
626                     setEditable(true);
627                 }
628                 else if (item instanceof NewTransactionModel.TransactionAccount) {
629                     NewTransactionModel.TransactionAccount acc = item.toTransactionAccount();
630
631                     final String incomingAccountName = acc.getAccountName();
632                     final String presentAccountName = String.valueOf(b.accountRowAccName.getText());
633                     if (!TextUtils.equals(incomingAccountName, presentAccountName)) {
634                         Logger.debug("bind",
635                                 String.format("Setting account name from '%s' to '%s' (| @ %d)",
636                                         presentAccountName, incomingAccountName,
637                                         acc.getAccountNameCursorPosition()));
638                         // avoid triggering completion pop-up
639                         AccountAutocompleteAdapter a =
640                                 (AccountAutocompleteAdapter) b.accountRowAccName.getAdapter();
641                         try {
642                             b.accountRowAccName.setAdapter(null);
643                             b.accountRowAccName.setText(incomingAccountName);
644                             b.accountRowAccName.setSelection(acc.getAccountNameCursorPosition());
645                         }
646                         finally {
647                             b.accountRowAccName.setAdapter(a);
648                         }
649                     }
650
651                     final String amountHint = acc.getAmountHint();
652                     if (amountHint == null) {
653                         b.accountRowAccAmounts.setHint(R.string.zero_amount);
654                     }
655                     else {
656                         b.accountRowAccAmounts.setHint(amountHint);
657                     }
658
659                     b.accountRowAccAmounts.setImeOptions(
660                             acc.isLast() ? EditorInfo.IME_ACTION_DONE : EditorInfo.IME_ACTION_NEXT);
661
662                     setCurrencyString(acc.getCurrency());
663                     b.accountRowAccAmounts.setText(
664                             acc.isAmountSet() ? String.format("%4.2f", acc.getAmount()) : null);
665                     displayAmountValidity(true);
666
667                     b.comment.setText(acc.getComment());
668
669                     b.ntrData.setVisibility(View.GONE);
670                     b.ntrAccount.setVisibility(View.VISIBLE);
671
672                     setEditable(true);
673                 }
674                 else {
675                     throw new RuntimeException("Don't know how to handle " + item);
676                 }
677
678                 applyFocus(mAdapter.model.getFocusInfo()
679                                          .getValue());
680             }
681             finally {
682                 syncingData = false;
683             }
684         }
685         finally {
686             endUpdates();
687         }
688     }
689     private void styleComment(EditText editText, String comment) {
690         final View focusedView = editText.findFocus();
691         editText.setTypeface(null, (focusedView == editText) ? Typeface.NORMAL : Typeface.ITALIC);
692         editText.setVisibility(
693                 ((focusedView != editText) && TextUtils.isEmpty(comment)) ? View.INVISIBLE
694                                                                           : View.VISIBLE);
695     }
696     @Override
697     public void onDatePicked(int year, int month, int day) {
698         final NewTransactionModel.TransactionHead head = getItem().toTransactionHead();
699         head.setDate(new SimpleDate(year, month + 1, day));
700         b.newTransactionDate.setText(head.getFormattedDate());
701
702         boolean focused = b.newTransactionDescription.requestFocus();
703         if (focused)
704             Misc.showSoftKeyboard((NewTransactionActivity) b.getRoot()
705                                                             .getContext());
706
707     }
708     private NewTransactionModel.Item getItem() {
709         return Objects.requireNonNull(mAdapter.model.getItems()
710                                                     .getValue())
711                       .get(getAdapterPosition());
712     }
713     @Override
714     public void descriptionSelected(String description) {
715         b.accountRowAccName.setText(description);
716         b.accountRowAccAmounts.requestFocus(View.FOCUS_FORWARD);
717     }
718 }