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