2020-08-04 11:10:09 +00:00
|
|
|
import QtQuick 2.13
|
|
|
|
import QtQuick.Controls 2.13
|
|
|
|
import QtQuick.Layouts 1.13
|
|
|
|
import QtGraphicalEffects 1.13
|
2021-09-28 15:04:06 +00:00
|
|
|
|
|
|
|
import utils 1.0
|
2021-12-08 21:20:43 +00:00
|
|
|
import shared.stores 1.0
|
2022-07-21 08:40:49 +00:00
|
|
|
import shared.panels 1.0
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2021-11-15 12:36:22 +00:00
|
|
|
|
2021-10-14 09:38:23 +00:00
|
|
|
import "../"
|
2021-10-14 13:09:35 +00:00
|
|
|
import "../panels"
|
2021-10-14 10:12:22 +00:00
|
|
|
import "."
|
2021-10-14 09:38:23 +00:00
|
|
|
|
2020-08-04 11:10:09 +00:00
|
|
|
Item {
|
2021-12-08 21:20:43 +00:00
|
|
|
id: root
|
2022-04-04 11:26:30 +00:00
|
|
|
property string balanceErrorMessage: qsTr("Insufficient balance")
|
|
|
|
property string greaterThanOrEqualTo0ErrorMessage: qsTr("Must be greater than or equal to 0")
|
|
|
|
property string invalidInputErrorMessage: qsTr("This needs to be a number")
|
|
|
|
property string noInputErrorMessage: qsTr("Please enter an amount")
|
2021-12-23 20:46:58 +00:00
|
|
|
property string currentCurrency: "USD"
|
2020-08-13 08:24:51 +00:00
|
|
|
property alias selectedFiatAmount: txtFiatBalance.text
|
|
|
|
property alias selectedAmount: inputAmount.text
|
2020-08-12 09:05:12 +00:00
|
|
|
property var selectedAccount
|
|
|
|
property alias selectedAsset: selectAsset.selectedAsset
|
|
|
|
property var getFiatValue: function () {}
|
|
|
|
property var getCryptoValue: function () {}
|
|
|
|
property bool isDirty: false
|
2020-10-28 07:44:09 +00:00
|
|
|
property bool validateBalance: true
|
2020-09-08 21:22:47 +00:00
|
|
|
property bool isValid: false
|
2021-07-07 18:37:49 +00:00
|
|
|
property string validationError
|
|
|
|
property var formattedInputValue
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
height: inputAmount.height + (inputAmount.validationError ? -16 - inputAmount.validationErrorTopMargin : 0) + txtFiatBalance.height + txtFiatBalance.anchors.topMargin
|
|
|
|
anchors.right: parent.right
|
|
|
|
anchors.left: parent.left
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
function validate(checkDirty) {
|
|
|
|
let isValid = true
|
|
|
|
let error = ""
|
|
|
|
const hasTyped = checkDirty ? isDirty : true
|
|
|
|
const balance = parseFloat(txtBalance.text || "0.00")
|
2021-07-07 18:37:49 +00:00
|
|
|
formattedInputValue = parseFloat(inputAmount.text || "0.00")
|
2020-08-12 09:05:12 +00:00
|
|
|
const noInput = inputAmount.text === ""
|
|
|
|
if (noInput && hasTyped) {
|
|
|
|
error = noInputErrorMessage
|
|
|
|
isValid = false
|
|
|
|
} else if (isNaN(inputAmount.text)) {
|
|
|
|
error = invalidInputErrorMessage
|
|
|
|
isValid = false
|
2021-07-07 18:37:49 +00:00
|
|
|
} else if (formattedInputValue < 0.00 && hasTyped) {
|
|
|
|
error = greaterThanOrEqualTo0ErrorMessage
|
2020-08-12 09:05:12 +00:00
|
|
|
isValid = false
|
2021-07-07 18:37:49 +00:00
|
|
|
} else if (validateBalance && formattedInputValue > balance && !noInput) {
|
2020-08-12 09:05:12 +00:00
|
|
|
error = balanceErrorMessage
|
|
|
|
isValid = false
|
|
|
|
}
|
|
|
|
if (!isValid) {
|
2021-07-07 18:37:49 +00:00
|
|
|
root.validationError = error
|
2020-08-12 09:05:12 +00:00
|
|
|
txtBalanceDesc.color = Style.current.danger
|
|
|
|
txtBalance.color = Style.current.danger
|
|
|
|
} else {
|
2021-07-07 18:37:49 +00:00
|
|
|
root.validationError = ""
|
2020-08-12 09:05:12 +00:00
|
|
|
txtBalanceDesc.color = Style.current.secondaryText
|
|
|
|
txtBalance.color = Qt.binding(function() { return txtBalance.hovered ? Style.current.textColor : Style.current.secondaryText })
|
|
|
|
}
|
2020-08-20 04:45:29 +00:00
|
|
|
root.isValid = isValid
|
2020-08-12 09:05:12 +00:00
|
|
|
return isValid
|
|
|
|
}
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
onSelectedAccountChanged: {
|
2021-12-08 21:20:43 +00:00
|
|
|
selectAsset.assets = Qt.binding(function() {
|
2020-08-20 04:45:29 +00:00
|
|
|
if (selectedAccount) {
|
|
|
|
return selectedAccount.assets
|
|
|
|
}
|
|
|
|
})
|
2021-12-08 21:20:43 +00:00
|
|
|
txtBalance.text = Qt.binding(function() {
|
2020-08-20 04:45:29 +00:00
|
|
|
return selectAsset.selectedAsset ? Utils.stripTrailingZeros(selectAsset.selectedAsset.value) : ""
|
|
|
|
})
|
2020-08-12 09:05:12 +00:00
|
|
|
}
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
Item {
|
2020-10-28 07:44:09 +00:00
|
|
|
visible: root.validateBalance
|
2020-08-12 09:05:12 +00:00
|
|
|
anchors.right: parent.right
|
|
|
|
anchors.left: parent.left
|
|
|
|
anchors.top: parent.top
|
|
|
|
height: txtBalanceDesc.height
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
StyledText {
|
|
|
|
id: txtBalanceDesc
|
2022-04-04 11:26:30 +00:00
|
|
|
text: qsTr("Balance: ")
|
2020-08-12 09:05:12 +00:00
|
|
|
anchors.right: txtBalance.left
|
|
|
|
font.weight: Font.Medium
|
|
|
|
font.pixelSize: 13
|
|
|
|
color: Style.current.secondaryText
|
|
|
|
}
|
|
|
|
|
|
|
|
StyledText {
|
|
|
|
id: txtBalance
|
|
|
|
property bool hovered: false
|
2020-08-13 08:24:51 +00:00
|
|
|
text: selectAsset.selectedAsset ? Utils.stripTrailingZeros(selectAsset.selectedAsset.value) : "0.00"
|
2020-08-12 09:05:12 +00:00
|
|
|
anchors.right: parent.right
|
|
|
|
font.weight: Font.Medium
|
|
|
|
font.pixelSize: 13
|
|
|
|
color: hovered ? Style.current.textColor : Style.current.secondaryText
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
MouseArea {
|
|
|
|
cursorShape: Qt.PointingHandCursor
|
|
|
|
anchors.fill: parent
|
|
|
|
hoverEnabled: true
|
|
|
|
onExited: {
|
|
|
|
txtBalance.hovered = false
|
|
|
|
}
|
|
|
|
onEntered: {
|
|
|
|
txtBalance.hovered = true
|
|
|
|
}
|
|
|
|
onClicked: {
|
2020-08-20 04:45:29 +00:00
|
|
|
inputAmount.text = Utils.stripTrailingZeros(selectAsset.selectedAsset.value)
|
2021-12-23 20:46:58 +00:00
|
|
|
txtFiatBalance.text = root.getFiatValue(inputAmount.text, selectAsset.selectedAsset.symbol, root.currentCurrency)
|
2020-08-12 09:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
Input {
|
|
|
|
id: inputAmount
|
2022-04-04 11:26:30 +00:00
|
|
|
label: qsTr("Asset & Amount")
|
2020-08-12 09:05:12 +00:00
|
|
|
placeholderText: "0.00"
|
|
|
|
anchors.top: parent.top
|
|
|
|
customHeight: 56
|
|
|
|
validationErrorAlignment: TextEdit.AlignRight
|
|
|
|
validationErrorTopMargin: 8
|
2021-07-07 18:37:49 +00:00
|
|
|
validationErrorColor: formattedInputValue === 0 ? Style.current.warning : Style.current.danger
|
|
|
|
validationError: {
|
|
|
|
if (root.validationError) {
|
|
|
|
return root.validationError
|
|
|
|
}
|
|
|
|
if (formattedInputValue === 0) {
|
2022-04-04 11:26:30 +00:00
|
|
|
return qsTr("The amount is 0. Proceed only if this is desired.")
|
2021-07-07 18:37:49 +00:00
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
Keys.onReleased: {
|
|
|
|
let amount = inputAmount.text.trim()
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
if (isNaN(amount)) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if (amount === "") {
|
|
|
|
txtFiatBalance.text = "0.00"
|
|
|
|
} else {
|
2021-12-23 20:46:58 +00:00
|
|
|
txtFiatBalance.text = root.getFiatValue(amount, selectAsset.selectedAsset.symbol, root.currentCurrency)
|
2020-08-12 09:05:12 +00:00
|
|
|
}
|
2020-08-04 11:10:09 +00:00
|
|
|
}
|
2020-08-12 09:05:12 +00:00
|
|
|
onTextChanged: {
|
2020-08-20 04:45:29 +00:00
|
|
|
root.isDirty = true
|
2020-08-12 09:05:12 +00:00
|
|
|
root.validate(true)
|
2020-08-04 11:10:09 +00:00
|
|
|
}
|
2020-08-12 09:05:12 +00:00
|
|
|
}
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2021-11-15 12:36:22 +00:00
|
|
|
StatusAssetSelector {
|
|
|
|
id: selectAsset
|
|
|
|
anchors.top: inputAmount.top
|
2022-07-21 08:40:49 +00:00
|
|
|
anchors.topMargin: 28
|
2021-11-15 12:36:22 +00:00
|
|
|
anchors.right: parent.right
|
|
|
|
anchors.rightMargin: Style.current.smallPadding
|
|
|
|
defaultToken: Style.png("tokens/DEFAULT-TOKEN@3x")
|
|
|
|
getCurrencyBalanceString: function (currencyBalance) {
|
2021-12-08 21:20:43 +00:00
|
|
|
return Utils.toLocaleString(currencyBalance.toFixed(2), RootStore.locale, {"currency": true}) + " " + root.currentCurrency.toUpperCase()
|
2021-11-15 12:36:22 +00:00
|
|
|
}
|
|
|
|
tokenAssetSourceFn: function (symbol) {
|
|
|
|
return symbol ? Style.png("tokens/" + symbol) : defaultToken
|
|
|
|
}
|
2022-08-29 16:28:54 +00:00
|
|
|
searchTokenSymbolByAddressFn: function (address) {
|
2022-08-18 20:49:50 +00:00
|
|
|
if(popup.selectedAccount) {
|
|
|
|
return popup.selectedAccount.findTokenSymbolByAddress(address)
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
2021-11-15 12:36:22 +00:00
|
|
|
onSelectedAssetChanged: {
|
|
|
|
if (!selectAsset.selectedAsset) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
txtBalance.text = Utils.stripTrailingZeros(parseFloat(selectAsset.selectedAsset.balance).toFixed(4))
|
|
|
|
if (inputAmount.text === "" || isNaN(inputAmount.text)) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-23 20:46:58 +00:00
|
|
|
txtFiatBalance.text = root.getFiatValue(inputAmount.text, selectAsset.selectedAsset.symbol, root.currentCurrency)
|
2020-08-20 04:45:29 +00:00
|
|
|
root.validate(true)
|
2020-08-12 09:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
Item {
|
|
|
|
height: txtFiatBalance.height
|
|
|
|
anchors.left: parent.left
|
|
|
|
anchors.top: inputAmount.bottom
|
|
|
|
anchors.topMargin: inputAmount.validationError ? -16 : inputAmount.validationErrorTopMargin
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
StyledTextField {
|
|
|
|
id: txtFiatBalance
|
|
|
|
anchors.left: parent.left
|
|
|
|
anchors.top: parent.top
|
|
|
|
color: txtFiatBalance.activeFocus ? Style.current.textColor : Style.current.secondaryText
|
|
|
|
font.weight: Font.Medium
|
|
|
|
font.pixelSize: 12
|
|
|
|
inputMethodHints: Qt.ImhFormattedNumbersOnly
|
2020-08-13 08:24:51 +00:00
|
|
|
text: "0.00"
|
2020-08-12 09:05:12 +00:00
|
|
|
selectByMouse: true
|
|
|
|
background: Rectangle {
|
|
|
|
color: Style.current.transparent
|
|
|
|
}
|
|
|
|
padding: 0
|
|
|
|
Keys.onReleased: {
|
|
|
|
let balance = txtFiatBalance.text.trim()
|
|
|
|
if (balance === "" || isNaN(balance)) {
|
|
|
|
return
|
|
|
|
}
|
2021-12-23 20:46:58 +00:00
|
|
|
inputAmount.text = root.getCryptoValue(balance, root.currentCurrency, selectAsset.selectedAsset.symbol)
|
2020-08-12 09:05:12 +00:00
|
|
|
}
|
|
|
|
}
|
2020-08-04 11:10:09 +00:00
|
|
|
|
2020-08-12 09:05:12 +00:00
|
|
|
StyledText {
|
|
|
|
id: txtFiatSymbol
|
2021-12-23 20:46:58 +00:00
|
|
|
text: root.currentCurrency.toUpperCase()
|
2020-08-12 09:05:12 +00:00
|
|
|
font.weight: Font.Medium
|
|
|
|
font.pixelSize: 12
|
|
|
|
color: Style.current.secondaryText
|
|
|
|
anchors.top: parent.top
|
|
|
|
anchors.left: txtFiatBalance.right
|
|
|
|
anchors.leftMargin: 2
|
|
|
|
}
|
|
|
|
}
|
2020-08-04 11:10:09 +00:00
|
|
|
}
|