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