Skip to content

Commit

Permalink
When a token is added, amount field gains focus when applicable. Als…
Browse files Browse the repository at this point in the history
…o hide set full amount button when not applicable #51
  • Loading branch information
MrStahlfelge committed Dec 12, 2021
1 parent e9153ee commit 0dbf43d
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 21 deletions.
Expand Up @@ -253,8 +253,7 @@ class SendFundsFragment : AbstractAuthenticationFragment(), PasswordDialogCallba
this.removeAllViews()
tokensChosen.forEach {
val ergoId = it.key
tokensAvail.filter { it.tokenId.equals(ergoId) }
.firstOrNull()?.let { tokenDbEntity ->
tokensAvail.firstOrNull { it.tokenId.equals(ergoId) }?.let { tokenDbEntity ->
val itemBinding =
FragmentSendFundsTokenItemBinding.inflate(layoutInflater, this, true)
itemBinding.tvTokenName.text = tokenDbEntity.name
Expand All @@ -270,7 +269,7 @@ class SendFundsFragment : AbstractAuthenticationFragment(), PasswordDialogCallba
if (tokenDbEntity.decimals > 0) InputType.TYPE_CLASS_NUMBER or InputType.TYPE_NUMBER_FLAG_DECIMAL
else InputType.TYPE_CLASS_NUMBER
itemBinding.inputTokenAmount.addTextChangedListener(
TokenAmountWatcher(tokenDbEntity)
TokenAmountWatcher(tokenDbEntity, itemBinding)
)
itemBinding.inputTokenAmount.setText(
viewModel.uiLogic.tokenAmountToText(
Expand All @@ -285,6 +284,7 @@ class SendFundsFragment : AbstractAuthenticationFragment(), PasswordDialogCallba
tokenDbEntity.decimals
)
)
itemBinding.buttonTokenAll.visibility = View.INVISIBLE
}
}

Expand All @@ -293,13 +293,22 @@ class SendFundsFragment : AbstractAuthenticationFragment(), PasswordDialogCallba
viewModel.uiLogic.removeToken(ergoId)
} else {
itemBinding.inputTokenAmount.text = null
itemBinding.inputTokenAmount.requestFocus()
}
}
}
}

setFocusToEmptyTokenAmountInput()
}
}

private fun setFocusToEmptyTokenAmountInput() {
binding.tokensList.descendants.firstOrNull {
it is EditText && it.text.isEmpty() && it.isEnabled && it.visibility == View.VISIBLE
}?.requestFocus()
}

private fun setAmountEdittext(amountToSend: ErgoAmount) {
binding.amount.editText?.setText(amountToSend.toStringTrimTrailingZeros())
}
Expand All @@ -317,9 +326,7 @@ class SendFundsFragment : AbstractAuthenticationFragment(), PasswordDialogCallba
}
if (checkResponse.tokenError) {
binding.labelTokenAmountError.visibility = View.VISIBLE
binding.tokensList.descendants.filter { it is EditText && it.text.isEmpty() }
.firstOrNull()
?.requestFocus()
setFocusToEmptyTokenAmountInput()
}

if (checkResponse.canPay) {
Expand Down Expand Up @@ -414,7 +421,10 @@ class SendFundsFragment : AbstractAuthenticationFragment(), PasswordDialogCallba

}

