]> git.ktnx.net Git - mobile-ledger.git/blob - app/src/main/java/net/ktnx/mobileledger/ui/activity/NewTransactionItemHolder.java
add progress while retrieving previous transaction
[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.requestFocus();
316                                     break;
317                                 case Account:
318                                     focused = tvAccount.requestFocus();
319                                     tvAccount.dismissDropDown();
320                                     if (focused)
321                                         Misc.showSoftKeyboard(
322                                                 (NewTransactionActivity) tvAccount.getContext());
323                                     break;
324                             }
325                         }
326
327                         break;
328                 }
329             }
330         };
331         accountCountObserver = count -> {
332             final int adapterPosition = getAdapterPosition();
333             final int layoutPosition = getLayoutPosition();
334             Logger.debug("holder",
335                     String.format(Locale.US, "count=%d; pos=%d, layoutPos=%d [%s]", count,
336                             adapterPosition, layoutPosition, item.getType()
337                                                                  .toString()
338                                                                  .concat(item.getType() ==
339                                                                          ItemType.transactionRow
340                                                                          ? String.format(Locale.US,
341                                                                          "'%s'=%s",
342                                                                          item.getAccount()
343                                                                              .getAccountName(),
344                                                                          item.getAccount()
345                                                                              .isAmountSet()
346                                                                          ? String.format(Locale.US,
347                                                                                  "%.2f",
348                                                                                  item.getAccount()
349                                                                                      .getAmount())
350                                                                          : "unset") : "")));
351             if (adapterPosition == count)
352                 tvAmount.setImeOptions(EditorInfo.IME_ACTION_DONE);
353             else
354                 tvAmount.setImeOptions(EditorInfo.IME_ACTION_NEXT);
355         };
356
357         localeObserver = locale -> {
358             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
359                 tvAmount.setKeyListener(DigitsKeyListener.getInstance(locale, true, true));
360         };
361
362         currencyObserver = currency -> {
363             setCurrency(currency);
364             adapter.checkTransactionSubmittable();
365         };
366
367         currencyGapObserver =
368                 hasGap -> updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(),
369                         hasGap);
370
371         currencyPositionObserver =
372                 position -> updateCurrencyPositionAndPadding(position, Data.currencyGap.getValue());
373
374         showCurrencyObserver = showCurrency -> {
375             if (showCurrency) {
376                 tvCurrency.setVisibility(View.VISIBLE);
377             }
378             else {
379                 tvCurrency.setVisibility(View.GONE);
380                 item.setCurrency(null);
381             }
382         };
383
384         commentObserver = comment -> {
385             final View focusedView = tvComment.findFocus();
386             tvComment.setTypeface(null,
387                     (focusedView == tvComment) ? Typeface.NORMAL : Typeface.ITALIC);
388             tvComment.setVisibility(
389                     ((focusedView != tvComment) && Misc.isEmptyOrNull(comment)) ? View.INVISIBLE
390                                                                                 : View.VISIBLE);
391         };
392
393         busyFlagObserver = isBusy -> {p.setVisibility(isBusy ? View.VISIBLE : View.INVISIBLE);};
394     }
395     private void updateCurrencyPositionAndPadding(Currency.Position position, boolean hasGap) {
396         ConstraintLayout.LayoutParams amountLP =
397                 (ConstraintLayout.LayoutParams) tvAmount.getLayoutParams();
398         ConstraintLayout.LayoutParams currencyLP =
399                 (ConstraintLayout.LayoutParams) tvCurrency.getLayoutParams();
400
401         if (position == Currency.Position.before) {
402             currencyLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
403             currencyLP.endToEnd = ConstraintLayout.LayoutParams.UNSET;
404
405             amountLP.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
406             amountLP.endToStart = ConstraintLayout.LayoutParams.UNSET;
407             amountLP.startToStart = ConstraintLayout.LayoutParams.UNSET;
408             amountLP.startToEnd = tvCurrency.getId();
409
410             tvCurrency.setGravity(Gravity.END);
411         }
412         else {
413             currencyLP.startToStart = ConstraintLayout.LayoutParams.UNSET;
414             currencyLP.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
415
416             amountLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
417             amountLP.startToEnd = ConstraintLayout.LayoutParams.UNSET;
418             amountLP.endToEnd = ConstraintLayout.LayoutParams.UNSET;
419             amountLP.endToStart = tvCurrency.getId();
420
421             tvCurrency.setGravity(Gravity.START);
422         }
423
424         amountLP.resolveLayoutDirection(tvAmount.getLayoutDirection());
425         currencyLP.resolveLayoutDirection(tvCurrency.getLayoutDirection());
426
427         tvAmount.setLayoutParams(amountLP);
428         tvCurrency.setLayoutParams(currencyLP);
429
430         // distance between the amount and the currency symbol
431         int gapSize = DimensionUtils.sp2px(tvCurrency.getContext(), 5);
432
433         if (position == Currency.Position.before) {
434             tvCurrency.setPaddingRelative(0, 0, hasGap ? gapSize : 0, 0);
435         }
436         else {
437             tvCurrency.setPaddingRelative(hasGap ? gapSize : 0, 0, 0, 0);
438         }
439     }
440     private void setCurrencyString(String currency) {
441         if ((currency == null) || currency.isEmpty()) {
442             tvCurrency.setText(R.string.currency_symbol);
443             tvCurrency.setTextColor(0x7f000000 + (0x00ffffff & Colors.defaultTextColor));
444         }
445         else {
446             tvCurrency.setText(currency);
447             tvCurrency.setTextColor(Colors.defaultTextColor);
448         }
449     }
450     private void setCurrency(Currency currency) {
451         setCurrencyString((currency == null) ? null : currency.getName());
452     }
453     private void setEditable(Boolean editable) {
454         tvDate.setEnabled(editable);
455         tvDescription.setEnabled(editable);
456         tvAccount.setEnabled(editable);
457         tvAmount.setEnabled(editable);
458     }
459     private void setCommentVisible(@NotNull Boolean visible) {
460         if (visible) {
461             // showing; show the comment view and align the comment button to it
462             tvComment.setVisibility(View.VISIBLE);
463             tvComment.requestFocus();
464             ConstraintLayout.LayoutParams lp =
465                     (ConstraintLayout.LayoutParams) commentButton.getLayoutParams();
466             lp.bottomToBottom = R.id.comment;
467
468             commentButton.setLayoutParams(lp);
469         }
470         else {
471             // hiding; hide the comment view and align the comment bottom to the amount
472             tvComment.setVisibility(View.GONE);
473             ConstraintLayout.LayoutParams lp =
474                     (ConstraintLayout.LayoutParams) commentButton.getLayoutParams();
475             lp.bottomToBottom = R.id.amount_layout;   // R.id.parent doesn't work here
476
477             commentButton.setLayoutParams(lp);
478         }
479     }
480     private void beginUpdates() {
481         if (inUpdate)
482             throw new RuntimeException("Already in update mode");
483         inUpdate = true;
484     }
485     private void endUpdates() {
486         if (!inUpdate)
487             throw new RuntimeException("Not in update mode");
488         inUpdate = false;
489     }
490     /**
491      * syncData()
492      * <p>
493      * Stores the data from the UI elements into the model item
494      * Returns true if there were changes made that suggest transaction has to be
495      * checked for being submittable
496      */
497     private boolean syncData() {
498         if (item == null)
499             return false;
500
501         if (syncingData) {
502             Logger.debug("new-trans", "skipping syncData() loop");
503             return false;
504         }
505
506         syncingData = true;
507
508         try {
509             switch (item.getType()) {
510                 case generalData:
511                     item.setDate(String.valueOf(tvDate.getText()));
512                     item.setDescription(String.valueOf(tvDescription.getText()));
513                     break;
514                 case transactionRow:
515                     final LedgerTransactionAccount account = item.getAccount();
516                     account.setAccountName(String.valueOf(tvAccount.getText()));
517
518                     item.setComment(String.valueOf(tvComment.getText()));
519
520                     String amount = String.valueOf(tvAmount.getText());
521                     amount = amount.trim();
522
523                     if (amount.isEmpty()) {
524                         account.resetAmount();
525 //                        account.setCurrency(null);
526                     }
527                     else {
528                         try {
529                             amount = amount.replace(decimalSeparator, decimalDot);
530                             account.setAmount(Float.parseFloat(amount));
531                         }
532                         catch (NumberFormatException e) {
533                             Logger.debug("new-trans", String.format(
534                                     "assuming amount is not set due to number format exception. " +
535                                     "input was '%s'", amount));
536                             account.resetAmount();
537                         }
538                         final String curr = String.valueOf(tvCurrency.getText());
539                         if (curr.equals(tvCurrency.getContext()
540                                                   .getResources()
541                                                   .getString(R.string.currency_symbol)) ||
542                             curr.isEmpty())
543                             account.setCurrency(null);
544                         else
545                             account.setCurrency(curr);
546                     }
547
548                     break;
549                 case bottomFiller:
550                     throw new RuntimeException("Should not happen");
551             }
552
553             return true;
554         }
555         finally {
556             syncingData = false;
557         }
558     }
559     private void pickTransactionDate() {
560         DatePickerFragment picker = new DatePickerFragment();
561         picker.setFutureDates(mProfile.getFutureDates());
562         picker.setOnDatePickedListener(this);
563         picker.show(((NewTransactionActivity) tvDate.getContext()).getSupportFragmentManager(),
564                 "datePicker");
565     }
566     /**
567      * setData
568      *
569      * @param item updates the UI elements with the data from the model item
570      */
571     @SuppressLint("DefaultLocale")
572     public void setData(NewTransactionModel.Item item) {
573         beginUpdates();
574         try {
575             if (this.item != null && !this.item.equals(item)) {
576                 this.item.stopObservingDate(dateObserver);
577                 this.item.stopObservingDescription(descriptionObserver);
578                 this.item.stopObservingAmountHint(hintObserver);
579                 this.item.stopObservingEditableFlag(editableObserver);
580                 this.item.getModel()
581                          .stopObservingFocusedItem(focusedAccountObserver);
582                 this.item.getModel()
583                          .stopObservingAccountCount(accountCountObserver);
584                 Data.currencySymbolPosition.removeObserver(currencyPositionObserver);
585                 Data.currencyGap.removeObserver(currencyGapObserver);
586                 Data.locale.removeObserver(localeObserver);
587                 this.item.stopObservingCurrency(currencyObserver);
588                 this.item.getModel().showCurrency.removeObserver(showCurrencyObserver);
589                 this.item.stopObservingComment(commentObserver);
590                 this.item.getModel().stopObservingBusyFlag(busyFlagObserver);
591
592                 this.item = null;
593             }
594
595             switch (item.getType()) {
596                 case generalData:
597                     tvDate.setText(item.getFormattedDate());
598                     tvDescription.setText(item.getDescription());
599                     lHead.setVisibility(View.VISIBLE);
600                     lAccount.setVisibility(View.GONE);
601                     lPadding.setVisibility(View.GONE);
602                     setEditable(true);
603                     break;
604                 case transactionRow:
605                     LedgerTransactionAccount acc = item.getAccount();
606                     tvAccount.setText(acc.getAccountName());
607                     tvComment.setText(acc.getComment());
608                     if (acc.isAmountSet()) {
609                         tvAmount.setText(String.format("%1.2f", acc.getAmount()));
610                     }
611                     else {
612                         tvAmount.setText("");
613 //                        tvAmount.setHint(R.string.zero_amount);
614                     }
615                     tvAmount.setHint(item.getAmountHint());
616                     setCurrencyString(acc.getCurrency());
617                     lHead.setVisibility(View.GONE);
618                     lAccount.setVisibility(View.VISIBLE);
619                     lPadding.setVisibility(View.GONE);
620                     setEditable(true);
621                     break;
622                 case bottomFiller:
623                     lHead.setVisibility(View.GONE);
624                     lAccount.setVisibility(View.GONE);
625                     lPadding.setVisibility(View.VISIBLE);
626                     setEditable(false);
627                     break;
628             }
629             if (this.item == null) { // was null or has changed
630                 this.item = item;
631                 final NewTransactionActivity activity =
632                         (NewTransactionActivity) tvDescription.getContext();
633
634                 if (!item.isOfType(ItemType.bottomFiller)) {
635                     item.observeEditableFlag(activity, editableObserver);
636                     item.getModel()
637                         .observeFocusedItem(activity, focusedAccountObserver);
638                 }
639                 switch (item.getType()) {
640                     case generalData:
641                         item.observeDate(activity, dateObserver);
642                         item.observeDescription(activity, descriptionObserver);
643                         item.getModel().observeBusyFlag(activity, busyFlagObserver);
644                         break;
645                     case transactionRow:
646                         item.observeAmountHint(activity, hintObserver);
647                         Data.currencySymbolPosition.observe(activity, currencyPositionObserver);
648                         Data.currencyGap.observe(activity, currencyGapObserver);
649                         Data.locale.observe(activity, localeObserver);
650                         item.observeCurrency(activity, currencyObserver);
651                         item.getModel().showCurrency.observe(activity, showCurrencyObserver);
652                         item.observeComment(activity, commentObserver);
653                         item.getModel()
654                             .observeAccountCount(activity, accountCountObserver);
655                         break;
656                 }
657             }
658         }
659         finally {
660             endUpdates();
661         }
662     }
663     @Override
664     public void onDatePicked(int year, int month, int day) {
665         final Calendar c = GregorianCalendar.getInstance();
666         c.set(year, month, day);
667         item.setDate(c.getTime());
668         boolean focused = tvDescription.requestFocus();
669         if (focused)
670             Misc.showSoftKeyboard((NewTransactionActivity) tvAccount.getContext());
671
672     }
673     @Override
674     public void descriptionSelected(String description) {
675         tvAccount.setText(description);
676         tvAmount.requestFocus(View.FOCUS_FORWARD);
677     }
678 }