refactor(@desktop/wallet): Add account tokens model/item
This commit is contained in:
parent
9e6a1bdf57
commit
1088669a6e
|
@ -0,0 +1,46 @@
|
||||||
|
import strformat
|
||||||
|
|
||||||
|
type
|
||||||
|
Item* = object
|
||||||
|
name: string
|
||||||
|
symbol: string
|
||||||
|
value: float
|
||||||
|
address: string
|
||||||
|
fiatBalance: float
|
||||||
|
fiatBalanceDisplay: string
|
||||||
|
|
||||||
|
proc initItem*(name, symbol: string, value: float, address: string, fiatBalance: float, fiatBalanceDisplay: string): Item =
|
||||||
|
result.name = name
|
||||||
|
result.symbol = symbol
|
||||||
|
result.value = value
|
||||||
|
result.address = address
|
||||||
|
result.fiatBalance = fiatBalance
|
||||||
|
result.fiatBalanceDisplay = fiatBalanceDisplay
|
||||||
|
|
||||||
|
proc `$`*(self: Item): string =
|
||||||
|
result = fmt"""AllTokensItem(
|
||||||
|
name: {self.name},
|
||||||
|
symbol: {self.symbol},
|
||||||
|
value: {self.value},
|
||||||
|
address: {self.address},
|
||||||
|
fiatBalance: {self.fiatBalance},
|
||||||
|
fiatBalanceDisplay: {self.fiatBalanceDisplay}
|
||||||
|
]"""
|
||||||
|
|
||||||
|
proc getName*(self: Item): string =
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
proc getSymbol*(self: Item): string =
|
||||||
|
return self.symbol
|
||||||
|
|
||||||
|
proc getValue*(self: Item): float =
|
||||||
|
return self.value
|
||||||
|
|
||||||
|
proc getAddress*(self: Item): string =
|
||||||
|
return self.address
|
||||||
|
|
||||||
|
proc getFiatBalance*(self: Item): float =
|
||||||
|
return self.fiatBalance
|
||||||
|
|
||||||
|
proc getFiatBalanceDisplay*(self: Item): string =
|
||||||
|
return self.fiatBalanceDisplay
|
|
@ -0,0 +1,81 @@
|
||||||
|
import NimQml, Tables, strutils, strformat
|
||||||
|
|
||||||
|
import ./item
|
||||||
|
|
||||||
|
type
|
||||||
|
ModelRole {.pure.} = enum
|
||||||
|
Name = UserRole + 1,
|
||||||
|
Symbol
|
||||||
|
Value
|
||||||
|
Address
|
||||||
|
FiatBalanceDisplay
|
||||||
|
FiatBalance
|
||||||
|
|
||||||
|
QtObject:
|
||||||
|
type
|
||||||
|
Model* = ref object of QAbstractListModel
|
||||||
|
items: seq[Item]
|
||||||
|
|
||||||
|
proc delete(self: Model) =
|
||||||
|
self.items = @[]
|
||||||
|
self.QAbstractListModel.delete
|
||||||
|
|
||||||
|
proc setup(self: Model) =
|
||||||
|
self.QAbstractListModel.setup
|
||||||
|
|
||||||
|
proc newModel*(): Model =
|
||||||
|
new(result, delete)
|
||||||
|
result.setup
|
||||||
|
|
||||||
|
proc `$`*(self: Model): string =
|
||||||
|
for i in 0 ..< self.items.len:
|
||||||
|
result &= fmt"""[{i}]:({$self.items[i]})"""
|
||||||
|
|
||||||
|
proc countChanged(self: Model) {.signal.}
|
||||||
|
|
||||||
|
proc getCount(self: Model): int {.slot.} =
|
||||||
|
self.items.len
|
||||||
|
|
||||||
|
QtProperty[int] count:
|
||||||
|
read = getCount
|
||||||
|
notify = countChanged
|
||||||
|
|
||||||
|
method rowCount(self: Model, index: QModelIndex = nil): int =
|
||||||
|
return self.items.len
|
||||||
|
|
||||||
|
method roleNames(self: Model): Table[int, string] =
|
||||||
|
{
|
||||||
|
ModelRole.Name.int:"name",
|
||||||
|
ModelRole.Symbol.int:"symbol",
|
||||||
|
ModelRole.Value.int:"value",
|
||||||
|
ModelRole.Address.int:"address",
|
||||||
|
ModelRole.FiatBalanceDisplay.int:"fiatBalanceDisplay",
|
||||||
|
ModelRole.FiatBalance.int:"fiatBalance"
|
||||||
|
}.toTable
|
||||||
|
|
||||||
|
method data(self: Model, index: QModelIndex, role: int): QVariant =
|
||||||
|
if (not index.isValid):
|
||||||
|
return
|
||||||
|
|
||||||
|
if (index.row < 0 or index.row >= self.items.len):
|
||||||
|
return
|
||||||
|
|
||||||
|
let item = self.items[index.row]
|
||||||
|
let enumRole = role.ModelRole
|
||||||
|
|
||||||
|
case enumRole:
|
||||||
|
of ModelRole.Name:
|
||||||
|
result = newQVariant(item.getName())
|
||||||
|
of ModelRole.Symbol:
|
||||||
|
result = newQVariant(item.getSymbol())
|
||||||
|
of ModelRole.Value:
|
||||||
|
result = newQVariant(item.getValue())
|
||||||
|
of ModelRole.Address:
|
||||||
|
result = newQVariant(item.getAddress())
|
||||||
|
of ModelRole.FiatBalanceDisplay:
|
||||||
|
result = newQVariant(item.getFiatBalanceDisplay())
|
||||||
|
of ModelRole.FiatBalance:
|
||||||
|
result = newQVariant(item.getFiatBalance())
|
||||||
|
|
||||||
|
proc setData*(self: Model, item: seq[Item]) =
|
||||||
|
self.countChanged()
|
|
@ -11,7 +11,7 @@ type
|
||||||
proc newModule*[T](delegate: T): Module[T] =
|
proc newModule*[T](delegate: T): Module[T] =
|
||||||
result = Module[T]()
|
result = Module[T]()
|
||||||
result.delegate = delegate
|
result.delegate = delegate
|
||||||
result.view = newView()
|
result.view = newView(result)
|
||||||
result.moduleLoaded = false
|
result.moduleLoaded = false
|
||||||
|
|
||||||
method delete*[T](self: Module[T]) =
|
method delete*[T](self: Module[T]) =
|
||||||
|
|
|
@ -1,15 +1,32 @@
|
||||||
import NimQml
|
import NimQml
|
||||||
|
|
||||||
|
import ./model
|
||||||
|
import ./io_interface
|
||||||
|
|
||||||
QtObject:
|
QtObject:
|
||||||
type
|
type
|
||||||
View* = ref object of QObject
|
View* = ref object of QObject
|
||||||
|
delegate: io_interface.AccessInterface
|
||||||
proc setup(self: View) =
|
model: Model
|
||||||
self.QObject.setup
|
modelVariant: QVariant
|
||||||
|
|
||||||
proc delete*(self: View) =
|
proc delete*(self: View) =
|
||||||
|
self.model.delete
|
||||||
|
self.modelVariant.delete
|
||||||
self.QObject.delete
|
self.QObject.delete
|
||||||
|
|
||||||
proc newView*(): View =
|
proc newView*(delegate: io_interface.AccessInterface): View =
|
||||||
new(result, delete)
|
new(result, delete)
|
||||||
result.setup()
|
result.QObject.setup
|
||||||
|
result.delegate = delegate
|
||||||
|
result.model = newModel()
|
||||||
|
result.modelVariant = newQVariant(result.model)
|
||||||
|
|
||||||
|
proc modelChanged*(self: View) {.signal.}
|
||||||
|
|
||||||
|
proc getModel(self: View): QVariant {.slot.} =
|
||||||
|
return self.modelVariant
|
||||||
|
|
||||||
|
QtProperty[QVariant] model:
|
||||||
|
read = getModel
|
||||||
|
notify = modelChanged
|
Loading…
Reference in New Issue