refactor(@desktop/wallet): Add getToken and controller

This commit is contained in:
Anthony Laibe 2021-10-14 10:42:18 +02:00 committed by Iuri Matias
parent d9b6153ed0
commit 27d92f32f9
9 changed files with 94 additions and 14 deletions

View File

@ -0,0 +1,26 @@
import ./controller_interface
import ../../../../../app_service/service/token/service as token_service
export controller_interface
type
Controller*[T: controller_interface.DelegateInterface] = ref object of controller_interface.AccessInterface
delegate: T
tokenService: token_service.ServiceInterface
proc newController*[T](
delegate: T,
tokenService: token_service.ServiceInterface
): Controller[T] =
result = Controller[T]()
result.delegate = delegate
result.tokenService = tokenService
method delete*[T](self: Controller[T]) =
discard
method init*[T](self: Controller[T]) =
discard
method getTokens*[T](self: Controller[T], chainId: int): seq[token_service.Dto] =
return self.tokenService.getTokens(chainId)

View File

@ -0,0 +1,20 @@
import ../../../../../app_service/service/token/service_interface as token_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 getTokens*(self: AccessInterface, chainId: int): seq[token_service.Dto] {.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

@ -77,5 +77,8 @@ QtObject:
of ModelRole.IsCustom:
result = newQVariant(item.getIsCustom())
proc setData*(self: Model, item: seq[Item]) =
proc setItems*(self: Model, items: seq[Item]) =
self.beginResetModel()
self.items = items
self.endResetModel()
self.countChanged()

View File

@ -1,23 +1,42 @@
import ./io_interface, ./view
import sequtils, sugar
import ./io_interface, ./view, ./controller, ./item
import ../../../../../app_service/service/token/service as token_service
export io_interface
type
Module* [T: DelegateInterface] = ref object of AccessInterface
type
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): Module[T] =
proc newModule*[T](delegate: T, tokenService: token_service.ServiceInterface): Module[T] =
result = Module[T]()
result.delegate = delegate
result.view = newView(result)
result.controller = controller.newController[Module[T]](result, tokenService)
result.moduleLoaded = false
method delete*[T](self: Module[T]) =
self.view.delete
self.controller.delete
method load*[T](self: Module[T]) =
# TODO: replace chainID 1 with chain ID coming from settings
let tokens = self.controller.getTokens(1)
self.view.setItems(
tokens.map(t => initItem(
t.name,
t.symbol,
t.hasIcon,
t.addressAsString(),
t.decimals,
t.isCustom
))
)
self.moduleLoaded = true
method isLoaded*[T](self: Module[T]): bool =

View File

@ -1,6 +1,7 @@
import NimQml
import ./model
import ./item
import ./io_interface
QtObject:
@ -29,4 +30,7 @@ QtObject:
QtProperty[QVariant] model:
read = getModel
notify = modelChanged
notify = modelChanged
proc setItems*(self: View, items: seq[Item]) =
self.model.setItems(items)

View File

@ -35,7 +35,7 @@ proc newModule*[T](delegate: T, tokenService: token_service.Service): Module[T]
result.accountTokensModule = account_tokens_module.newModule(result)
result.accountsModule = accounts_module.newModule(result)
result.allTokensModule = all_tokens_module.newModule(result)
result.allTokensModule = all_tokens_module.newModule(result, tokenService)
result.collectiblesModule = collectibles_module.newModule(result)
result.mainAccountModule = main_account_module.newModule(result)
result.transactionsModule = transactions_module.newModule(result)

View File

@ -4,6 +4,7 @@ include ../../common/json_utils
import
web3/ethtypes, json_serialization
from web3/conversions import `$`
type
Dto* = ref object of RootObj
@ -14,12 +15,18 @@ type
decimals*: int
hasIcon* {.dontSerialize.}: bool
color*: string
isCustom* {.dontSerialize.}: bool
proc newDto*(name: string, chainId: int, address: Address, symbol: string, decimals: int, hasIcon: bool): Dto =
Dto(name: name, chainId: chainId, address: address, symbol: symbol, decimals: decimals, hasIcon: hasIcon)
proc newDto*(
name: string, chainId: int, address: Address, symbol: string, decimals: int, hasIcon: bool, isCustom: bool = false
): Dto =
Dto(
name: name, chainId: chainId, address: address, symbol: symbol, decimals: decimals, hasIcon: hasIcon, isCustom: isCustom
)
proc toDto*(jsonObj: JsonNode): Dto =
result = Dto()
result.isCustom = true
discard jsonObj.getProp("name", result.name)
discard jsonObj.getProp("chainId", result.chainId)
discard jsonObj.getProp("address", result.address)
@ -27,4 +34,5 @@ proc toDto*(jsonObj: JsonNode): Dto =
discard jsonObj.getProp("decimals", result.decimals)
discard jsonObj.getProp("color", result.color)
proc addressAsString*(self: Dto): string =
return $self.address

View File

@ -1,4 +1,4 @@
import json, sequtils, strformat, chronicles
import json, sequtils, chronicles
import status/statusgo_backend_new/custom_tokens as custom_tokens
import ./service_interface, ./dto, ./static_token
@ -33,5 +33,5 @@ method init*(self: Service) =
error "error: ", errDesription
return
method getTokens*(self: Service): seq[Dto] =
return self.tokens
method getTokens*(self: Service, chainId: int): seq[Dto] =
return self.tokens.filter(proc(x: Dto): bool = x.chainId == chainId)

View File

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