From a962069b730eef80c35995210003820314592880 Mon Sep 17 00:00:00 2001 From: Jonathan Rainville Date: Thu, 14 May 2020 16:04:38 -0400 Subject: [PATCH] feat: hook wallet assets to nim model --- src/assets.nim | 67 ++++++++++++++++++++++++++++ src/nim_status_client.nim | 10 +++++ ui/app/AppLayouts/Wallet/LeftTab.qml | 27 ++++------- 3 files changed, 86 insertions(+), 18 deletions(-) create mode 100644 src/assets.nim diff --git a/src/assets.nim b/src/assets.nim new file mode 100644 index 0000000000..9c921f847b --- /dev/null +++ b/src/assets.nim @@ -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 diff --git a/src/nim_status_client.nim b/src/nim_status_client.nim index d3f343980e..903bc15fd8 100644 --- a/src/nim_status_client.nim +++ b/src/nim_status_client.nim @@ -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") diff --git a/ui/app/AppLayouts/Wallet/LeftTab.qml b/ui/app/AppLayouts/Wallet/LeftTab.qml index 1ef1179ba3..735bb9b59d 100644 --- a/ui/app/AppLayouts/Wallet/LeftTab.qml +++ b/ui/app/AppLayouts/Wallet/LeftTab.qml @@ -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} +} +##^##*/