From 1088669a6eeccd4b721336166cdd05a9b5c90405 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 13 Oct 2021 11:48:17 +0200 Subject: [PATCH] refactor(@desktop/wallet): Add account tokens model/item --- .../wallet_section/account_tokens/item.nim | 46 +++++++++++ .../wallet_section/account_tokens/model.nim | 81 +++++++++++++++++++ .../wallet_section/account_tokens/module.nim | 2 +- .../wallet_section/account_tokens/view.nim | 27 +++++-- 4 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 src/app/modules/main/wallet_section/account_tokens/item.nim create mode 100644 src/app/modules/main/wallet_section/account_tokens/model.nim diff --git a/src/app/modules/main/wallet_section/account_tokens/item.nim b/src/app/modules/main/wallet_section/account_tokens/item.nim new file mode 100644 index 0000000000..a0ff9bb465 --- /dev/null +++ b/src/app/modules/main/wallet_section/account_tokens/item.nim @@ -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 \ No newline at end of file diff --git a/src/app/modules/main/wallet_section/account_tokens/model.nim b/src/app/modules/main/wallet_section/account_tokens/model.nim new file mode 100644 index 0000000000..0dccf0f0e0 --- /dev/null +++ b/src/app/modules/main/wallet_section/account_tokens/model.nim @@ -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() \ No newline at end of file diff --git a/src/app/modules/main/wallet_section/account_tokens/module.nim b/src/app/modules/main/wallet_section/account_tokens/module.nim index fae63721bb..920799f36e 100644 --- a/src/app/modules/main/wallet_section/account_tokens/module.nim +++ b/src/app/modules/main/wallet_section/account_tokens/module.nim @@ -11,7 +11,7 @@ type proc newModule*[T](delegate: T): Module[T] = result = Module[T]() result.delegate = delegate - result.view = newView() + result.view = newView(result) result.moduleLoaded = false method delete*[T](self: Module[T]) = diff --git a/src/app/modules/main/wallet_section/account_tokens/view.nim b/src/app/modules/main/wallet_section/account_tokens/view.nim index c75634662b..519f0900cf 100644 --- a/src/app/modules/main/wallet_section/account_tokens/view.nim +++ b/src/app/modules/main/wallet_section/account_tokens/view.nim @@ -1,15 +1,32 @@ import NimQml +import ./model +import ./io_interface + QtObject: type View* = ref object of QObject - - proc setup(self: View) = - self.QObject.setup + delegate: io_interface.AccessInterface + model: Model + modelVariant: QVariant proc delete*(self: View) = + self.model.delete + self.modelVariant.delete self.QObject.delete - proc newView*(): View = + proc newView*(delegate: io_interface.AccessInterface): View = 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 \ No newline at end of file