feat: show accounts in wallet and put assetList in Account

This commit is contained in:
Jonathan Rainville 2020-06-02 16:10:48 -04:00 committed by Iuri Matias
parent 15bd1c3c2c
commit cc85a42b5c
13 changed files with 159 additions and 37 deletions

View File

@ -2,7 +2,6 @@ import NimQml, Tables
import random
import ../../../status/chat
const accountColors* = ["#9B832F", "#D37EF4", "#1D806F", "#FA6565", "#7CDA00", "#887af9", "#8B3131"]
const channelColors* = ["#fa6565", "#7cda00", "#887af9", "#51d0f0", "#FE8F59", "#d37ef4"]
type

View File

@ -4,13 +4,15 @@ import strformat
import strutils
import chronicles
import view
import views/asset_list
import views/account_list
import ../../status/libstatus/wallet as status_wallet
import ../../signals/types
import ../../status/wallet
import ../../status/status
import view
type WalletController* = ref object of SignalSubscriber
status: Status
@ -28,14 +30,26 @@ proc delete*(self: WalletController) =
delete self.variant
proc init*(self: WalletController) =
var symbol = "ETH"
var eth_balance = self.status.wallet.getEthBalance("0x0000000000000000000000000000000000000000")
var usd_balance = self.status.wallet.getFiatValue(eth_balance, symbol, "USD")
let accounts = status_wallet.getAccounts()
var asset = Asset(name:"Ethereum", symbol: symbol, value: fmt"{eth_balance:.6}", fiatValue: "$" & fmt"{usd_balance:.6}", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg")
self.view.addAssetToList(asset)
var totalAccountBalance: float = 0
self.view.setDefaultAccount(status_wallet.getAccount())
const symbol = "ETH"
for address in accounts:
let eth_balance = self.status.wallet.getEthBalance(address)
# TODO get all user assets and add them to balance
let usd_balance = self.status.wallet.getFiatValue(eth_balance, symbol, "USD")
totalAccountBalance = totalAccountBalance + usd_balance
let assetList = newAssetList()
let asset = Asset(name:"Ethereum", symbol: symbol, value: fmt"{eth_balance:.6}", fiatValue: "$" & fmt"{usd_balance:.2f}", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg")
assetList.addAssetToList(asset)
let account = Account(name: "Status Account", address: address, iconColor: "", balance: fmt"{totalAccountBalance:.2f} USD", assetList: assetList)
self.view.addAccountToList(account)
self.view.setDefaultAccount(accounts[0])
method onSignal(self: WalletController, data: Signal) =
debug "New signal received"

View File

@ -1,14 +1,18 @@
import NimQml, Tables
import NimQml
import Tables
import views/asset_list
import views/account_list
import ../../status/wallet
import ../../status/status
import views/asset_list
QtObject:
type
WalletView* = ref object of QAbstractListModel
assets*: AssetsList
accounts*: AccountList
currentAssetList*: AssetList
defaultAccount: string
status: Status
currentAccount: int8
proc delete(self: WalletView) =
self.QAbstractListModel.delete
@ -19,17 +23,36 @@ QtObject:
proc newWalletView*(status: Status): WalletView =
new(result, delete)
result.status = status
result.assets = newAssetsList()
result.accounts = newAccountList()
result.currentAccount = 0
result.currentAssetList = newAssetList() # Temporarily set to an empty list
result.setup
proc addAssetToList*(self: WalletView, asset: Asset) =
self.assets.addAssetToList(asset)
proc currentAssetListChanged*(self: WalletView) {.signal.}
proc getAssetsList(self: WalletView): QVariant {.slot.} =
return newQVariant(self.assets)
proc getCurrentAssetList(self: WalletView): QVariant {.slot.} =
return newQVariant(self.currentAssetList)
proc setCurrentAssetList*(self: WalletView, assetList: AssetList) =
self.currentAssetList = assetList
self.currentAssetListChanged()
QtProperty[QVariant] assets:
read = getAssetsList
read = getCurrentAssetList
write = setCurrentAssetList
notify = currentAssetListChanged
proc addAccountToList*(self: WalletView, account: Account) =
self.accounts.addAccountToList(account)
# If it's the first account we ever get, use its assetList as our currentAssetList
if (self.accounts.rowCount == 1):
self.setCurrentAssetList(account.assetList)
proc getAccountList(self: WalletView): QVariant {.slot.} =
return newQVariant(self.accounts)
QtProperty[QVariant] accounts:
read = getAccountList
proc onSendTransaction*(self: WalletView, from_value: string, to: string, value: string, password: string): string {.slot.} =
result = self.status.wallet.sendTransaction(from_value, to, value, password)

View File

@ -0,0 +1,77 @@
import NimQml
import Tables
import random
import ./asset_list
import ../../../status/wallet
# Need to put the definition here as putting it in status/wallet.nim would create a circular dep
type Account* = ref object
name*, address*, iconColor*, balance*: string
assetList*: AssetList
const accountColors* = [
"#9B832F",
"#D37EF4",
"#1D806F",
"#FA6565",
"#7CDA00",
"#887af9",
"#8B3131"
]
type
AccountRoles {.pure.} = enum
Name = UserRole + 1,
Address = UserRole + 2,
Color = UserRole + 3,
Balance = UserRole + 4
QtObject:
type AccountList* = ref object of QAbstractListModel
accounts*: seq[Account]
proc setup(self: AccountList) = self.QAbstractListModel.setup
proc delete(self: AccountList) =
self.QAbstractListModel.delete
self.accounts = @[]
proc newAccountList*(): AccountList =
new(result, delete)
result.accounts = @[]
result.setup
proc getAccountByIndex*(self: AccountList, index: int8): Account =
if (index >= self.accounts.len):
raise newException(ValueError, "Index out of bounds for accounts")
result = self.accounts[index]
method rowCount*(self: AccountList, index: QModelIndex = nil): int =
return self.accounts.len
method data(self: AccountList, index: QModelIndex, role: int): QVariant =
if not index.isValid:
return
if index.row < 0 or index.row >= self.accounts.len:
return
let account = self.accounts[index.row]
let accountRole = role.AccountRoles
case accountRole:
of AccountRoles.Name: result = newQVariant(account.name)
of AccountRoles.Address: result = newQVariant(account.address)
of AccountRoles.Color: result = newQVariant(account.iconColor)
of AccountRoles.Balance: result = newQVariant(account.balance)
method roleNames(self: AccountList): Table[int, string] =
{ AccountRoles.Name.int:"name",
AccountRoles.Address.int:"address",
AccountRoles.Color.int:"iconColor",
AccountRoles.Balance.int:"balance" }.toTable
proc addAccountToList*(self: AccountList, account: Account) =
if account.iconColor == "":
randomize()
account.iconColor = accountColors[rand(accountColors.len - 1)]
self.beginInsertRows(newQModelIndex(), self.accounts.len, self.accounts.len)
self.accounts.add(account)
self.endInsertRows()

View File

@ -1,6 +1,5 @@
import NimQml
import Tables
import strformat
import tables
import ../../../status/wallet
type
@ -12,24 +11,24 @@ type
Image = UserRole + 5
QtObject:
type AssetsList* = ref object of QAbstractListModel
type AssetList* = ref object of QAbstractListModel
assets*: seq[Asset]
proc setup(self: AssetsList) = self.QAbstractListModel.setup
proc setup(self: AssetList) = self.QAbstractListModel.setup
proc delete(self: AssetsList) =
proc delete(self: AssetList) =
self.QAbstractListModel.delete
self.assets = @[]
proc newAssetsList*(): AssetsList =
proc newAssetList*(): AssetList =
new(result, delete)
result.assets = @[]
result.setup
method rowCount(self: AssetsList, index: QModelIndex = nil): int =
method rowCount(self: AssetList, index: QModelIndex = nil): int =
return self.assets.len
method data(self: AssetsList, index: QModelIndex, role: int): QVariant =
method data(self: AssetList, index: QModelIndex, role: int): QVariant =
if not index.isValid:
return
if index.row < 0 or index.row >= self.assets.len:
@ -43,14 +42,14 @@ QtObject:
of AssetRoles.FiatValue: result = newQVariant(asset.fiatValue)
of AssetRoles.Image: result = newQVariant(asset.image)
method roleNames(self: AssetsList): Table[int, string] =
method roleNames(self: AssetList): Table[int, string] =
{ AssetRoles.Name.int:"name",
AssetRoles.Symbol.int:"symbol",
AssetRoles.Value.int:"value",
AssetRoles.FiatValue.int:"fiatValue",
AssetRoles.Image.int:"image" }.toTable
proc addAssetToList*(self: AssetsList, asset: Asset) =
proc addAssetToList*(self: AssetList, asset: Asset) =
self.beginInsertRows(newQModelIndex(), self.assets.len, self.assets.len)
self.assets.add(asset)
self.endInsertRows()

View File

@ -38,7 +38,7 @@ proc mainProc() =
signalsQObjPointer = cast[pointer](signalController.vptr)
var wallet = wallet.newController(status)
engine.setRootContextProperty("assetsModel", wallet.variant)
engine.setRootContextProperty("walletModel", wallet.variant)
var chat = chat.newController(status)
engine.setRootContextProperty("chatsModel", chat.variant)

View File

@ -1,5 +1,4 @@
import eventemitter
import json
import strformat
import strutils
import libstatus/wallet as status_wallet

View File

@ -31,7 +31,7 @@ Item {
onDataChanged: {
// If the current active channel receives messages and changes its position,
// refresh the currentIndex accordingly
if(chatsModel.activeChannelIndex != chatGroupsListView.currentIndex){
if(chatsModel.activeChannelIndex !== chatGroupsListView.currentIndex){
chatGroupsListView.currentIndex = chatsModel.activeChannelIndex
}
}

View File

@ -34,7 +34,7 @@ Item {
}
Text {
id: assetFullTokenName
text: fullTokenName
text: name
anchors.bottom: parent.bottom
anchors.bottomMargin: 0
anchors.left: assetInfoImage.right
@ -80,7 +80,7 @@ Item {
anchors.topMargin: 20
anchors.fill: parent
// model: exampleModel
model: assetsModel.assets
model: walletModel.assets
delegate: assetViewDelegate
}
}

View File

@ -119,7 +119,7 @@ Item {
anchors.bottom: parent.bottom
anchors.bottomMargin: Theme.padding
onClicked: {
let result = assetsModel.onSendTransaction(txtFrom.text,
let result = walletModel.onSendTransaction(txtFrom.text,
txtTo.text,
txtValue.text,
txtPassword.text)

View File

@ -89,7 +89,7 @@ Item {
ColorOverlay {
anchors.fill: walletIcon
source: walletIcon
color: selected ? Theme.transparent : "#7CDA00" // change image color
color: selected ? Theme.transparent : iconColor // change image color
}
Text {
id: walletName
@ -105,6 +105,9 @@ Item {
Text {
id: walletAddress
text: address
anchors.right: parent.right
anchors.rightMargin: parent.width/2
elide: Text.ElideMiddle
anchors.bottom: parent.bottom
anchors.bottomMargin: Theme.smallPadding
anchors.left: walletName.left
@ -148,24 +151,31 @@ Item {
delegate: walletDelegate
model: ListModel {
ListModel {
id: exampleWalletModel
ListElement {
name: "Status account"
address: "0x2Ef1...E0Ba"
address: "0xcfc9f08bbcbcb80760e8cb9a3c1232d19662fc6f"
balance: "12.00 USD"
iconColor: "#7CDA00"
}
ListElement {
name: "Test account 1"
address: "0x2Ef1...E0Ba"
balance: "12.00 USD"
iconColor: "#FA6565"
}
ListElement {
name: "Status account"
address: "0x2Ef1...E0Ba"
balance: "12.00 USD"
iconColor: "#7CDA00"
}
}
model: walletModel.getAccountList()
// model: exampleWalletModel
}
}

View File

@ -11,7 +11,7 @@ Item {
popup.open()
sendModalContent.valueInput.text = ""
sendModalContent.valueInput.forceActiveFocus(Qt.MouseFocusReason)
sendModalContent.defaultAccount = assetsModel.getDefaultAccount()
sendModalContent.defaultAccount = walletModel.getDefaultAccount()
}
function close() {

View File

@ -8,6 +8,7 @@ Item {
property string placeholderText: "My placeholder"
property string text: ""
property string label: ""
// property string label: "My Label"
// property url icon: "../app/img/hash.svg"
property url icon: ""