refactor(@desktop/wallet): Add collection/item/collectibles module

This commit is contained in:
Anthony Laibe 2021-10-18 11:55:18 +02:00 committed by Iuri Matias
parent c97138c1dd
commit 1828029ab2
23 changed files with 567 additions and 28 deletions

View File

@ -1,5 +1,5 @@
import ./controller_interface import ./controller_interface
import ../../../../../app_service/service/collectible/service as collectible_service import ../../../../../../app_service/service/collectible/service as collectible_service
export controller_interface export controller_interface

View File

@ -1,5 +1,3 @@
import ../../../../../app_service/service/token/service_interface as token_service
type type
AccessInterface* {.pure inheritable.} = ref object of RootObj AccessInterface* {.pure inheritable.} = ref object of RootObj
## Abstract class for any input/interaction with this module. ## Abstract class for any input/interaction with this module.

View File

@ -0,0 +1,17 @@
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 load*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method isLoaded*(self: AccessInterface): bool {.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

@ -8,8 +8,8 @@ proc initItem*(name: string): Item =
result.name = name result.name = name
proc `$`*(self: Item): string = proc `$`*(self: Item): string =
result = fmt"""AllTokensItem( result = fmt"""Collectible(
name: {self.name}, name: {self.name}
]""" ]"""
proc getName*(self: Item): string = proc getName*(self: Item): string =

View File

@ -40,7 +40,7 @@ QtObject:
method roleNames(self: Model): Table[int, string] = method roleNames(self: Model): Table[int, string] =
{ {
ModelRole.Name.int:"name" ModelRole.Name.int:"name",
}.toTable }.toTable
method data(self: Model, index: QModelIndex, role: int): QVariant = method data(self: Model, index: QModelIndex, role: int): QVariant =

View File

@ -0,0 +1,30 @@
import sequtils, sugar
import ./io_interface, ./view, ./controller, ./item
import ../../../../../../app_service/service/collectible/service as collectible_service
export io_interface
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, collectibleService: collectible_service.ServiceInterface): Module[T] =
result = Module[T]()
result.delegate = delegate
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
method isLoaded*[T](self: Module[T]): bool =
return self.moduleLoaded

View File

@ -0,0 +1,36 @@
import NimQml
import ./model
import ./item
import ./io_interface
QtObject:
type
View* = ref object of QObject
delegate: io_interface.AccessInterface
model: Model
modelVariant: QVariant
proc delete*(self: View) =
self.model.delete
self.modelVariant.delete
self.QObject.delete
proc newView*(delegate: io_interface.AccessInterface): View =
new(result, delete)
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

@ -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,15 @@
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,17 @@
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 load*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method isLoaded*(self: AccessInterface): bool {.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,27 @@
import strformat
type
Item* = object
id: int
name, imageThumbnailUrl: string
proc initItem*(id: int, name: string, imageThumbnailUrl: string): Item =
result.id = id
result.name = name
result.imageThumbnailUrl = imageThumbnailUrl
proc `$`*(self: Item): string =
result = fmt"""Collectibles(
id: {self.id},
name: {self.name},
imageThumbnailUrl: {self.imageThumbnailUrl}
]"""
proc getId*(self: Item): int =
return self.id
proc getName*(self: Item): string =
return self.name
proc getImageThumbnailUrl*(self: Item): string =
return self.imageThumbnailUrl

View File

@ -0,0 +1,72 @@
import NimQml, Tables, strutils, strformat
import ./item
type
ModelRole {.pure.} = enum
Id = UserRole + 1,
Name
ImageThumbnailUrl
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.Id.int:"id",
ModelRole.Name.int:"name",
ModelRole.ImageThumbnailUrl.int:"imageThumbnailUrl",
}.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.Id:
result = newQVariant(item.getId())
of ModelRole.Name:
result = newQVariant(item.getName())
of ModelRole.ImageThumbnailUrl:
result = newQVariant(item.getImageThumbnailUrl())
proc setItems*(self: Model, items: seq[Item]) =
self.beginResetModel()
self.items = items
self.endResetModel()
self.countChanged()

View File

@ -0,0 +1,30 @@
import sequtils, sugar
import ./io_interface, ./view, ./controller, ./item
import ../../../../../../app_service/service/collectible/service as collectible_service
export io_interface
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, collectibleService: collectible_service.ServiceInterface): Module[T] =
result = Module[T]()
result.delegate = delegate
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
method isLoaded*[T](self: Module[T]): bool =
return self.moduleLoaded

View File

@ -0,0 +1,36 @@
import NimQml
import ./model
import ./item
import ./io_interface
QtObject:
type
View* = ref object of QObject
delegate: io_interface.AccessInterface
model: Model
modelVariant: QVariant
proc delete*(self: View) =
self.model.delete
self.modelVariant.delete
self.QObject.delete
proc newView*(delegate: io_interface.AccessInterface): View =
new(result, delete)
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

