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