fix: fix defaultTokenList use in QML

This commit is contained in:
Jonathan Rainville 2020-09-14 13:32:47 -04:00 committed by Iuri Matias
parent 103f02b289
commit 17aa8d274e
3 changed files with 201 additions and 153 deletions

View File

@ -7,7 +7,8 @@ type
Name = UserRole + 1,
Symbol = UserRole + 2,
HasIcon = UserRole + 3,
Address = UserRole + 4
Address = UserRole + 4,
Decimals = UserRole + 5
QtObject:
type TokenList* = ref object of QAbstractListModel
@ -38,6 +39,17 @@ QtObject:
result.tokens = @[]
result.setup
proc rowData(self: TokenList, index: int, column: string): string {.slot.} =
if (index >= self.tokens.len):
return
let token = self.tokens[index]
case column:
of "name": result = token["name"].getStr
of "symbol": result = token["symbol"].getStr
of "hasIcon": result = $token["hasIcon"].getBool
of "address": result = token["address"].getStr
of "decimals": result = $token["decimals"].getInt
method rowCount(self: TokenList, index: QModelIndex = nil): int =
return self.tokens.len
@ -53,10 +65,12 @@ QtObject:
of TokenRoles.Symbol: result = newQVariant(token["symbol"].getStr)
of TokenRoles.HasIcon: result = newQVariant(token{"hasIcon"}.getBool)
of TokenRoles.Address: result = newQVariant(token["address"].getStr)
of TokenRoles.Decimals: result = newQVariant(token["decimals"].getInt)
method roleNames(self: TokenList): Table[int, string] =
{TokenRoles.Name.int:"name",
TokenRoles.Symbol.int:"symbol",
TokenRoles.HasIcon.int:"hasIcon",
TokenRoles.Address.int:"address"}.toTable
TokenRoles.Address.int:"address",
TokenRoles.Decimals.int:"decimals"}.toTable

View File