inner class TokenAmountWatcher(private val token: WalletToken) : TextWatcher {
inner class TokenAmountWatcher(
private val token: WalletToken,
private val itemBinding: FragmentSendFundsTokenItemBinding
) : TextWatcher {
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

}
Expand All @@ -429,6 +439,7 @@ class SendFundsFragment : AbstractAuthenticationFragment(), PasswordDialogCallba
s?.toString()?.toTokenAmount(token.decimals) ?: TokenAmount(0, token.decimals)
)
binding.labelTokenAmountError.visibility = View.GONE
itemBinding.buttonTokenAll.visibility = View.VISIBLE
}

}
Expand Down
26 changes: 14 additions & 12 deletions ios/src/main/java/org/ergoplatform/ios/tokens/SendTokenEntryView.kt
Expand Up @@ -15,7 +15,7 @@ import org.robovm.apple.uikit.*
*/
class SendTokenEntryView(
val uiLogic: SendFundsUiLogic, private val amountErrorField: UIView,
val token: WalletToken, ergoToken: ErgoToken
private val token: WalletToken, ergoToken: ErgoToken
) :
UIStackView(CGRect.Zero()) {

Expand Down Expand Up @@ -60,13 +60,11 @@ class SendTokenEntryView(
if (!isSingular) {
isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer {
token.let { token ->
inputTokenVal.text = uiLogic.tokenAmountToText(
token.amount!!,
token.decimals
)
amountChanged()
}
inputTokenVal.text = uiLogic.tokenAmountToText(
token.amount!!,
token.decimals
)
amountChanged()
})
}
}
Expand Down Expand Up @@ -99,7 +97,11 @@ class SendTokenEntryView(
inputTokenVal.keyboardType =
if (token.decimals > 0) UIKeyboardType.NumbersAndPunctuation else UIKeyboardType.NumberPad
inputTokenVal.text = uiLogic.tokenAmountToText(amountChosen, token.decimals)
maxAmountImageView.alpha = if (isSingular) 0.0 else 1.0
setMaxAmountImageViewVisibility(ergoToken.value)
}

private fun setMaxAmountImageViewVisibility(currentRawAmount: Long) {
maxAmountImageView.alpha = if (isSingular || currentRawAmount == token.amount) 0.0 else 1.0
}

private fun removeTokenClicked() {
Expand All @@ -110,20 +112,20 @@ class SendTokenEntryView(
} else {
inputTokenVal.text = ""
amountChanged()
inputTokenVal.becomeFirstResponder()
}
}

private fun amountChanged() {
val amount = getInputAmount()
uiLogic.setTokenAmount(token.tokenId!!, amount)
amountErrorField.setHiddenAnimated(true)
setMaxAmountImageViewVisibility(amount.rawValue)
}

private fun getInputAmount(): TokenAmount {
val amountString = inputTokenVal.text
val amount =
amountString.toTokenAmount(token.decimals) ?: TokenAmount(0, token.decimals)
return amount
return amountString.toTokenAmount(token.decimals) ?: TokenAmount(0, token.decimals)
}

fun hasAmount(): Boolean {
Expand Down
Expand Up @@ -109,6 +109,7 @@ class ChooseTokenListViewController(
numberOfLines = 1
textAlignment = NSTextAlignment.Center
lineBreakMode = NSLineBreakMode.TruncatingMiddle
textColor = UIColor.secondaryLabel()
}
contentView.addSubview(nameLabel)
contentView.addSubview(tokenIdLabel)
Expand Down
Expand Up @@ -254,8 +254,7 @@ class SendFundsViewController(
}
if (checkResponse.tokenError) {
tokensError.setHiddenAnimated(false)
(tokensUiList.arrangedSubviews.firstOrNull { (it as? SendTokenEntryView)?.hasAmount() == false }
as? SendTokenEntryView)?.setFocus()
setFocusToEmptyTokenAmountInput()
}

if (checkResponse.canPay) {
Expand All @@ -265,6 +264,11 @@ class SendFundsViewController(
}
}

private fun setFocusToEmptyTokenAmountInput() {
(tokensUiList.arrangedSubviews.firstOrNull { (it as? SendTokenEntryView)?.hasAmount() == false }
as? SendTokenEntryView)?.setFocus()
}

inner class IosSendFundsUiLogic : SendFundsUiLogic() {
var progressViewController: ProgressViewController? = null

Expand Down Expand Up @@ -302,6 +306,7 @@ class SendFundsViewController(
}
}
tokensUiList.isHidden = uiLogic.tokensChosen.isEmpty()
setFocusToEmptyTokenAmountInput()
}

override fun notifyAmountsChanged() {
Expand Down

0 comments on commit 0dbf43d

Please sign in to comment.