refactor(@desktop/wallet): Init collectible service

This commit is contained in:
Anthony Laibe 2021-10-15 11:12:48 +02:00 committed by Iuri Matias
parent 8203643f86
commit 20a636d912
13 changed files with 205 additions and 16 deletions

View File

@ -8,6 +8,8 @@ import ../../app_service/service/chat/service as chat_service
import ../../app_service/service/community/service as community_service
import ../../app_service/service/token/service as token_service
import ../../app_service/service/transaction/service as transaction_service
import ../../app_service/service/collectible/service as collectible_service
import ../core/local_account_settings
import ../modules/startup/module as startup_module
@ -61,11 +63,11 @@ type
communityService: community_service.Service
tokenService: token_service.Service
transactionService: transaction_service.Service
collectibleService: collectible_service.Service
# Core
localAccountSettings: LocalAccountSettings
localAccountSettingsVariant: QVariant
startupModule: startup_module.AccessInterface
mainModule: main_module.AccessInterface
#################################################
@ -116,6 +118,7 @@ proc newAppController*(appService: AppService): AppController =
result.communityService = community_service.newService(result.chatService)
result.tokenService = token_service.newService()
result.transactionService = transaction_service.newService()
result.collectibleService = collectible_service.newService()
# Core
result.localAccountSettingsVariant = newQVariant(
@ -132,7 +135,8 @@ proc newAppController*(appService: AppService): AppController =
result.chatService,
result.communityService,
result.tokenService,
result.transactionService
result.transactionService,
result.collectibleService
)
#################################################

View File

@ -1,5 +1,3 @@
import NimQml, Tables
import controller_interface
import io_interface
import ../../core/global_singleton

View File

@ -12,6 +12,7 @@ import ../../../app_service/service/chat/service as chat_service
import ../../../app_service/service/community/service as community_service
import ../../../app_service/service/token/service as token_service
import ../../../app_service/service/transaction/service as transaction_service
import ../../../app_service/service/collectible/service as collectible_service
import eventemitter
@ -45,7 +46,8 @@ proc newModule*[T](
chatService: chat_service.Service,
communityService: community_service.Service,
tokenService: token_service.Service,
transactionService: transaction_service.Service
transactionService: transaction_service.Service,
collectibleService: collectible_service.Service
): Module[T] =
result = Module[T]()
result.delegate = delegate
@ -65,7 +67,7 @@ proc newModule*[T](
)
result.walletSectionModule = wallet_section_module.newModule[Module[T]](
result, tokenService, transactionService
result, tokenService, transactionService, collectible_service
)

View File

@ -0,0 +1,23 @@
import ./controller_interface
import ../../../../../app_service/service/collectible/service as collectible_service
export controller_interface
type
Controller*[T: controller_interface.DelegateInterface] = ref object of controller_interface.AccessInterface
delegate: T
collectibleService: collectible_service.ServiceInterface
proc newController*[T](
delegate: T,
collectibleService: collectible_service.ServiceInterface
): Controller[T] =
result = Controller[T]()
result.delegate = delegate
result.collectibleService = collectibleService
method delete*[T](self: Controller[T]) =
discard
method init*[T](self: Controller[T]) =
discard

View File

@ -0,0 +1,17 @@
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")
type
## Abstract class (concept) which must be implemented by object/s used in this
## module.
DelegateInterface* = concept c

View File

@ -0,0 +1,16 @@
import strformat
type
Item* = object
name: string
proc initItem*(name: string): Item =
result.name = name
proc `$`*(self: Item): string =
result = fmt"""AllTokensItem(
name: {self.name},
]"""
proc getName*(self: Item): string =
return self.name

View File

@ -0,0 +1,64 @@
import NimQml, Tables, strutils, strformat
import ./item
type
ModelRole {.pure.} = enum
Name = UserRole + 1,
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"
}.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())
proc setItems*(self: Model, items: seq[Item]) =
self.beginResetModel()
self.items = items
self.endResetModel()
self.countChanged()

View File

@ -1,21 +1,28 @@
import ./io_interface, ./view
import ./io_interface, ./view, ./controller
import ../../../../../app_service/service/collectible/service as collectible_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): Module[T] =
proc newModule*[T](
delegate: T,
collectibleService: collectible_service.ServiceInterface
): Module[T] =
result = Module[T]()
result.delegate = delegate
result.view = newView()
result.view = newView(result)
result.controller = controller.newController[Module[T]](result, collectibleService)
result.moduleLoaded = false
method delete*[T](self: Module[T]) =
self.view.delete
self.controller.delete
method load*[T](self: Module[T]) =
self.moduleLoaded = true

View File

@ -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

View File

@ -11,6 +11,7 @@ import ./transactions/module as transactions_module
import ../../../../app_service/service/token/service as token_service
import ../../../../app_service/service/transaction/service as transaction_service
import ../../../../app_service/service/collectible/service as collectible_service
import io_interface
export io_interface
@ -32,6 +33,7 @@ proc newModule*[T](
delegate: T,
tokenService: token_service.Service,
transactionService: transaction_service.Service,
collectibleService: collectible_service.Service
): Module[T] =
result = Module[T]()
result.delegate = delegate
@ -41,7 +43,7 @@ proc newModule*[T](
result.accountTokensModule = account_tokens_module.newModule(result)
result.accountsModule = accounts_module.newModule(result)
result.allTokensModule = all_tokens_module.newModule(result, tokenService)
result.collectiblesModule = collectibles_module.newModule(result)
result.collectiblesModule = collectibles_module.newModule(result, collectibleService)
result.mainAccountModule = main_account_module.newModule(result)
result.transactionsModule = transactions_module.newModule(result, transactionService)

View File

@ -0,0 +1,6 @@
import json
include ../../common/json_utils
type Dto* = ref object
id*, name*, image*, collectibleType*, description*, externalUrl*: string

View File

@ -0,0 +1,20 @@
import chronicles
import ./service_interface, ./dto
export service_interface
logScope:
topics = "collectible-service"
type
Service* = ref object of ServiceInterface
method delete*(self: Service) =
discard
proc newService*(): Service =
result = Service()
method init*(self: Service) =
discard

View File

@ -0,0 +1,13 @@
import dto
export dto
type
ServiceInterface* {.pure inheritable.} = ref object of RootObj
## Abstract class for this service access.
method delete*(self: ServiceInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method init*(self: ServiceInterface) {.base.} =
raise newException(ValueError, "No implementation available")