refactor(@desktop/wallet): load account list

This commit is contained in:
Anthony Laibe 2021-10-19 17:15:03 +02:00 committed by Iuri Matias
parent 7e22ff161c
commit b7eaef8a84
11 changed files with 286 additions and 16 deletions

View File

@ -0,0 +1,26 @@
import ./controller_interface
import ../../../../../app_service/service/wallet_account/service as wallet_account_service
export controller_interface
type
Controller*[T: controller_interface.DelegateInterface] = ref object of controller_interface.AccessInterface
delegate: T
walletAccountService: wallet_account_service.ServiceInterface
proc newController*[T](
delegate: T,
walletAccountService: wallet_account_service.ServiceInterface
): Controller[T] =
result = Controller[T]()
result.delegate = delegate
result.walletAccountService = walletAccountService
method delete*[T](self: Controller[T]) =
discard
method init*[T](self: Controller[T]) =
discard
method getWalletAccounts*[T](self: Controller[T]): seq[wallet_account_service.WalletAccountDto] =
return self.walletAccountService.getWalletAccounts()

View File

@ -0,0 +1,20 @@
import ../../../../../app_service/service/wallet_account/service as wallet_account_service
type
AccessInterface* {.pure inheritable.} = ref object of RootObj
## Abstract class for any input/interaction with this module.
method delete*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method init*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method getWalletAccounts*(self: AccessInterface): seq[wallet_account_service.WalletAccountDto] {.base.} =
raise newException(ValueError, "No implementation available")
type
## Abstract class (concept) which must be implemented by object/s used in this
## module.
DelegateInterface* = concept c

View File

@ -0,0 +1,75 @@
import strformat
type
Item* = object
name: string
address: string
path: string
color: string
publicKey: string
walletType: string
isWallet: bool
isChat: bool
currencyBalance: float64
proc initItem*(
name: string,
address: string,
path: string,
color: string,
publicKey: string,
walletType: string,
isWallet: bool,
isChat: bool,
currencyBalance: float64
): Item =
result.name = name
result.address = address
result.path = path
result.color = color
result.publicKey = publicKey
result.walletType = walletType
result.isWallet = isWallet
result.isChat = isChat
result.currencyBalance = currencyBalance
proc `$`*(self: Item): string =
result = fmt"""AllTokensItem(
name: {self.name},
name: {self.name},
address: {self.address},
path: {self.path},
color: {self.color},
publicKey: {self.publicKey},
walletType: {self.walletType},
isWallet: {self.isWallet},
isChat: {self.isChat},
currencyBalance: {self.currencyBalance},
]"""
proc getName*(self: Item): string =
return self.name
proc getAddress*(self: Item): string =
return self.address
proc getPath*(self: Item): string =
return self.path
proc getColor*(self: Item): string =
return self.color
proc getPublicKey*(self: Item): string =
return self.publicKey
proc getWalletType*(self: Item): string =
return self.walletType
proc getIsWallet*(self: Item): bool =
return self.isWallet
proc getIsChat*(self: Item): bool =
return self.isChat
proc getCurrencyBalance*(self: Item): float64 =
return self.currencyBalance

View File

@ -0,0 +1,96 @@
import NimQml, Tables, strutils, strformat
import ./item
type
ModelRole {.pure.} = enum
Name = UserRole + 1,
Address,
Path,
Color,
PublicKey,
WalletType,
IsWallet,
IsChat,
CurrencyBalance
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.Address.int:"address",
ModelRole.Path.int:"path",
ModelRole.Color.int:"color",
ModelRole.PublicKey.int:"publicKey",
ModelRole.WalletType.int:"walletType",
ModelRole.IsWallet.int:"isWallet",
ModelRole.IsChat.int:"isChat",
ModelRole.CurrencyBalance.int:"currencyBalance"
}.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.Address:
result = newQVariant(item.getAddress())
of ModelRole.Path:
result = newQVariant(item.getPath())
of ModelRole.Color:
result = newQVariant(item.getColor())
of ModelRole.PublicKey:
result = newQVariant(item.getPublicKey())
of ModelRole.WalletType:
result = newQVariant(item.getWalletType())
of ModelRole.IsWallet:
result = newQVariant(item.getIsWallet())
of ModelRole.IsChat:
result = newQVariant(item.getIsChat())
of ModelRole.CurrencyBalance:
result = newQVariant(item.getCurrencyBalance())
proc setItems*(self: Model, items: seq[Item]) =
self.beginResetModel()
self.items = items
self.endResetModel()
self.countChanged()