@ -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,15 @@
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,17 @@
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 load*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method isLoaded*(self: AccessInterface): bool {.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,35 @@
import strformat
type
Item* = object
name: string
slug: string
imageUrl: string
ownedAssetCount: int
proc initItem*(name, slug, imageUrl: string, ownedAssetCount: int): Item =
result.name = name
result.slug = slug
result.imageUrl = imageUrl
result.ownedAssetCount = ownedAssetCount
proc `$`*(self: Item): string =
result = fmt"""CollectibleCollection(
name: {self.name},
slug: {self.slug},
imageUrl: {self.imageUrl},
ownedAssetCount: {self.ownedAssetCount}
]"""
proc getName*(self: Item): string =
return self.name
proc getSlug*(self: Item): string =
return self.slug
proc getImageUrl*(self: Item): string =
return self.imageUrl
proc getOwnedAssetCount*(self: Item): int =
return self.ownedAssetCount

View File

@ -0,0 +1,76 @@
import NimQml, Tables, strutils, strformat
import ./item
type
ModelRole {.pure.} = enum
Name = UserRole + 1,
Slug
ImageUrl
OwnedAssetCount
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.Slug.int:"slug",
ModelRole.ImageUrl.int:"imageurl",
ModelRole.OwnedAssetCount.int:"ownedAssetCount"
}.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.Slug:
result = newQVariant(item.getSlug())
of ModelRole.ImageUrl:
result = newQVariant(item.getImageUrl())
of ModelRole.OwnedAssetCount:
result = newQVariant(item.getOwnedAssetCount())
proc setItems*(self: Model, items: seq[Item]) =
self.beginResetModel()
self.items = items
self.endResetModel()
self.countChanged()

View File

@ -0,0 +1,30 @@
import sequtils, sugar
import ./io_interface, ./view, ./controller, ./item
import ../../../../../../app_service/service/collectible/service as collectible_service
export io_interface
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, collectibleService: collectible_service.ServiceInterface): Module[T] =
result = Module[T]()
result.delegate = delegate
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
method isLoaded*[T](self: Module[T]): bool =
return self.moduleLoaded

View File

@ -0,0 +1,36 @@
import NimQml
import ./model
import ./item
import ./io_interface
QtObject:
type
View* = ref object of QObject
delegate: io_interface.AccessInterface
model: Model
modelVariant: QVariant
proc delete*(self: View) =
self.model.delete
self.modelVariant.delete
self.QObject.delete
proc newView*(delegate: io_interface.AccessInterface): View =
new(result, delete)
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

@ -1,15 +1,22 @@
import ./io_interface, ./view, ./controller import ./io_interface, ./view
import ../../../../../app_service/service/collectible/service as collectible_service import ../../../../../app_service/service/collectible/service as collectible_service
import collectible/module as collectible_module
import collections/module as collections_module
import collectibles/module as collectibles_module
export io_interface export io_interface
type type
Module* [T: io_interface.DelegateInterface] = ref object of io_interface.AccessInterface Module* [T: io_interface.DelegateInterface] = ref object of io_interface.AccessInterface
delegate: T delegate: T
view: View view: View
controller: controller.AccessInterface
moduleLoaded: bool moduleLoaded: bool
collectiblesModule: collectibles_module.AccessInterface
collectionsModule: collections_module.AccessInterface
collectibleModule: collectible_module.AccessInterface
proc newModule*[T]( proc newModule*[T](
delegate: T, delegate: T,
collectibleService: collectible_service.ServiceInterface collectibleService: collectible_service.ServiceInterface
@ -17,14 +24,29 @@ proc newModule*[T](
result = Module[T]() result = Module[T]()
result.delegate = delegate result.delegate = delegate
result.view = newView(result) result.view = newView(result)
result.controller = controller.newController[Module[T]](result, collectibleService)
result.moduleLoaded = false result.moduleLoaded = false
result.collectiblesModule = collectibles_module.newModule[Module[T]](
result, collectibleService
)
result.collectionsModule = collectionsModule.newModule[Module[T]](
result, collectibleService
)
result.collectibleModule = collectibleModule.newModule[Module[T]](
result, collectibleService
)
method delete*[T](self: Module[T]) = method delete*[T](self: Module[T]) =
self.view.delete self.view.delete
self.controller.delete self.collectiblesModule.delete
self.collectionsModule.delete
self.collectibleModule.delete
method load*[T](self: Module[T]) = method load*[T](self: Module[T]) =
self.collectiblesModule.load
self.collectionsModule.load
self.collectibleModule.load
self.moduleLoaded = true self.moduleLoaded = true
method isLoaded*[T](self: Module[T]): bool = method isLoaded*[T](self: Module[T]): bool =

View File

@ -1,32 +1,16 @@
import NimQml import NimQml
import ./model
import ./io_interface import ./io_interface
QtObject: QtObject:
type type
View* = ref object of QObject View* = ref object of QObject
delegate: io_interface.AccessInterface delegate: io_interface.AccessInterface
model: Model
modelVariant: QVariant
proc delete*(self: View) = proc delete*(self: View) =
self.model.delete
self.modelVariant.delete
self.QObject.delete self.QObject.delete
proc newView*(delegate: io_interface.AccessInterface): View = proc newView*(delegate: io_interface.AccessInterface): View =
new(result, delete) new(result, delete)
result.QObject.setup result.QObject.setup
result.delegate = delegate 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