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