2023-09-15 08:51:06 +00:00
|
|
|
import QtQuick 2.15
|
|
|
|
import QtQuick.Layouts 1.15
|
2022-12-14 21:06:14 +00:00
|
|
|
|
|
|
|
import StatusQ.Core 0.1
|
|
|
|
import StatusQ.Core.Theme 0.1
|
2024-06-10 10:31:13 +00:00
|
|
|
import StatusQ.Controls 0.1
|
2023-09-15 08:51:06 +00:00
|
|
|
import StatusQ.Core.Utils 0.1 as SQUtils
|
2024-05-28 17:39:41 +00:00
|
|
|
import StatusQ.Components 0.1
|
2022-12-14 21:06:14 +00:00
|
|
|
import StatusQ.Controls.Validators 0.1
|
|
|
|
|
|
|
|
import "../controls"
|
|
|
|
|
|
|
|
import utils 1.0
|
|
|
|
|
|
|
|
ColumnLayout {
|
|
|
|
id: root
|
|
|
|
|
2023-09-15 08:51:06 +00:00
|
|
|
readonly property alias input: topAmountToSendInput
|
2024-05-28 17:39:41 +00:00
|
|
|
readonly property bool inputNumberValid: !!input.text && !isNaN(d.parsedInput) && input.valid
|
2023-09-15 08:51:06 +00:00
|
|
|
|
|
|
|
readonly property int minSendCryptoDecimals:
|
|
|
|
!inputIsFiat ? LocaleUtils.fractionalPartLength(d.inputNumber) : 0
|
|
|
|
readonly property int minReceiveCryptoDecimals:
|
|
|
|
!inputIsFiat ? minSendCryptoDecimals + 1 : 0
|
|
|
|
readonly property int minSendFiatDecimals:
|
|
|
|
inputIsFiat ? LocaleUtils.fractionalPartLength(d.inputNumber) : 0
|
|
|
|
readonly property int minReceiveFiatDecimals:
|
|
|
|
inputIsFiat ? minSendFiatDecimals + 1 : 0
|
|
|
|
|
2024-02-05 16:44:49 +00:00
|
|
|
property var selectedHolding // Crypto asset symbol like ETH
|
2023-09-15 08:51:06 +00:00
|
|
|
property string currentCurrency // Fiat currency symbol like USD
|
|
|
|
|
|
|
|
property int multiplierIndex // How divisible the token is, 18 for ETH
|
|
|
|
|
|
|
|
property double maxInputBalance
|
2022-12-14 21:06:14 +00:00
|
|
|
|
|
|
|
property bool isBridgeTx: false
|
|
|
|
property bool interactive: false
|
2023-01-08 22:23:51 +00:00
|
|
|
property bool inputIsFiat: false
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-05-28 17:39:41 +00:00
|
|
|
property string caption: isBridgeTx ? qsTr("Amount to bridge") : qsTr("Amount to send")
|
|
|
|
|
|
|
|
property bool fiatInputInteractive: true
|
|
|
|
|
2023-09-15 08:51:06 +00:00
|
|
|
// Crypto value to send expressed in base units (like wei for ETH),
|
|
|
|
// as a string representing integer decimal
|
|
|
|
readonly property alias cryptoValueToSend: d.cryptoValueRawToSend
|
|
|
|
|
|
|
|
readonly property alias cryptoValueToSendFloat: d.cryptoValueToSend
|
|
|
|
|
|
|
|
property var formatCurrencyAmount:
|
|
|
|
(amount, symbol, options = null, locale = null) => {}
|
2022-12-14 21:06:14 +00:00
|
|
|
|
2024-06-14 11:34:20 +00:00
|
|
|
property bool mainInputLoading
|
|
|
|
property bool bottomTextLoading
|
2024-05-28 17:39:41 +00:00
|
|
|
|
2022-12-14 21:06:14 +00:00
|
|
|
signal reCalculateSuggestedRoute()
|
|
|
|
|
|
|
|
QtObject {
|
|
|
|
id: d
|
2023-09-15 08:51:06 +00:00
|
|
|
|
|
|
|
property double cryptoValueToSend
|
|
|
|
property double fiatValueToSend
|
|
|
|
|
|
|
|
Binding on cryptoValueToSend {
|
|
|
|
value: {
|
2024-02-05 16:44:49 +00:00
|
|
|
root.selectedHolding
|
|
|
|
if(!root.selectedHolding || !root.selectedHolding.marketDetails || !root.selectedHolding.marketDetails.currencyPrice) {
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
return root.inputIsFiat ? d.fiatValueToSend/root.selectedHolding.marketDetails.currencyPrice.amount
|
2023-09-15 08:51:06 +00:00
|
|
|
: d.inputNumber
|
|
|
|
}
|
|
|
|
delayed: true
|
|
|
|
}
|
|
|
|
|
|
|
|
Binding on fiatValueToSend {
|
|
|
|
value: {
|
2024-02-05 16:44:49 +00:00
|
|
|
root.selectedHolding
|
|
|
|
if(!root.selectedHolding || !root.selectedHolding.marketDetails || !root.selectedHolding.marketDetails.currencyPrice) {
|
|
|
|
return 0
|
|
|
|
}
|
2023-09-15 08:51:06 +00:00
|
|
|
return root.inputIsFiat ? d.inputNumber
|
2024-02-05 16:44:49 +00:00
|
|
|
: d.cryptoValueToSend * root.selectedHolding.marketDetails.currencyPrice.amount
|
2023-09-15 08:51:06 +00:00
|
|
|
}
|
|
|
|
delayed: true
|
|
|
|
}
|
|
|
|
|
2024-02-05 16:44:49 +00:00
|
|
|
readonly property string selectedSymbol: !!root.selectedHolding && !!root.selectedHolding.symbol ? root.selectedHolding.symbol: ""
|
|
|
|
|
2023-09-15 08:51:06 +00:00
|
|
|
readonly property string cryptoValueRawToSend: {
|
|
|
|
return SQUtils.AmountsArithmetic.fromNumber(
|
|
|
|
d.cryptoValueToSend, root.multiplierIndex).toString()
|
|
|
|
}
|
|
|
|
|
2024-06-13 00:45:33 +00:00
|
|
|
// Crypto value should be represented by 0 and fiat with 0.00
|
|
|
|
readonly property string zeroString: {
|
|
|
|
let decimals = root.inputIsFiat ? 2 : 0
|
|
|
|
LocaleUtils.numberToLocaleString(0, decimals, topAmountToSendInput.locale)
|
|
|
|
}
|
2023-09-15 08:51:06 +00:00
|
|
|
|
|
|
|
readonly property double parsedInput:
|
|
|
|
LocaleUtils.numberFromLocaleString(topAmountToSendInput.text,
|
2024-05-28 17:39:41 +00:00
|
|
|
topAmountToSendInput.locale)
|
2023-09-15 08:51:06 +00:00
|
|
|
|
|
|
|
readonly property double inputNumber:
|
2024-06-14 11:34:20 +00:00
|
|
|
// we should still calculate if value entered is greater than max safe value
|
|
|
|
!!input.text && !isNaN(d.parsedInput) && d.parsedInput >= 0 ? d.parsedInput : 0
|
2023-09-15 08:51:06 +00:00
|
|
|
|
|
|
|
readonly property Timer waitTimer: Timer {
|
2022-12-14 21:06:14 +00:00
|
|
|
interval: 1000
|
|
|
|
onTriggered: reCalculateSuggestedRoute()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:56:31 +00:00
|
|
|
onMaxInputBalanceChanged: {
|
2022-12-19 13:02:56 +00:00
|
|
|
input.validate()
|
|
|
|
}
|
|
|
|
|
2024-07-17 16:48:16 +00:00
|
|
|
onSelectedHoldingChanged: {
|
|
|
|
input.validate()
|
|
|
|
}
|
|
|
|
|
2022-12-14 21:06:14 +00:00
|
|
|
StatusBaseText {
|
2024-05-28 17:39:41 +00:00
|
|
|
text: root.caption
|
2022-12-14 21:06:14 +00:00
|
|
|
font.pixelSize: 13
|
|
|
|
lineHeight: 18
|
|
|
|
lineHeightMode: Text.FixedHeight
|
|
|
|
color: Theme.palette.directColor1
|
|
|
|
}
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2022-12-14 21:06:14 +00:00
|
|
|
RowLayout {
|
2024-05-28 17:39:41 +00:00
|
|
|
Layout.fillWidth: true
|
2023-01-08 22:23:51 +00:00
|
|
|
id: topItem
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2022-12-14 21:06:14 +00:00
|
|
|
AmountInputWithCursor {
|
2023-01-08 22:23:51 +00:00
|
|
|
id: topAmountToSendInput
|
2024-05-28 17:39:41 +00:00
|
|
|
Layout.fillWidth: true
|
2023-06-09 10:38:40 +00:00
|
|
|
Layout.maximumWidth: 250
|
2024-04-15 05:28:29 +00:00
|
|
|
Layout.preferredWidth: !!text ? input.edit.paintedWidth + 2
|
2023-09-15 08:51:06 +00:00
|
|
|
: textMetrics.advanceWidth
|
2022-12-14 21:06:14 +00:00
|
|
|
placeholderText: d.zeroString
|
2023-09-15 08:51:06 +00:00
|
|
|
input.edit.color: input.valid ? Theme.palette.directColor1
|
|
|
|
: Theme.palette.dangerColor1
|
2022-12-14 21:06:14 +00:00
|
|
|
input.edit.readOnly: !root.interactive
|
2024-05-28 17:39:41 +00:00
|
|
|
validationMode: StatusInput.ValidationMode.Always
|
2022-12-14 21:06:14 +00:00
|
|
|
validators: [
|
2023-09-15 08:51:06 +00:00
|
|
|
StatusValidator {
|
|
|
|
errorMessage: ""
|
|
|
|
|
|
|
|
validate: (text) => {
|
2024-07-17 16:48:16 +00:00
|
|
|
const num = LocaleUtils.numberFromLocaleString(topAmountToSendInput.text,
|
2024-06-14 11:34:20 +00:00
|
|
|
topAmountToSendInput.locale)
|
2024-07-17 16:48:16 +00:00
|
|
|
if (isNaN(num) || num <= 0 || num > root.maxInputBalance) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
if(!root.selectedHolding || !root.selectedHolding.marketDetails || !root.selectedHolding.marketDetails.currencyPrice) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
const cryptoValueToSend = root.inputIsFiat ? num / root.selectedHolding.marketDetails.currencyPrice.amount : num
|
|
|
|
const cryptoValueToSendRaw = SQUtils.AmountsArithmetic.fromNumber(cryptoValueToSend, root.multiplierIndex).toString()
|
|
|
|
return cryptoValueToSendRaw >= 1
|
2024-05-28 17:39:41 +00:00
|
|
|
}
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
|
|
|
]
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2022-12-14 21:06:14 +00:00
|
|
|
TextMetrics {
|
|
|
|
id: textMetrics
|
2023-01-08 22:23:51 +00:00
|
|
|
text: topAmountToSendInput.placeholderText
|
2024-05-28 17:39:41 +00:00
|
|
|
font: topAmountToSendInput.placeholderFont
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2022-12-14 21:06:14 +00:00
|
|
|
Keys.onReleased: {
|
2023-09-15 08:51:06 +00:00
|
|
|
const amount = LocaleUtils.numberFromLocaleString(
|
|
|
|
topAmountToSendInput.text,
|
2024-04-15 05:28:29 +00:00
|
|
|
locale)
|
2023-09-15 08:51:06 +00:00
|
|
|
if (!isNaN(amount))
|
|
|
|
d.waitTimer.restart()
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
2024-05-28 17:39:41 +00:00
|
|
|
|
2024-06-14 11:34:20 +00:00
|
|
|
visible: !root.mainInputLoading
|
2024-05-28 17:39:41 +00:00
|
|
|
}
|
|
|
|
LoadingComponent {
|
|
|
|
objectName: "topAmountToSendInputLoadingComponent"
|
|
|
|
Layout.preferredWidth: topAmountToSendInput.width
|
|
|
|
Layout.preferredHeight: topAmountToSendInput.height
|
2024-06-14 11:34:20 +00:00
|
|
|
visible: root.mainInputLoading
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
|
|
|
}
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2024-05-28 17:39:41 +00:00
|
|
|
StatusBaseText {
|
|
|
|
Layout.maximumWidth: parent.width
|
|
|
|
id: bottomItem
|
|
|
|
objectName: "bottomItemText"
|
|
|
|
|
|
|
|
readonly property double bottomAmountToSend: inputIsFiat ? d.cryptoValueToSend
|
|
|
|
: d.fiatValueToSend
|
|
|
|
readonly property string bottomAmountSymbol: inputIsFiat ? d.selectedSymbol
|
|
|
|
: root.currentCurrency
|
|
|
|
elide: Text.ElideMiddle
|
|
|
|
text: root.formatCurrencyAmount(bottomAmountToSend, bottomAmountSymbol)
|
|
|
|
font.pixelSize: 13
|
|
|
|
color: Theme.palette.directColor5
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2022-12-14 21:06:14 +00:00
|
|
|
MouseArea {
|
|
|
|
anchors.fill: parent
|
2024-05-28 17:39:41 +00:00
|
|
|
cursorShape: enabled ? Qt.PointingHandCursor : undefined
|
|
|
|
enabled: root.fiatInputInteractive && !!root.selectedHolding
|
2023-09-15 08:51:06 +00:00
|
|
|
|
2022-12-14 21:06:14 +00:00
|
|
|
onClicked: {
|
2023-01-08 22:23:51 +00:00
|
|
|
topAmountToSendInput.validate()
|
|
|
|
if(!!topAmountToSendInput.text) {
|
2023-09-15 08:51:06 +00:00
|
|
|
topAmountToSendInput.text = root.formatCurrencyAmount(
|
|
|
|
bottomItem.bottomAmountToSend,
|
|
|
|
bottomItem.bottomAmountSymbol,
|
|
|
|
{ noSymbol: true, rawAmount: true },
|
2024-05-28 17:39:41 +00:00
|
|
|
topAmountToSendInput.locale)
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
2024-05-28 17:39:41 +00:00
|
|
|
root.inputIsFiat = !root.inputIsFiat
|
2023-01-08 22:23:51 +00:00
|
|
|
d.waitTimer.restart()
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
|
|
|
}
|
2024-06-14 11:34:20 +00:00
|
|
|
visible: !root.bottomTextLoading
|
2022-12-14 21:06:14 +00:00
|
|
|
}
|
|
|
|
|
2024-05-28 17:39:41 +00:00
|
|
|
LoadingComponent {
|
|
|
|
objectName: "bottomItemTextLoadingComponent"
|
|
|
|
Layout.preferredWidth: bottomItem.width
|
|
|
|
Layout.preferredHeight: bottomItem.height
|
2024-06-14 11:34:20 +00:00
|
|
|
visible: root.bottomTextLoading
|
2024-05-28 17:39:41 +00:00
|
|
|
}
|
|
|
|
}
|