]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
support multiple currencies in new transaction activity
[mobile-ledger.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.Gravity;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.view.inputmethod.EditorInfo;
29 import android.widget.AutoCompleteTextView;
30 import android.widget.EditText;
31 import android.widget.FrameLayout;
32 import android.widget.LinearLayout;
33 import android.widget.TextView;
34
35 import androidx.annotation.NonNull;
36 import androidx.appcompat.app.AppCompatActivity;
37 import androidx.constraintlayout.widget.ConstraintLayout;
38 import androidx.lifecycle.Observer;
39 import androidx.recyclerview.widget.RecyclerView;
40
41 import net.ktnx.mobileledger.R;
42 import net.ktnx.mobileledger.async.DescriptionSelectedCallback;
43 import net.ktnx.mobileledger.model.Currency;
44 import net.ktnx.mobileledger.model.Data;
45 import net.ktnx.mobileledger.model.LedgerTransactionAccount;
46 import net.ktnx.mobileledger.model.MobileLedgerProfile;
47 import net.ktnx.mobileledger.ui.CurrencySelectorFragment;
48 import net.ktnx.mobileledger.ui.DatePickerFragment;
49 import net.ktnx.mobileledger.ui.OnCurrencySelectedListener;
50 import net.ktnx.mobileledger.ui.TextViewClearHelper;
51 import net.ktnx.mobileledger.utils.Colors;
52 import net.ktnx.mobileledger.utils.DimensionUtils;
53 import net.ktnx.mobileledger.utils.Logger;
54 import net.ktnx.mobileledger.utils.MLDB;
55 import net.ktnx.mobileledger.utils.Misc;
56
57 import java.text.DecimalFormatSymbols;
58 import java.util.Calendar;
59 import java.util.Date;
60 import java.util.GregorianCalendar;
61 import java.util.Locale;
62
63 import static net.ktnx.mobileledger.ui.activity.NewTransactionModel.ItemType;
64
65 class NewTransactionItemHolder extends RecyclerView.ViewHolder
66         implements DatePickerFragment.DatePickedListener, DescriptionSelectedCallback,
67         OnCurrencySelectedListener {
68     private final String decimalSeparator;
69     private final String decimalDot;
70     private final TextView tvCurrency;
71     private NewTransactionModel.Item item;
72     private TextView tvDate;
73     private AutoCompleteTextView tvDescription;
74     private AutoCompleteTextView tvAccount;
75     private TextView tvComment;
76     private EditText tvAmount;
77     private LinearLayout lHead;
78     private ViewGroup lAccount;
79     private FrameLayout lPadding;
80     private MobileLedgerProfile mProfile;
81     private Date date;
82     private Observer<Date> dateObserver;
83     private Observer<String> descriptionObserver;
84     private Observer<String> hintObserver;
85     private Observer<Integer> focusedAccountObserver;
86     private Observer<Integer> accountCountObserver;
87     private Observer<Boolean> editableObserver;
88     private Observer<Boolean> commentVisibleObserver;
89     private Observer<String> commentObserver;
90     private Observer<Currency.Position> currencyPositionObserver;
91     private Observer<Boolean> currencyGapObserver;
92     private Observer<Locale> localeObserver;
93     private Observer<Currency> currencyObserver;
94     private Observer<Boolean> showCurrencyObserver;
95     private boolean inUpdate = false;
96     private boolean syncingData = false;
97     private View commentButton;
98     private NewTransactionItemsAdapter adapter;
99     //TODO multiple amounts with different currencies per posting
100     NewTransactionItemHolder(@NonNull View itemView, NewTransactionItemsAdapter adapter) {
101         super(itemView);
102         this.adapter = adapter;
103         tvAccount = itemView.findViewById(R.id.account_row_acc_name);
104         tvComment = itemView.findViewById(R.id.comment);
105         new TextViewClearHelper().attachToTextView((EditText) tvComment);
106         commentButton = itemView.findViewById(R.id.comment_button);
107         tvAmount = itemView.findViewById(R.id.account_row_acc_amounts);
108         tvCurrency = itemView.findViewById(R.id.currency);
109         tvDate = itemView.findViewById(R.id.new_transaction_date);
110         tvDescription = itemView.findViewById(R.id.new_transaction_description);
111         lHead = itemView.findViewById(R.id.ntr_data);
112         lAccount = itemView.findViewById(R.id.ntr_account);
113         lPadding = itemView.findViewById(R.id.ntr_padding);
114
115         tvDescription.setNextFocusForwardId(View.NO_ID);
116         tvAccount.setNextFocusForwardId(View.NO_ID);
117         tvAmount.setNextFocusForwardId(View.NO_ID); // magic!
118
119         tvDate.setOnClickListener(v -> pickTransactionDate());
120
121         mProfile = Data.profile.getValue();
122         if (mProfile == null)
123             throw new AssertionError();
124
125         View.OnFocusChangeListener focusMonitor = (v, hasFocus) -> {
126             if (hasFocus) {
127                 boolean wasSyncing = syncingData;
128                 syncingData = true;
129                 try {
130                     final int pos = getAdapterPosition();
131                     adapter.updateFocusedItem(pos);
132                     switch (v.getId()) {
133                         case R.id.account_row_acc_name:
134                             adapter.noteFocusIsOnAccount(pos);
135                             break;
136                         case R.id.account_row_acc_amounts:
137                             adapter.noteFocusIsOnAmount(pos);
138                             break;
139                         case R.id.comment:
140                             adapter.noteFocusIsOnComment(pos);
141                             break;
142                     }
143                 }
144                 finally {
145                     syncingData = wasSyncing;
146                 }
147             }
148         };
149
150         tvDescription.setOnFocusChangeListener(focusMonitor);
151         tvAccount.setOnFocusChangeListener(focusMonitor);
152         tvAmount.setOnFocusChangeListener(focusMonitor);
153
154         itemView.findViewById(R.id.comment_button)
155                 .setOnClickListener(v -> {
156                     final int pos = getAdapterPosition();
157                     adapter.toggleComment(pos);
158                 });
159         MLDB.hookAutocompletionAdapter(tvDescription.getContext(), tvDescription,
160                 MLDB.DESCRIPTION_HISTORY_TABLE, "description", false, adapter, mProfile);
161         MLDB.hookAutocompletionAdapter(tvAccount.getContext(), tvAccount, MLDB.ACCOUNTS_TABLE,
162                 "name", true, this, mProfile);
163
164         // FIXME: react on configuration (locale) changes
165         decimalSeparator = String.valueOf(DecimalFormatSymbols.getInstance()
166                                                               .getMonetaryDecimalSeparator());
167         decimalDot = ".";
168
169         final TextWatcher tw = new TextWatcher() {
170             @Override
171             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
172             }
173
174             @Override
175             public void onTextChanged(CharSequence s, int start, int before, int count) {
176             }
177
178             @Override
179             public void afterTextChanged(Editable s) {
180 //                debug("input", "text changed");
181                 if (inUpdate)
182                     return;
183
184                 Logger.debug("textWatcher", "calling syncData()");
185                 syncData();
186                 Logger.debug("textWatcher",
187                         "syncData() returned, checking if transaction is submittable");
188                 adapter.model.checkTransactionSubmittable(adapter);
189                 Logger.debug("textWatcher", "done");
190             }
191         };
192         final TextWatcher amountWatcher = new TextWatcher() {
193             @Override
194             public void beforeTextChanged(CharSequence s, int start, int count, int after) {
195                 Logger.debug("num",
196                         String.format(Locale.US, "beforeTextChanged: start=%d, count=%d, after=%d",
197                                 start, count, after));
198             }
199             @Override
200             public void onTextChanged(CharSequence s, int start, int before, int count) {}
201             @Override
202             public void afterTextChanged(Editable s) {
203                 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
204                     // only one decimal separator is allowed
205                     // plus and minus are allowed only at the beginning
206                     String allowed = "0123456789";
207                     String val = s.toString();
208                     Logger.debug("input", val);
209                     if (val.isEmpty() || (tvAmount.getSelectionStart() == 0))
210                         allowed += "-";
211                     if (!val.contains(decimalSeparator) && !val.contains(decimalDot))
212                         allowed += decimalSeparator + decimalDot;
213
214                     tvAmount.setKeyListener(DigitsKeyListener.getInstance(allowed));
215                 }
216
217                 if (syncData())
218                     adapter.model.checkTransactionSubmittable(adapter);
219             }
220         };
221         tvDescription.addTextChangedListener(tw);
222         tvAccount.addTextChangedListener(tw);
223         tvComment.addTextChangedListener(tw);
224         tvAmount.addTextChangedListener(amountWatcher);
225
226         tvCurrency.setOnClickListener(v -> {
227             CurrencySelectorFragment cpf = new CurrencySelectorFragment();
228             cpf.setOnCurrencySelectedListener(this);
229             final AppCompatActivity activity = (AppCompatActivity) v.getContext();
230             cpf.show(activity.getSupportFragmentManager(), "currency-selector");
231         });
232
233         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
234             tvAmount.setKeyListener(
235                     DigitsKeyListener.getInstance(Data.locale.getValue(), true, true));
236         else
237             tvAmount.setKeyListener(
238                     DigitsKeyListener.getInstance("0123456789+-" + decimalSeparator + decimalDot));
239
240         dateObserver = date -> {
241             if (syncingData)
242                 return;
243             syncingData = true;
244             try {
245                 tvDate.setText(item.getFormattedDate());
246             }
247             finally {
248                 syncingData = false;
249             }
250         };
251         descriptionObserver = description -> {
252             if (syncingData)
253                 return;
254             syncingData = true;
255             try {
256                 tvDescription.setText(description);
257             }
258             finally {
259                 syncingData = false;
260             }
261         };
262         hintObserver = hint -> {
263             if (syncingData)
264                 return;
265             syncingData = true;
266             try {
267                 if (hint == null)
268                     tvAmount.setHint(R.string.zero_amount);
269                 else
270                     tvAmount.setHint(hint);
271             }
272             finally {
273                 syncingData = false;
274             }
275         };
276         editableObserver = this::setEditable;
277         commentVisibleObserver = this::setCommentVisible;
278         commentObserver = this::setComment;
279         focusedAccountObserver = index -> {
280             if ((index != null) && index.equals(getAdapterPosition())) {
281                 switch (item.getType()) {
282                     case generalData:
283                         // bad idea - double pop-up, and not really necessary.
284                         // the user can tap the input to get the calendar
285                         //if (!tvDate.hasFocus()) tvDate.requestFocus();
286                         boolean focused = tvDescription.requestFocus();
287                         tvDescription.dismissDropDown();
288                         if (focused)
289                             Misc.showSoftKeyboard(
290                                     (NewTransactionActivity) tvDescription.getContext());
291                         break;
292                     case transactionRow:
293                         // do nothing if a row element already has the focus
294                         if (!itemView.hasFocus()) {
295                             switch (item.getFocusedElement()) {
296                                 case Amount:
297                                     tvAmount.requestFocus();
298                                     break;
299                                 case Comment:
300                                     tvComment.requestFocus();
301                                     break;
302                                 case Account:
303                                     focused = tvAccount.requestFocus();
304                                     tvAccount.dismissDropDown();
305                                     if (focused)
306                                         Misc.showSoftKeyboard(
307                                                 (NewTransactionActivity) tvAccount.getContext());
308                                     break;
309                             }
310                         }
311
312                         break;
313                 }
314             }
315         };
316         accountCountObserver = count -> {
317             final int adapterPosition = getAdapterPosition();
318             final int layoutPosition = getLayoutPosition();
319             Logger.debug("holder",
320                     String.format(Locale.US, "count=%d; pos=%d, layoutPos=%d [%s]", count,
321                             adapterPosition, layoutPosition, item.getType()
322                                                                  .toString()
323                                                                  .concat(item.getType() ==
324                                                                          ItemType.transactionRow
325                                                                          ? String.format(Locale.US,
326                                                                          "'%s'=%s",
327                                                                          item.getAccount()
328                                                                              .getAccountName(),
329                                                                          item.getAccount()
330                                                                              .isAmountSet()
331                                                                          ? String.format(Locale.US,
332                                                                                  "%.2f",
333                                                                                  item.getAccount()
334                                                                                      .getAmount())
335                                                                          : "unset") : "")));
336             if (adapterPosition == count)
337                 tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE);
338             else
339                 tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
340         };
341
342         localeObserver = locale -> {
343             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
344                 tvAmount.setKeyListener(DigitsKeyListener.getInstance(locale, true, true));
345         };
346
347         currencyObserver = currency -> {
348             setCurrency(currency);
349             adapter.model.checkTransactionSubmittable(adapter);
350         };
351
352         currencyGapObserver = hasGap -> {
353             updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(), hasGap);
354         };
355
356         currencyPositionObserver = position -> {
357             updateCurrencyPositionAndPadding(position, Data.currencyGap.getValue());
358         };
359
360         showCurrencyObserver = showCurrency -> {
361             if (showCurrency) {
362                 tvCurrency.setVisibility(View.VISIBLE);
363             }
364             else {
365                 tvCurrency.setVisibility(View.GONE);
366                 item.setCurrency(null);
367             }
368         };
369     }
370     private void updateCurrencyPositionAndPadding(Currency.Position position, boolean hasGap) {
371         ConstraintLayout.LayoutParams amountLP =
372                 (ConstraintLayout.LayoutParams) tvAmount.getLayoutParams();
373         ConstraintLayout.LayoutParams currencyLP =
374                 (ConstraintLayout.LayoutParams) tvCurrency.getLayoutParams();
375
376         if (position == Currency.Position.before) {
377             currencyLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
378             currencyLP.endToEnd = ConstraintLayout.LayoutParams.UNSET;
379
380             amountLP.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
381             amountLP.endToStart = ConstraintLayout.LayoutParams.UNSET;
382             amountLP.startToStart = ConstraintLayout.LayoutParams.UNSET;
383             amountLP.startToEnd = tvCurrency.getId();
384
385             tvCurrency.setGravity(Gravity.END);
386         }
387         else {
388             currencyLP.startToStart = ConstraintLayout.LayoutParams.UNSET;
389             currencyLP.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
390
391             amountLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
392             amountLP.startToEnd = ConstraintLayout.LayoutParams.UNSET;
393             amountLP.endToEnd = ConstraintLayout.LayoutParams.UNSET;
394             amountLP.endToStart = tvCurrency.getId();
395
396             tvCurrency.setGravity(Gravity.START);
397         }
398
399         amountLP.resolveLayoutDirection(tvAmount.getLayoutDirection());
400         currencyLP.resolveLayoutDirection(tvCurrency.getLayoutDirection());
401
402         tvAmount.setLayoutParams(amountLP);
403         tvCurrency.setLayoutParams(currencyLP);
404
405         // distance between the amount and the currency symbol
406         int gapSize = DimensionUtils.sp2px(tvCurrency.getContext(), 5);
407
408         if (position == Currency.Position.before) {
409             tvCurrency.setPaddingRelative(0, 0, hasGap ? gapSize : 0, 0);
410         }
411         else {
412             tvCurrency.setPaddingRelative(hasGap ? gapSize : 0, 0, 0, 0);
413         }
414     }
415     private void setCurrencyString(String currency) {
416         if ((currency == null) || currency.isEmpty()) {
417             tvCurrency.setText(R.string.currency_symbol);
418             tvCurrency.setTextColor(0x7f000000 + (0x00ffffff & Colors.defaultTextColor));
419         }
420         else {
421             tvCurrency.setText(currency);
422             tvCurrency.setTextColor(Colors.defaultTextColor);
423         }
424     }
425     private void setCurrency(Currency currency) {
426         setCurrencyString((currency == null) ? null : currency.getName());
427     }
428     private void setEditable(Boolean editable) {
429         tvDate.setEnabled(editable);
430         tvDescription.setEnabled(editable);
431         tvAccount.setEnabled(editable);
432         tvAmount.setEnabled(editable);
433     }
434     private void setCommentVisible(Boolean visible) {
435         if (visible) {
436             // showing; show the comment view and align the comment button to it
437             tvComment.setVisibility(View.VISIBLE);
438             tvComment.requestFocus();
439             ConstraintLayout.LayoutParams lp =
440                     (ConstraintLayout.LayoutParams) commentButton.getLayoutParams();
441             lp.bottomToBottom = R.id.comment;
442
443             commentButton.setLayoutParams(lp);
444         }
445         else {
446             // hiding; hide the comment comment view and align amounts layout under it
447             tvComment.setVisibility(View.GONE);
448             ConstraintLayout.LayoutParams lp =
449                     (ConstraintLayout.LayoutParams) commentButton.getLayoutParams();
450             lp.bottomToBottom = R.id.ntr_account;   // R.id.parent doesn't work here
451
452             commentButton.setLayoutParams(lp);
453         }
454     }
455     private void setComment(String comment) {
456         if ((comment != null) && !comment.isEmpty())
457             commentButton.setBackgroundResource(R.drawable.ic_comment_black_24dp);
458         else
459             commentButton.setBackgroundResource(R.drawable.ic_comment_gray_24dp);
460     }
461     private void beginUpdates() {
462         if (inUpdate)
463             throw new RuntimeException("Already in update mode");
464         inUpdate = true;
465     }
466     private void endUpdates() {
467         if (!inUpdate)
468             throw new RuntimeException("Not in update mode");
469         inUpdate = false;
470     }
471     /**
472      * syncData()
473      * <p>
474      * Stores the data from the UI elements into the model item
475      * Returns true if there were changes made that suggest transaction has to be
476      * checked for being submittable
477      */
478     private boolean syncData() {
479         if (item == null)
480             return false;
481
482         if (syncingData) {
483             Logger.debug("new-trans", "skipping syncData() loop");
484             return false;
485         }
486
487         syncingData = true;
488
489         try {
490             switch (item.getType()) {
491                 case generalData:
492                     item.setDate(String.valueOf(tvDate.getText()));
493                     item.setDescription(String.valueOf(tvDescription.getText()));
494                     break;
495                 case transactionRow:
496                     final LedgerTransactionAccount account = item.getAccount();
497                     account.setAccountName(String.valueOf(tvAccount.getText()));
498
499                     item.setComment(String.valueOf(tvComment.getText()));
500
501                     String amount = String.valueOf(tvAmount.getText());
502                     amount = amount.trim();
503
504                     if (amount.isEmpty()) {
505                         account.resetAmount();
506 //                        account.setCurrency(null);
507                     }
508                     else {
509                         try {
510                             amount = amount.replace(decimalSeparator, decimalDot);
511                             account.setAmount(Float.parseFloat(amount));
512                         }
513                         catch (NumberFormatException e) {
514                             Logger.debug("new-trans", String.format(
515                                     "assuming amount is not set due to number format exception. " +
516                                     "input was '%s'", amount));
517                             account.resetAmount();
518                         }
519                         final String curr = String.valueOf(tvCurrency.getText());
520                         if (curr.equals(tvCurrency.getContext()
521                                                   .getResources()
522                                                   .getString(R.string.currency_symbol)) ||
523                             curr.isEmpty())
524                             account.setCurrency(null);
525                         else
526                             account.setCurrency(curr);
527                     }
528
529                     break;
530                 case bottomFiller:
531                     throw new RuntimeException("Should not happen");
532             }
533
534             return true;
535         }
536         finally {
537             syncingData = false;
538         }
539     }
540     private void pickTransactionDate() {
541         DatePickerFragment picker = new DatePickerFragment();
542         picker.setFutureDates(mProfile.getFutureDates());
543         picker.setOnDatePickedListener(this);
544         picker.show(((NewTransactionActivity) tvDate.getContext()).getSupportFragmentManager(),
545                 "datePicker");
546     }
547     /**
548      * setData
549      *
550      * @param item updates the UI elements with the data from the model item
551      */
552     @SuppressLint("DefaultLocale")
553     public void setData(NewTransactionModel.Item item) {
554         beginUpdates();
555         try {
556             if (this.item != null && !this.item.equals(item)) {
557                 this.item.stopObservingDate(dateObserver);
558                 this.item.stopObservingDescription(descriptionObserver);
559                 this.item.stopObservingAmountHint(hintObserver);
560                 this.item.stopObservingEditableFlag(editableObserver);
561                 this.item.stopObservingCommentVisible(commentVisibleObserver);
562                 this.item.stopObservingComment(commentObserver);
563                 this.item.getModel()
564                          .stopObservingFocusedItem(focusedAccountObserver);
565                 this.item.getModel()
566                          .stopObservingAccountCount(accountCountObserver);
567                 Data.currencySymbolPosition.removeObserver(currencyPositionObserver);
568                 Data.currencyGap.removeObserver(currencyGapObserver);
569                 Data.locale.removeObserver(localeObserver);
570                 this.item.stopObservingCurrency(currencyObserver);
571                 this.item.getModel().showCurrency.removeObserver(showCurrencyObserver);
572
573                 this.item = null;
574             }
575
576             switch (item.getType()) {
577                 case generalData:
578                     tvDate.setText(item.getFormattedDate());
579                     tvDescription.setText(item.getDescription());
580                     lHead.setVisibility(View.VISIBLE);
581                     lAccount.setVisibility(View.GONE);
582                     lPadding.setVisibility(View.GONE);
583                     setEditable(true);
584                     break;
585                 case transactionRow:
586                     LedgerTransactionAccount acc = item.getAccount();
587                     tvAccount.setText(acc.getAccountName());
588                     tvComment.setText(acc.getComment());
589                     if (acc.isAmountSet()) {
590                         tvAmount.setText(String.format("%1.2f", acc.getAmount()));
591                     }
592                     else {
593                         tvAmount.setText("");
594 //                        tvAmount.setHint(R.string.zero_amount);
595                     }
596                     tvAmount.setHint(item.getAmountHint());
597                     setCurrencyString(acc.getCurrency());
598                     lHead.setVisibility(View.GONE);
599                     lAccount.setVisibility(View.VISIBLE);
600                     lPadding.setVisibility(View.GONE);
601                     setEditable(true);
602                     break;
603                 case bottomFiller:
604                     lHead.setVisibility(View.GONE);
605                     lAccount.setVisibility(View.GONE);
606                     lPadding.setVisibility(View.VISIBLE);
607                     setEditable(false);
608                     break;
609             }
610             if (this.item == null) { // was null or has changed
611                 this.item = item;
612                 final NewTransactionActivity activity =
613                         (NewTransactionActivity) tvDescription.getContext();
614
615                 if (!item.isOfType(ItemType.bottomFiller)) {
616                     item.observeEditableFlag(activity, editableObserver);
617                     item.getModel()
618                         .observeFocusedItem(activity, focusedAccountObserver);
619                 }
620                 switch (item.getType()) {
621                     case generalData:
622                         item.observeDate(activity, dateObserver);
623                         item.observeDescription(activity, descriptionObserver);
624                         break;
625                     case transactionRow:
626                         item.observeAmountHint(activity, hintObserver);
627                         item.observeCommentVisible(activity, commentVisibleObserver);
628                         item.observeComment(activity, commentObserver);
629                         Data.currencySymbolPosition.observe(activity, currencyPositionObserver);
630                         Data.currencyGap.observe(activity, currencyGapObserver);
631                         Data.locale.observe(activity, localeObserver);
632                         item.observeCurrency(activity, currencyObserver);
633                         item.getModel().showCurrency.observe(activity, showCurrencyObserver);
634                         item.getModel()
635                             .observeAccountCount(activity, accountCountObserver);
636                         break;
637                 }
638             }
639         }
640         finally {
641             endUpdates();
642         }
643     }
644     @Override
645     public void onDatePicked(int year, int month, int day) {
646         final Calendar c = GregorianCalendar.getInstance();
647         c.set(year, month, day);
648         item.setDate(c.getTime());
649         boolean focused = tvDescription.requestFocus();
650         if (focused)
651             Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
652
653     }
654     @Override
655     public void onCurrencySelected(Currency currency) {
656         adapter.model.setItemCurrency(this.item, currency, adapter);
657     }
658     @Override
659     public void descriptionSelected(String description) {
660         tvAccount.setText(description);
661         tvAmount.requestFocus(View.FOCUS_FORWARD);
662     }
663 }