feat: show the real value of the current account in the wallet

This commit is contained in:
Jonathan Rainville 2020-06-03 15:59:18 -04:00 committed by Iuri Matias
parent 24c31e04f6
commit 15dc98b44a
6 changed files with 103 additions and 24 deletions

View File

@ -7,6 +7,7 @@ import chronicles
import view
import views/asset_list
import views/account_list
import views/account_item
import ../../status/libstatus/wallet as status_wallet
import ../../signals/types
@ -47,7 +48,7 @@ proc init*(self: WalletController) =
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} {defaultCurrency}", assetList: assetList)
let account = Account(name: "Status Account", address: address, iconColor: "", balance: fmt"{totalAccountBalance:.2f} {defaultCurrency}", assetList: assetList, realFiatBalance: totalAccountBalance)
self.view.addAccountToList(account)
self.view.setDefaultAccount(accounts[0])

View File

@ -1,7 +1,9 @@
import NimQml
import Tables
import strformat
import views/asset_list
import views/account_list
import views/account_item
import ../../status/wallet
import ../../status/status
@ -10,9 +12,10 @@ QtObject:
WalletView* = ref object of QAbstractListModel
accounts*: AccountList
currentAssetList*: AssetList
currentAccount: AccountItemView
defaultAccount: string
status: Status
currentAccount: int8
totalFiatBalance: float
proc delete(self: WalletView) =
self.QAbstractListModel.delete
@ -24,10 +27,28 @@ QtObject:
new(result, delete)
result.status = status
result.accounts = newAccountList()
result.currentAccount = 0
result.currentAccount = newAccountItemView()
result.currentAssetList = newAssetList() # Temporarily set to an empty list
result.setup
proc currentAccountChanged*(self: WalletView) {.signal.}
proc setCurrentAccountByIndex*(self: WalletView, index: int) {.slot.} =
if(self.accounts.rowCount() == 0): return
let selectedAccount = self.accounts.getAccount(index)
if self.currentAccount.address == selectedAccount.address: return
self.currentAccount.setAccountItem(selectedAccount)
self.currentAccountChanged()
proc getCurrentAccount*(self: WalletView): QVariant {.slot.} =
result = newQVariant(self.currentAccount)
QtProperty[QVariant] currentAccount:
read = getCurrentAccount
write = setCurrentAccountByIndex
notify = currentAccountChanged
proc currentAssetListChanged*(self: WalletView) {.signal.}
proc getCurrentAssetList(self: WalletView): QVariant {.slot.} =
@ -41,18 +62,38 @@ QtObject:
read = getCurrentAssetList
write = setCurrentAssetList
notify = currentAssetListChanged
proc totalFiatBalanceChanged*(self: WalletView) {.signal.}
proc getTotalFiatBalance(self: WalletView): QVariant {.slot.} =
return newQVariant(fmt"{self.totalFiatBalance:.2f} USD") # TODO use user's currency
proc setTotalFiatBalance*(self: WalletView, newBalance: float) =
self.totalFiatBalance = newBalance
self.totalFiatBalanceChanged()
QtProperty[QVariant] totalFiatBalance:
read = getTotalFiatBalance
write = setTotalFiatBalance
notify = currentAssetListChanged
proc accountListChanged*(self: WalletView) {.signal.}
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)
self.setCurrentAccountByIndex(0)
self.setTotalFiatBalance(account.realFiatBalance + self.totalFiatBalance)
self.accountListChanged()
proc getAccountList(self: WalletView): QVariant {.slot.} =
return newQVariant(self.accounts)
QtProperty[QVariant] accounts:
read = getAccountList
notify = accountListChanged
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,42 @@
import NimQml
import std/wrapnils
import ./asset_list
type Account* = ref object
name*, address*, iconColor*, balance*: string
realFiatBalance*: float
assetList*: AssetList
QtObject:
type AccountItemView* = ref object of QObject
account*: Account
proc setup(self: AccountItemView) =
self.QObject.setup
proc delete*(self: AccountItemView) =
self.QObject.delete
proc newAccountItemView*(): AccountItemView =
new(result, delete)
result = AccountItemView()
result.setup
proc setAccountItem*(self: AccountItemView, account: Account) =
self.account = account
proc name*(self: AccountItemView): string {.slot.} = result = ?.self.account.name
QtProperty[string] name:
read = name
proc address*(self: AccountItemView): string {.slot.} = result = ?.self.account.address
QtProperty[string] address:
read = address
proc iconColor*(self: AccountItemView): string {.slot.} = result = ?.self.account.iconColor
QtProperty[string] iconColor:
read = iconColor
proc balance*(self: AccountItemView): string {.slot.} = result = ?.self.account.balance
QtProperty[string] balance:
read = balance

View File

@ -2,13 +2,9 @@ import NimQml
import Tables
import random
import ./asset_list
import ./account_item
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",
@ -41,10 +37,7 @@ QtObject:
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]
proc getAccount*(self: AccountList, index: int): Account = self.accounts[index]
method rowCount*(self: AccountList, index: QModelIndex = nil): int =
return self.accounts.len

View File

@ -9,7 +9,7 @@ import "../../../shared"
import "./Components"
Item {
property int selectedWallet: 0
property int selectedAccount: 0
id: walletInfoContainer
width: 340
@ -36,7 +36,7 @@ Item {
TextEdit {
id: walletAmountValue
color: "black"
text: qsTr("408.30 USD")
text: walletModel.totalFiatBalance
selectByMouse: true
cursorVisible: true
readOnly: true
@ -65,7 +65,7 @@ Item {
id: walletDelegate
Rectangle {
property bool selected: index == selectedWallet
property bool selected: index == selectedAccount
id: rectangle
height: 64
@ -131,8 +131,8 @@ Item {
MouseArea {
anchors.fill: parent
onClicked: {
selectedWallet = index
// TODO add call to Nim to change the wallet for real
selectedAccount = index
walletModel.setCurrentAccountByIndex(index)
}
}
}
@ -174,7 +174,7 @@ Item {
}
}
model: walletModel.getAccountList()
model: walletModel.accounts
// model: exampleWalletModel
}
}

View File

@ -6,6 +6,8 @@ import "../../../imports"
import "../../../shared"
Item {
property var currentAccount: walletModel.currentAccount
id: walletHeader
height: walletAddress.y + walletAddress.height
anchors.right: parent.right
@ -19,8 +21,7 @@ Item {
Text {
id: title
// TODO this should be the name of the wallet
text: qsTr("Status account")
text: currentAccount.name
anchors.top: parent.top
anchors.topMargin: 56
anchors.left: parent.left
@ -43,8 +44,7 @@ Item {
Text {
id: walletBalance
// TODO this should be the balance
text: qsTr("12.00 USD")
text: currentAccount.balance
anchors.left: separatorDot.right
anchors.leftMargin: 8
anchors.verticalCenter: title.verticalCenter
@ -53,8 +53,10 @@ Item {
Text {
id: walletAddress
// TODO this should be the address and an actual Address component that can shrink and expend
text: qsTr("0X2Ef1...E0Ba")
text: currentAccount.address
elide: Text.ElideMiddle
anchors.right: title.right
anchors.rightMargin: 0
anchors.top: title.bottom
anchors.topMargin: 0
anchors.left: title.left