/** Decide if should show errors in the UI. */
  private boolean shouldShowErrors(boolean isTyping, Value amount) {
    if (amount != null && !amount.isZero() && !isAmountWithinLimits(amount)) {
      return true;
    }

    if (isTyping) return false;
    if (amountCalculatorLink.isEmpty()) return false;
    if (amount != null && amount.isZero()) return false;

    return true;
  }
 /** Get the lowest deposit or withdraw for the provided amount type */
 private Value getLowestAmount(ValueType type) {
   Value min = type.minNonDust();
   if (marketInfo != null) {
     if (marketInfo.minimum.isOfType(min)) {
       min = Value.max(marketInfo.minimum, min);
     } else if (marketInfo.rate.canConvert(type, marketInfo.minimum.type)) {
       min = Value.max(marketInfo.rate.convert(marketInfo.minimum), min);
     }
   }
   return min;
 }
  /**
   * Check if amount is within the minimum and maximum deposit limits and if is dust or if is more
   * money than currently in the wallet
   */
  private boolean isAmountWithinLimits(Value amount) {
    boolean isWithinLimits = amount != null && amount.isPositive() && !amount.isDust();

    // Check if within min & max deposit limits
    if (isWithinLimits && marketInfo != null && canCompare(marketInfo.limit, amount)) {
      isWithinLimits = amount.within(marketInfo.minimum, marketInfo.limit);
    }

    // Check if we have the amount
    if (isWithinLimits && canCompare(lastBalance, amount)) {
      isWithinLimits = amount.compareTo(lastBalance) <= 0;
    }

    return isWithinLimits;
  }
  private void validateAmount(boolean isTyping) {
    Value amountParsed = amountCalculatorLink.getPrimaryAmount();

    if (isAmountValid(amountParsed)) {
      sendAmount = amountParsed;
      amountError.setVisibility(View.GONE);
      // Show warning that fees apply when entered the full amount inside the pocket
      if (canCompare(sendAmount, lastBalance) && sendAmount.compareTo(lastBalance) == 0) {
        amountWarning.setText(R.string.amount_warn_fees_apply);
        amountWarning.setVisibility(View.VISIBLE);
      } else {
        amountWarning.setVisibility(View.GONE);
      }
    } else {
      amountWarning.setVisibility(View.GONE);
      // ignore printing errors for null and zero amounts
      if (shouldShowErrors(isTyping, amountParsed)) {
        sendAmount = null;
        if (amountParsed == null) {
          amountError.setText(R.string.amount_error);
        } else if (amountParsed.isNegative()) {
          amountError.setText(R.string.amount_error_negative);
        } else if (!isAmountWithinLimits(amountParsed)) {
          String message = getString(R.string.error_generic);
          // If the amount is dust or lower than the deposit limit
          if (isAmountTooSmall(amountParsed)) {
            Value minAmount = getLowestAmount(amountParsed.type);
            message = getString(R.string.amount_error_too_small, minAmount.toFriendlyString());
          } else {
            // If we have the amount
            if (canCompare(lastBalance, amountParsed) && amountParsed.compareTo(lastBalance) > 0) {
              message =
                  getString(R.string.amount_error_not_enough_money, lastBalance.toFriendlyString());
            }

            if (marketInfo != null
                && canCompare(marketInfo.limit, amountParsed)
                && amountParsed.compareTo(marketInfo.limit) > 0) {
              message =
                  getString(R.string.trade_error_max_limit, marketInfo.limit.toFriendlyString());
            }
          }
          amountError.setText(message);
        } else { // Should not happen, but show a generic error
          amountError.setText(R.string.amount_error);
        }
        amountError.setVisibility(View.VISIBLE);
      } else {
        amountError.setVisibility(View.GONE);
      }
    }
    updateView();
  }
  private void setAmountForEmptyWallet() {
    updateBalance();
    if (state != State.INPUT || pocket == null || lastBalance == null) return;

    if (lastBalance.isZero()) {
      String message = getResources().getString(R.string.amount_error_not_enough_money_plain);
      Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
    } else {
      amountCalculatorLink.setPrimaryAmount(lastBalance);
      validateAmount();
    }
  }
  public void onMakeTransaction(Address toAddress, Value amount, @Nullable TxMessage txMessage) {
    Intent intent = new Intent(getActivity(), SignTransactionActivity.class);

    // Decide if emptying wallet or not
    if (canCompare(lastBalance, amount) && amount.compareTo(lastBalance) == 0) {
      intent.putExtra(Constants.ARG_EMPTY_WALLET, true);
    } else {
      intent.putExtra(Constants.ARG_SEND_VALUE, amount);
    }
    intent.putExtra(Constants.ARG_ACCOUNT_ID, pocket.getId());
    intent.putExtra(Constants.ARG_SEND_TO_ADDRESS, toAddress);
    if (txMessage != null) intent.putExtra(Constants.ARG_TX_MESSAGE, txMessage);

    startActivityForResult(intent, SIGN_TRANSACTION);
    state = State.INPUT;
  }
 /** Check if amount is smaller than the dust limit or if applicable, the minimum deposit. */
 private boolean isAmountTooSmall(Value amount) {
   return amount.compareTo(getLowestAmount(amount.type)) < 0;
 }