fix(StatusAssetSelector): Fixed error of crypto balance not updated correctly

Also added error handling in case in case that crypto symbol image is not available
Added a way to display the balance in currently selected currency on the widget
To do this the consumer of this widget needs to implement "getCurrencyBalanceString" based on the currently selected currency

fixes #4079
This commit is contained in:
Khushboo Mehta 2021-11-15 14:31:58 +01:00 committed by Michał Cieślak
parent 285641b434
commit 375c76ca1d
2 changed files with 31 additions and 31 deletions

View File

@ -13,22 +13,26 @@ Column {
spacing: 8 spacing: 8
StatusAssetSelector { StatusAssetSelector {
getCurrencyBalanceString: function (currencyBalance) {
return currencyBalance.toFixed(2) + " USD"
}
tokenAssetSourceFn: function (symbol) {
return "../../assets/img/icons/snt.svg"
}
assets: ListModel { assets: ListModel {
ListElement { ListElement {
address: "0x1234" address: "0x1234"
name: "Status Network Token" name: "Status Network Token"
value: "20" balance: "20"
symbol: "SNT" symbol: "SNT"
fiatBalance: "9992.01" currencyBalance: 9992.01
fiatBalanceDisplay: "9992.01"
} }
ListElement { ListElement {
address: "0x1234" address: "0x1234"
name: "DAI Token" name: "DAI Token"
value: "20" balance: "15"
symbol: "DAI" symbol: "DAI"
fiatBalance: "20" currencyBalance: 20.00001
fiatBalanceDisplay: "20"
} }
} }
} }

View File

@ -12,17 +12,12 @@ Item {
id: root id: root
property var assets property var assets
property var selectedAsset property var selectedAsset
property string defaultToken: ""
property var tokenAssetSourceFn: function (symbol) { property var tokenAssetSourceFn: function (symbol) {
return "" return ""
} }
property var assetGetterFn: function (index, field) { // Define this in the usage to get balance in currency selected by user
// This is merely to make the component work with nim model data property var getCurrencyBalanceString: function (currencyBalance) { return "" }
// as well as static QML ListModel data
if (typeof assets.get === "function") {
return assets.get(index)[field]
}
return assets.rowData(index, field)
}
implicitWidth: 86 implicitWidth: 86
implicitHeight: 32 implicitHeight: 32
@ -38,20 +33,6 @@ Item {
} }
} }
onAssetsChanged: {
if (!assets) {
return
}
selectedAsset = {
name: assetGetterFn(0, "name"),
symbol: assetGetterFn(0, "symbol"),
value: assetGetterFn(0, "value"),
fiatBalanceDisplay: assetGetterFn(0, "fiatBalanceDisplay"),
address: assetGetterFn(0, "address"),
fiatBalance: assetGetterFn(0, "fiatBalance")
}
}
StatusSelect { StatusSelect {
id: select id: select
width: parent.width width: parent.width
@ -71,6 +52,11 @@ Item {
width: 24 width: 24
height: 24 height: 24
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
image.onStatusChanged: {
if (iconImg.image.status === Image.Error) {
iconImg.image.source = defaultToken
}
}
} }
StatusBaseText { StatusBaseText {
id: selectedTextField id: selectedTextField
@ -101,6 +87,11 @@ Item {
anchors.leftMargin: 16 anchors.leftMargin: 16
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
image.source: root.tokenAssetSourceFn(symbol.toUpperCase()) image.source: root.tokenAssetSourceFn(symbol.toUpperCase())
image.onStatusChanged: {
if (iconImg.image.status === Image.Error) {
iconImg.image.source = defaultToken
}
}
} }
Column { Column {
anchors.left: iconImg.right anchors.left: iconImg.right
@ -125,23 +116,28 @@ Item {
anchors.verticalCenter: parent.verticalCenter anchors.verticalCenter: parent.verticalCenter
StatusBaseText { StatusBaseText {
font.pixelSize: 15 font.pixelSize: 15
text: parseFloat(value).toFixed(4) + " " + symbol text: parseFloat(balance).toFixed(4) + " " + symbol
} }
StatusBaseText { StatusBaseText {
font.pixelSize: 15 font.pixelSize: 15
anchors.right: parent.right anchors.right: parent.right
text: fiatBalanceDisplay.toUpperCase() text: getCurrencyBalanceString(currencyBalance)
color: Theme.palette.baseColor1 color: Theme.palette.baseColor1
} }
} }
background: Rectangle { background: Rectangle {
color: itemContainer.highlighted ? Theme.palette.statusSelect.menuItemHoverBackgroundColor : Theme.palette.statusSelect.menuItemBackgroundColor color: itemContainer.highlighted ? Theme.palette.statusSelect.menuItemHoverBackgroundColor : Theme.palette.statusSelect.menuItemBackgroundColor
} }
Component.onCompleted: {
if(index === 0 ) {
selectedAsset = { name: name, symbol: symbol, address: address, balance: balance, currencyBalance: currencyBalance}
}
}
MouseArea { MouseArea {
cursorShape: Qt.PointingHandCursor cursorShape: Qt.PointingHandCursor
anchors.fill: itemContainer anchors.fill: itemContainer
onClicked: { onClicked: {
root.selectedAsset = { address, name, value, symbol, fiatBalance, fiatBalanceDisplay } selectedAsset = {name: name, symbol: symbol, address: address, balance: balance, currencyBalance: currencyBalance}
select.selectMenu.close() select.selectMenu.close()
} }
} }