@ -24,6 +24,19 @@ Item {
}
}
}
property var tokens: {
const count = walletModel.defaultTokenList.rowCount()
const toks = []
for (var i = 0; i < count; i++) {
toks.push({
"address": walletModel.defaultTokenList.rowData(i, 'address'),
"name": walletModel.defaultTokenList.rowData(i, 'name'),
"decimals": parseInt(walletModel.defaultTokenList.rowData(i, 'decimals'), 10),
"symbol": walletModel.defaultTokenList.rowData(i, 'symbol')
})
}
return toks
}
property var token: {
if (commandParametersObject.contract === "") {
return {
@ -35,13 +48,14 @@ Item {
}
}
let count = walletModel.defaultTokenList.items.count
for (let i = 0; i < count; i++) {
let token = walletModel.defaultTokenList.items.get(i)
if (commandParametersObject.contract === token.address) {
const count = root.tokens.length
for (var i = 0; i < count; i++) {
let token = root.tokens[i]
if (token.address === commandParametersObject.contract) {
return token
}
}
return {}
}
property string tokenAmount: {

View File

@ -5,14 +5,29 @@ import "../../../imports"
import "../../../shared"
Item {
function checkIfHistoryIsBeingFetched(){
if(walletModel.isFetchingHistory(walletModel.currentAccount.address)){
loadingImg.active = true;
} else {
walletModel.loadTransactionsForAccount(walletModel.currentAccount.address)
}
property var tokens: {
const count = walletModel.defaultTokenList.rowCount()
const toks = []
for (var i = 0; i < count; i++) {
toks.push({
"address": walletModel.defaultTokenList.rowData(i, 'address'),
"symbol": walletModel.defaultTokenList.rowData(i, 'symbol')
})
}
return toks
}
function checkIfHistoryIsBeingFetched() {
if (walletModel.isFetchingHistory(walletModel.currentAccount.address)) {
loadingImg.active = true
} else {
walletModel.loadTransactionsForAccount(
walletModel.currentAccount.address)
}
}
id: root
Loader {
id: loadingImg
active: false
@ -37,157 +52,161 @@ Item {
}
Component {
id: transactionListItemCmp
id: transactionListItemCmp
Rectangle {
id: transactionListItem
property bool isHovered: false
property string symbol: ""
anchors.right: parent.right
anchors.left: parent.left
height: 64
color: isHovered ? Style.current.secondaryBackground : Style.current.transparent
radius: 8
Rectangle {
id: transactionListItem
property bool isHovered: false
property string symbol: ""
anchors.right: parent.right
anchors.left: parent.left
height: 64
color: isHovered ? Style.current.secondaryBackground : Style.current.transparent
radius: 8
Component.onCompleted: {
// TODO: test if this work. I could not obtain any transaction history
for (let i = 0; i < walletModel.defaultTokenList.items.count; i++) {
let token = walletModel.defaultTokenList.items.get(i)
if (token.address == contract) {
transactionListItem.symbol = token.symbol
break;
Component.onCompleted: {
const count = root.tokens.length
for (var i = 0; i < count; i++) {
let token = root.tokens[i]
if (token.address === contract) {
transactionListItem.symbol = token.symbol
break
}
}
}
}
}
MouseArea {
anchors.fill: parent
onClicked: transactionModal.open()
cursorShape: Qt.PointingHandCursor
hoverEnabled: true
onEntered: {
transactionListItem.isHovered = true
MouseArea {
anchors.fill: parent
onClicked: transactionModal.open()
cursorShape: Qt.PointingHandCursor
hoverEnabled: true
onEntered: {
transactionListItem.isHovered = true
}
onExited: {
transactionListItem.isHovered = false
}
}
onExited: {
transactionListItem.isHovered = false
TransactionModal {
id: transactionModal
}
Item {
Image {
id: assetIcon
width: 40
height: 40
source: "../../img/tokens/"
+ (transactionListItem.symbol
!= "" ? transactionListItem.symbol : "ETH") + ".png"
anchors.left: parent.left
anchors.top: parent.top
anchors.topMargin: 12
onStatusChanged: {
if (assetIcon.status == Image.Error) {
assetIcon.source = "../../img/tokens/0-native.png"
}
}
anchors.leftMargin: Style.current.padding
}
StyledText {
id: transferIcon
anchors.topMargin: 25
anchors.top: parent.top
anchors.left: assetIcon.right
anchors.leftMargin: 22
height: 15
width: 15
color: to !== walletModel.currentAccount.address ? "#4360DF" : "green"
text: to !== walletModel.currentAccount.address ? "↑" : "↓"
}
StyledText {
id: transactionValue
anchors.left: transferIcon.right
anchors.leftMargin: Style.current.smallPadding
anchors.top: parent.top
anchors.topMargin: Style.current.bigPadding
font.pixelSize: 15
text: value + " " + transactionListItem.symbol
}
}
Item {
anchors.right: timeInfo.left
anchors.top: parent.top
anchors.topMargin: Style.current.bigPadding
width: children[0].width + children[1].width
StyledText {
text: to !== walletModel.currentAccount.address ?
qsTr("To ") :
qsTr("From ")
anchors.right: addressValue.left
color: Style.current.darkGrey
anchors.top: parent.top
font.pixelSize: 15
font.strikeout: false
}
StyledText {
id: addressValue
font.family: Style.current.fontHexRegular.name
text: to
width: 100
elide: Text.ElideMiddle
anchors.right: parent.right
anchors.top: parent.top
font.pixelSize: 15
}
}
Item {
id: timeInfo
anchors.right: parent.right
anchors.top: parent.top
anchors.topMargin: Style.current.bigPadding
width: children[0].width + children[1].width + children[2].width
StyledText {
text: "• "
font.weight: Font.Bold
anchors.right: timeIndicator.left
color: Style.current.darkGrey
anchors.top: parent.top
font.pixelSize: 15
}
StyledText {
id: timeIndicator
text: "At "
anchors.right: timeValue.left
color: Style.current.darkGrey
anchors.top: parent.top
font.pixelSize: 15
font.strikeout: false
}
StyledText {
id: timeValue
text: timestamp
anchors.right: parent.right
anchors.top: parent.top
font.pixelSize: 15
anchors.rightMargin: Style.current.smallPadding
}
}
}
TransactionModal{
id: transactionModal
}
Item {
Image {
id: assetIcon
width: 40
height: 40
source: "../../img/tokens/" + (transactionListItem.symbol != "" ? transactionListItem.symbol : "ETH") + ".png"
anchors.left: parent.left
anchors.top: parent.top
anchors.topMargin: 12
onStatusChanged: {
if (assetIcon.status == Image.Error) {
assetIcon.source = "../../img/tokens/0-native.png"
}
}
anchors.leftMargin: Style.current.padding
}
StyledText {
id: transferIcon
anchors.topMargin: 25
anchors.top: parent.top
anchors.left: assetIcon.right
anchors.leftMargin: 22
height: 15
width: 15
color: to != walletModel.currentAccount.address ? "#4360DF" : "green"
text: to != walletModel.currentAccount.address ? "↑" : "↓"
}
StyledText {
id: transactionValue
anchors.left: transferIcon.right
anchors.leftMargin: Style.current.smallPadding
anchors.top: parent.top
anchors.topMargin: Style.current.bigPadding
font.pixelSize: 15
text: value + " " + transactionListItem.symbol
}
}
Item {
anchors.right: timeInfo.left
anchors.top: parent.top
anchors.topMargin: Style.current.bigPadding
width: children[0].width + children[1].width
StyledText {
text: to != walletModel.currentAccount.address ? "To " : "From "
anchors.right: addressValue.left
color: Style.current.darkGrey
anchors.top: parent.top
font.pixelSize: 15
font.strikeout: false
}
StyledText {
id: addressValue
font.family: Style.current.fontHexRegular.name
text: to
width: 100
elide: Text.ElideMiddle
anchors.right: parent.right
anchors.top: parent.top
font.pixelSize: 15
}
}
Item {
id: timeInfo
anchors.right: parent.right
anchors.top: parent.top
anchors.topMargin: Style.current.bigPadding
width: children[0].width + children[1].width + children[2].width
StyledText {
text: "• "
font.weight: Font.Bold
anchors.right: timeIndicator.left
color: Style.current.darkGrey
anchors.top: parent.top
font.pixelSize: 15
}
StyledText {
id: timeIndicator
text: "At "
anchors.right: timeValue.left
color: Style.current.darkGrey
anchors.top: parent.top
font.pixelSize: 15
font.strikeout: false
}
StyledText {
id: timeValue
text: timestamp
anchors.right: parent.right
anchors.top: parent.top
font.pixelSize: 15
anchors.rightMargin: Style.current.smallPadding
}
}
}
}
ListView {
anchors.topMargin: 20
anchors.fill: parent
model: walletModel.transactions
delegate: transactionListItemCmp
anchors.topMargin: 20
anchors.fill: parent
model: walletModel.transactions
delegate: transactionListItemCmp
}
}
@ -196,3 +215,4 @@ Designer {
D{i:0;autoSize:true;height:480;width:640}
}
##^##*/