+
+ currencyObserver = currency -> {
+ setCurrency(currency);
+ adapter.checkTransactionSubmittable();
+ };
+
+ currencyGapObserver =
+ hasGap -> updateCurrencyPositionAndPadding(Data.currencySymbolPosition.getValue(),
+ hasGap);
+
+ currencyPositionObserver =
+ position -> updateCurrencyPositionAndPadding(position, Data.currencyGap.getValue());
+
+ showCurrencyObserver = showCurrency -> {
+ if (showCurrency) {
+ tvCurrency.setVisibility(View.VISIBLE);
+ tvCurrencyButton.setVisibility(View.VISIBLE);
+ String defaultCommodity = mProfile.getDefaultCommodity();
+ item.setCurrency(
+ (defaultCommodity == null) ? null : Currency.loadByName(defaultCommodity));
+ }
+ else {
+ tvCurrency.setVisibility(View.GONE);
+ tvCurrencyButton.setVisibility(View.GONE);
+ item.setCurrency(null);
+ }
+ };
+
+ commentObserver = comment -> {
+ final View focusedView = tvComment.findFocus();
+ tvComment.setTypeface(null,
+ (focusedView == tvComment) ? Typeface.NORMAL : Typeface.ITALIC);
+ tvComment.setVisibility(
+ ((focusedView != tvComment) && TextUtils.isEmpty(comment)) ? View.INVISIBLE
+ : View.VISIBLE);
+ };
+
+ showCommentsObserver = show -> {
+ final View amountLayout = itemView.findViewById(R.id.amount_layout);
+ ConstraintLayout.LayoutParams amountLayoutParams =
+ (ConstraintLayout.LayoutParams) amountLayout.getLayoutParams();
+ ConstraintLayout.LayoutParams accountParams =
+ (ConstraintLayout.LayoutParams) tvAccount.getLayoutParams();
+ if (show) {
+ accountParams.endToStart = ConstraintLayout.LayoutParams.UNSET;
+ accountParams.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
+
+ amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.UNSET;
+ amountLayoutParams.topToBottom = tvAccount.getId();
+
+ commentLayout.setVisibility(View.VISIBLE);
+ }
+ else {
+ accountParams.endToStart = amountLayout.getId();
+ accountParams.endToEnd = ConstraintLayout.LayoutParams.UNSET;
+
+ amountLayoutParams.topToBottom = ConstraintLayout.LayoutParams.UNSET;
+ amountLayoutParams.topToTop = ConstraintLayout.LayoutParams.PARENT_ID;
+
+ commentLayout.setVisibility(View.GONE);
+ }
+
+ tvAccount.setLayoutParams(accountParams);
+ amountLayout.setLayoutParams(amountLayoutParams);
+
+ transactionCommentLayout.setVisibility(show ? View.VISIBLE : View.GONE);
+ };
+
+ amountValidityObserver = valid -> {
+ tvAmount.setCompoundDrawablesRelativeWithIntrinsicBounds(
+ valid ? 0 : R.drawable.ic_error_outline_black_24dp, 0, 0, 0);
+ tvAmount.setMinEms(valid ? 4 : 5);
+ };
+ }
+ private void commentFocusChanged(TextView textView, boolean hasFocus) {
+ @ColorInt int textColor;
+ textColor = tvDummy.getTextColors()
+ .getDefaultColor();
+ if (hasFocus) {
+ textView.setTypeface(null, Typeface.NORMAL);
+ textView.setHint(R.string.transaction_account_comment_hint);
+ }
+ else {
+ int alpha = (textColor >> 24 & 0xff);
+ alpha = 3 * alpha / 4;
+ textColor = (alpha << 24) | (0x00ffffff & textColor);
+ textView.setTypeface(null, Typeface.ITALIC);
+ textView.setHint("");
+ if (TextUtils.isEmpty(textView.getText())) {
+ textView.setVisibility(View.INVISIBLE);
+ }
+ }
+ textView.setTextColor(textColor);
+
+ }
+ private void updateCurrencyPositionAndPadding(Currency.Position position, boolean hasGap) {
+ ConstraintLayout.LayoutParams amountLP =
+ (ConstraintLayout.LayoutParams) tvAmount.getLayoutParams();
+ ConstraintLayout.LayoutParams currencyLP =
+ (ConstraintLayout.LayoutParams) tvCurrency.getLayoutParams();
+
+ if (position == Currency.Position.before) {
+ currencyLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
+ currencyLP.endToEnd = ConstraintLayout.LayoutParams.UNSET;
+
+ amountLP.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
+ amountLP.endToStart = ConstraintLayout.LayoutParams.UNSET;
+ amountLP.startToStart = ConstraintLayout.LayoutParams.UNSET;
+ amountLP.startToEnd = tvCurrency.getId();
+
+ tvCurrency.setGravity(Gravity.END);
+ }
+ else {
+ currencyLP.startToStart = ConstraintLayout.LayoutParams.UNSET;
+ currencyLP.endToEnd = ConstraintLayout.LayoutParams.PARENT_ID;
+
+ amountLP.startToStart = ConstraintLayout.LayoutParams.PARENT_ID;
+ amountLP.startToEnd = ConstraintLayout.LayoutParams.UNSET;
+ amountLP.endToEnd = ConstraintLayout.LayoutParams.UNSET;
+ amountLP.endToStart = tvCurrency.getId();
+
+ tvCurrency.setGravity(Gravity.START);
+ }
+
+ amountLP.resolveLayoutDirection(tvAmount.getLayoutDirection());
+ currencyLP.resolveLayoutDirection(tvCurrency.getLayoutDirection());
+
+ tvAmount.setLayoutParams(amountLP);
+ tvCurrency.setLayoutParams(currencyLP);
+
+ // distance between the amount and the currency symbol
+ int gapSize = DimensionUtils.sp2px(tvCurrency.getContext(), 5);
+
+ if (position == Currency.Position.before) {
+ tvCurrency.setPaddingRelative(0, 0, hasGap ? gapSize : 0, 0);
+ }
+ else {
+ tvCurrency.setPaddingRelative(hasGap ? gapSize : 0, 0, 0, 0);
+ }
+ }
+ private void setCurrencyString(String currency) {
+ @ColorInt int textColor = tvDummy.getTextColors()
+ .getDefaultColor();
+ if ((currency == null) || currency.isEmpty()) {
+ tvCurrency.setText(R.string.currency_symbol);
+ int alpha = (textColor >> 24) & 0xff;
+ alpha = alpha * 3 / 4;
+ tvCurrency.setTextColor((alpha << 24) | (0x00ffffff & textColor));
+ }
+ else {
+ tvCurrency.setText(currency);
+ tvCurrency.setTextColor(textColor);
+ }
+ }
+ private void setCurrency(Currency currency) {
+ setCurrencyString((currency == null) ? null : currency.getName());