View File

@ -1,25 +1,50 @@
import sequtils, sugar
import eventemitter
import ./io_interface, ./view
import ./io_interface, ./view, ./item, ./controller
import ../../../../../app_service/service/wallet_account/service as wallet_account_service
export io_interface
type
Module* [T: DelegateInterface] = ref object of AccessInterface
Module* [T: io_interface.DelegateInterface] = ref object of io_interface.AccessInterface
delegate: T
view: View
controller: controller.AccessInterface
moduleLoaded: bool
proc newModule*[T](delegate: T, events: EventEmitter): Module[T] =
proc newModule*[T](
delegate: T,
events: EventEmitter,
walletAccountService: wallet_account_service.ServiceInterface,
): Module[T] =
result = Module[T]()
result.delegate = delegate
result.view = newView()
result.view = newView(result)
result.controller = controller.newController[Module[T]](result, walletAccountService)
result.moduleLoaded = false
method delete*[T](self: Module[T]) =
self.view.delete
self.controller.delete
method load*[T](self: Module[T]) =
let walletAccounts = self.controller.getWalletAccounts()
self.view.setItems(
walletAccounts.map(w => initItem(
w.name,
w.address,
w.path,
w.color,
w.publicKey,
w.walletType,
w.isWallet,
w.isChat,
w.getCurrencyBalance()
))
)
self.moduleLoaded = true
method isLoaded*[T](self: Module[T]): bool =

View File

@ -1,15 +1,36 @@
import NimQml
import ./model
import ./item
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
proc setItems*(self: View, items: seq[Item]) =
self.model.setItems(items)

View File

@ -14,7 +14,11 @@ type
controller: controller.AccessInterface
moduleLoaded: bool
proc newModule*[T](delegate: T, events: EventEmitter, tokenService: token_service.ServiceInterface): Module[T] =
proc newModule*[T](
delegate: T,
events: EventEmitter,
tokenService: token_service.ServiceInterface
): Module[T] =
result = Module[T]()
result.delegate = delegate
result.view = newView(result)

View File

@ -48,7 +48,7 @@ proc newModule*[T](
result.moduleLoaded = false
result.accountTokensModule = account_tokens_module.newModule(result, events)
result.accountsModule = accounts_module.newModule(result, events)
result.accountsModule = accounts_module.newModule(result, events, walletAccountService)
result.allTokensModule = all_tokens_module.newModule(result, events, tokenService)
result.collectiblesModule = collectibles_module.newModule(result, events, collectibleService)
result.mainAccountModule = main_account_module.newModule(result, events)

View File

@ -1,4 +1,4 @@
import json
import json, sequtils, sugar
include ../../common/json_utils
@ -56,4 +56,7 @@ proc toWalletAccountDto*(jsonObj: JsonNode): WalletAccountDto =
discard jsonObj.getProp("wallet", result.isWallet)
discard jsonObj.getProp("chat", result.isChat)
discard jsonObj.getProp("public-key", result.publicKey)
discard jsonObj.getProp("type", result.walletType)
discard jsonObj.getProp("type", result.walletType)
proc getCurrencyBalance*(self: WalletAccountDto): float64 =
return self.tokens.map(t => t.currencyBalance).foldl(a + b, 0.0)

View File

@ -117,8 +117,8 @@ method init*(self: Service) =
error "error: ", errDesription
return
method getAccounts*(self: Service): seq[WalletAccountDto] =
method getWalletAccounts*(self: Service): seq[WalletAccountDto] =
return toSeq(self.accounts.values).filter(a => a.isChat)
method getCurrencyBalance*(self: Service): float64 =
return 0.0
return self.getWalletAccounts().map(a => a.getCurrencyBalance()).foldl(a + b, 0.0)

View File

@ -12,5 +12,5 @@ method delete*(self: ServiceInterface) {.base.} =
method init*(self: ServiceInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method getAccounts*(self: ServiceInterface): seq[WalletAccountDto] {.base.} =
method getWalletAccounts*(self: ServiceInterface): seq[WalletAccountDto] {.base.} =
raise newException(ValueError, "No implementation available")