-
Notifications
You must be signed in to change notification settings - Fork 34
/
SendTokenEntryView.kt
141 lines (117 loc) · 4.56 KB
/
SendTokenEntryView.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package org.ergoplatform.ios.tokens
import org.ergoplatform.TokenAmount
import org.ergoplatform.appkit.ErgoToken
import org.ergoplatform.ios.ui.*
import org.ergoplatform.persistance.WalletToken
import org.ergoplatform.toTokenAmount
import org.ergoplatform.tokens.isSingularToken
import org.ergoplatform.uilogic.transactions.SendFundsUiLogic
import org.robovm.apple.coregraphics.CGRect
import org.robovm.apple.uikit.*
/**
* View template for sending token entry on send funds screen
*/
class SendTokenEntryView(
val uiLogic: SendFundsUiLogic, private val amountErrorField: UIView,
private val token: WalletToken, ergoToken: ErgoToken
) :
UIStackView(CGRect.Zero()) {
private val inputTokenVal: UITextField
private val maxAmountImageView: UIImageView
private val labelTokenName = Body1Label()
private val isSingular: Boolean
private val amountDelegate = object : OnlyNumericInputTextFieldDelegate() {
override fun shouldReturn(textField: UITextField?): Boolean {
resignFirstResponder()
return true
}
}
init {
axis = UILayoutConstraintAxis.Horizontal
spacing = DEFAULT_MARGIN
val amountChosen = ergoToken.value
isSingular = token.isSingularToken() && amountChosen == 1L
inputTokenVal = createTextField().apply {
delegate = amountDelegate
if (!isSingular) {
addOnEditingChangedListener {
amountChanged()
}
}
}
val removeImageView =
UIImageView(getIosSystemImage(IMAGE_CROSS_CIRCLE, UIImageSymbolScale.Small)).apply {
tintColor = UIColor.label()
contentMode = UIViewContentMode.Center
}
maxAmountImageView = UIImageView(getIosSystemImage(IMAGE_FULL_AMOUNT, UIImageSymbolScale.Small)).apply {
tintColor = UIColor.label()
contentMode = UIViewContentMode.Center
if (!isSingular) {
isUserInteractionEnabled = true
addGestureRecognizer(UITapGestureRecognizer {
inputTokenVal.text = uiLogic.tokenAmountToText(
token.amount!!,
token.decimals
)
amountChanged()
})
}
}
addArrangedSubview(maxAmountImageView)
if (!isSingular) {
addArrangedSubview(inputTokenVal)
}
addArrangedSubview(labelTokenName)
addArrangedSubview(removeImageView)
maxAmountImageView.enforceKeepIntrinsicWidth()
inputTokenVal.fixedWidth(80.0)
inputTokenVal.enforceKeepIntrinsicWidth()
removeImageView.enforceKeepIntrinsicWidth()
removeImageView.isUserInteractionEnabled = true
removeImageView.addGestureRecognizer(UITapGestureRecognizer {
removeTokenClicked()
})
labelTokenName.numberOfLines = 1
// never shrink or grow the value, but the name
inputTokenVal.enforceKeepIntrinsicWidth()
amountDelegate.decimals = token.decimals > 0
labelTokenName.text = token.name
inputTokenVal.keyboardType =
if (token.decimals > 0) UIKeyboardType.NumbersAndPunctuation else UIKeyboardType.NumberPad
inputTokenVal.text = uiLogic.tokenAmountToText(amountChosen, token.decimals)
setMaxAmountImageViewVisibility(ergoToken.value)
}
private fun setMaxAmountImageViewVisibility(currentRawAmount: Long) {
maxAmountImageView.alpha = if (isSingular || currentRawAmount == token.amount) 0.0 else 1.0
}
private fun removeTokenClicked() {
if (isSingular || inputTokenVal.text.isBlank()) {
superview.animateLayoutChanges {
uiLogic.removeToken(token.tokenId!!)
}
} 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
return amountString.toTokenAmount(token.decimals) ?: TokenAmount(0, token.decimals)
}
fun hasAmount(): Boolean {
return isSingular || getInputAmount().rawValue > 0
}
fun setFocus() {
if (!isSingular) {
inputTokenVal.becomeFirstResponder()
}
}
}