feat: hook wallet assets to nim model

This commit is contained in:
Jonathan Rainville 2020-05-14 16:04:38 -04:00 committed by Iuri Matias
parent cd522ba803
commit a962069b73
3 changed files with 86 additions and 18 deletions

67
src/assets.nim Normal file
View File

@ -0,0 +1,67 @@
import NimQml
import Tables
type
AssetRoles {.pure.} = enum
Name = UserRole + 1,
Symbol = UserRole + 2,
Value = UserRole + 3,
FiatValue = UserRole + 4,
Image = UserRole + 5
type
Asset* = ref object of QObject
name*, symbol*, value*, fiatValue*, image*: string
QtObject:
type
AssetsModel* = ref object of QAbstractListModel
assets*: seq[Asset]
proc delete(self: AssetsModel) =
self.QAbstractListModel.delete
for asset in self.assets:
asset.delete
self.assets = @[]
proc setup(self: AssetsModel) =
self.QAbstractListModel.setup
proc newAssetsModel*(): AssetsModel =
new(result, delete)
result.assets = @[]
result.setup
proc addAssetToList*(self: AssetsModel, name: string, symbol: string, value: string, fiatValue: string) {.slot.} =
self.beginInsertRows(newQModelIndex(), self.assets.len, self.assets.len)
self.assets.add(Asset(name : name,
symbol : symbol,
value : value,
fiatValue: fiatValue,
image: ""))
self.endInsertRows()
method rowCount(self: AssetsModel, index: QModelIndex = nil): int =
return self.assets.len
method data(self: AssetsModel, index: QModelIndex, role: int): QVariant =
if not index.isValid:
return
if index.row < 0 or index.row >= self.assets.len:
return
let asset = self.assets[index.row]
let assetRole = role.AssetRoles
case assetRole:
of AssetRoles.Name: result = newQVariant(asset.name)
of AssetRoles.Symbol: result = newQVariant(asset.symbol)
of AssetRoles.Value: result = newQVariant(asset.value)
of AssetRoles.FiatValue: result = newQVariant(asset.fiatValue)
of AssetRoles.Image: result = newQVariant(asset.image)
method roleNames(self: AssetsModel): Table[int, string] =
{ AssetRoles.Name.int:"name",
AssetRoles.Symbol.int:"symbol",
AssetRoles.Value.int:"value",
AssetRoles.FiatValue.int:"fiatValue",
AssetRoles.Image.int:"image" }.toTable

View File

@ -1,6 +1,7 @@
import NimQml
import applicationView
import chats
import assets
import json
import state
import status/utils
@ -28,6 +29,9 @@ proc mainProc() =
var chatsModel = newChatsModel();
defer: chatsModel.delete
var assetsModel = newAssetsModel();
defer: assetsModel.delete
var engine = newQQmlApplicationEngine()
defer: engine.delete()
@ -76,6 +80,9 @@ proc mainProc() =
chatsModel.addNameTolist(channel.name)
)
let assetsVariant = newQVariant(assetsModel)
defer: chatsVariant.delete
status.startMessenger()
appState.addChannel("test")
@ -83,6 +90,9 @@ proc mainProc() =
engine.setRootContextProperty("logic", logicVariant)
engine.setRootContextProperty("chatsModel", chatsVariant)
engine.setRootContextProperty("assetsModel", assetsVariant)
assetsModel.addAssetToList("Ethereum", "ETH", fmt"{eth_value:.6}", "$" & fmt"{usd_balance:.6}")
engine.load("../ui/main.qml")

View File

@ -193,13 +193,11 @@ ColumnLayout {
RowLayout {
id: assetInfoContainer
width: 100
height: 100
Layout.fillHeight: true
Layout.fillWidth: true
Rectangle {
id: walletSendBg
width: 200
height: 200
color: "#ffffff"
Layout.fillHeight: true
Layout.fillWidth: true
@ -266,26 +264,19 @@ ColumnLayout {
}
}
// Delete this when we have a real model
ListModel {
id: exampleAssets
ListElement {
name: "Ethereum"
symbol: "ETH"
value: "0.4564234124124..."
fiatValue: "$268.30"
image: ""
}
}
ListView {
id: listView
anchors.topMargin: 36
anchors.fill: parent
// Change this to the real model
model: exampleAssets
model: assetsModel
delegate: assetViewDelegate
}
}
}
}
/*##^##
Designer {
D{i:0;formeditorZoom:0.8999999761581421;height:770;width:340}
}
##^##*/