Skip to content

Commit

Permalink
fix(@desktop/wallet): fix switch accounts after opening collectible d…
Browse files Browse the repository at this point in the history
…etails

Moved logic for CollectibleDetailView to Nim module.
  • Loading branch information
dlipicar committed Nov 23, 2022
1 parent 2bf2861 commit ecd799c
Show file tree
Hide file tree
Showing 20 changed files with 375 additions and 46 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ./item

type
AccessInterface* {.pure inheritable.} = ref object of RootObj
## Abstract class for any input/interaction with this module.
Expand All @@ -17,6 +19,9 @@ method fetch*(self: AccessInterface, collectionSlug: string) {.base.} =
method setCurrentAddress*(self: AccessInterface, address: string) {.base.} =
raise newException(ValueError, "No implementation available")

method getCollectible*(self: AccessInterface, collectionSlug: string, id: int) : Item {.base.} =
raise newException(ValueError, "No implementation available")

# View Delegate Interface
# Delegate for the view must be declared here due to use of QtObject and multi
# inheritance, which is not well supported in Nim.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ proc initItem*(
result.rankings = rankings
result.stats = stats

proc initItem*: Item =
result = initItem(-1, "", "", "transparent", "Collectibles", "", @[], @[], @[])

proc `$`*(self: Item): string =
result = fmt"""Collectibles(
id: {self.id},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,3 +100,9 @@ QtObject:
self.items = items
self.endResetModel()
self.countChanged()

proc getItemByID*(self: Model, id: int): Item =
for item in self.items:
if(item.getId() == id):
return item
return initItem()
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,6 @@ method fetch*(self: Module, collectionSlug: string) =
c.statistics.map(t => initTrait(t.traitType, t.value, t.displayType, t.maxValue)),
))
self.view.setItems(collectionSlug, items)

method getCollectible*(self: Module, collectionSlug: string, id: int): Item =
return self.view.getCollectible(collectionSlug, id)
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,14 @@ QtObject:
proc fetch*(self: View, collectionSlug: string) {.slot.} =
self.delegate.fetch(collectionSlug)

proc getModelForCollection*(self: View, collectionSlug: string): QObject {.slot.} =
proc getModelForCollectionPrivate(self: View, collectionSlug: string): Model =
if not self.models.hasKey(collectionSlug):
self.models[collectionSlug] = newModel()

return self.models[collectionSlug]

proc getModelForCollection*(self: View, collectionSlug: string): QObject {.slot.} =
return self.getModelForCollectionPrivate(collectionSlug)

proc getCollectible*(self: View, collectionSlug: string, id: int): Item =
let model = self.getModelForCollectionPrivate(collectionSlug)
return model.getItemByID(id)
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import ../../../../../../app_service/service/collectible/service as collectible_service
import ./item

type
AccessInterface* {.pure inheritable.} = ref object of RootObj
Expand All @@ -19,6 +20,8 @@ method loadCollections*(self: AccessInterface, address: string) {.base.} =
method setCollections*(self: AccessInterface, collections: seq[CollectionDto]) {.base.} =
raise newException(ValueError, "No implementation available")

method getCollection*(self: AccessInterface, slug: string): Item {.base.} =
raise newException(ValueError, "No implementation available")

# View Delegate Interface
# Delegate for the view must be declared here due to use of QtObject and multi
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ proc initItem*(name, slug, imageUrl: string, ownedAssetCount: int): Item =
result.imageUrl = imageUrl
result.ownedAssetCount = ownedAssetCount

proc initItem*(): Item =
result = initItem("", "", "", 0)

proc `$`*(self: Item): string =
result = fmt"""CollectibleCollection(
name: {self.name},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,3 +74,9 @@ QtObject:
self.items = items
self.endResetModel()
self.countChanged()

proc getItemBySlug*(self: Model, slug: string): Item =
for item in self.items:
if(item.getSlug() == slug):
return item
return initItem()
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,6 @@ method setCollections*(self: Module, collections: seq[CollectionDto]) =
c.ownedAssetCount,
))
)

method getCollection*(self: Module, slug: string): Item =
return self.view.getCollection(slug)
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,6 @@ QtObject:

proc setItems*(self: View, items: seq[Item]) =
self.model.setItems(items)

proc getCollection*(self: View, slug: string): Item =
return self.model.getItemBySlug(slug)
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import io_interface

import ../collectibles/module as collectibles_module
import ../collections/module as collections_module

type
Controller* = ref object of RootObj
delegate: io_interface.AccessInterface
collectiblesModule: collectibles_module.AccessInterface
collectionsModule: collections_module.AccessInterface

proc newController*(
delegate: io_interface.AccessInterface,
collectiblesModule: collectibles_module.AccessInterface,
collectionsModule: collections_module.AccessInterface
): Controller =
result = Controller()
result.delegate = delegate
result.collectiblesModule = collectiblesModule
result.collectionsModule = collectionsModule

proc delete*(self: Controller) =
discard

proc init*(self: Controller) =
discard

proc update*(self: Controller, slug: string, id: int) =
self.delegate.setData(self.collectionsModule.getCollection(slug), self.collectiblesModule.getCollectible(slug, id))
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import ../collections/item as collection_item
import ../collectibles/item as collectible_item

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")

method update*(self: AccessInterface, slug: string, id: int) {.base.} =
raise newException(ValueError, "No implementation available")

method setData*(self: AccessInterface, collection: collection_item.Item, collectible: collectible_item.Item) {.base.} =
raise newException(ValueError, "No implementation available")

# View Delegate Interface
# Delegate for the view must be declared here due to use of QtObject and multi
# inheritance, which is not well supported in Nim.
method viewDidLoad*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import NimQml, sequtils, sugar

import ../../../../../global/global_singleton

import ./io_interface, ./view, ./controller
import ../io_interface as delegate_interface
import ../collectibles/module as collectibles_module
import ../collections/module as collections_module

import ../collections/item as collection_item
import ../collectibles/item as collectible_item

export io_interface

type
Module* = ref object of io_interface.AccessInterface
delegate: delegate_interface.AccessInterface
view: View
controller: Controller
moduleLoaded: bool
currentAccountIndex: int

proc newModule*(
delegate: delegate_interface.AccessInterface,
collectionsModule: collections_module.AccessInterface,
collectiblesModule: collectibles_module.AccessInterface,
): Module =
result = Module()
result.delegate = delegate
result.view = newView(result)
result.controller = newController(result, collectiblesModule, collectionsModule)
result.moduleLoaded = false

method delete*(self: Module) =
self.view.delete

method load*(self: Module) =
singletonInstance.engine.setRootContextProperty("walletSectionCollectibleCurrent", newQVariant(self.view))

self.controller.init()
self.view.load()

method isLoaded*(self: Module): bool =
return self.moduleLoaded

method viewDidLoad*(self: Module) =
self.moduleLoaded = true
self.delegate.currentCollectibleModuleDidLoad()

method update*(self: Module, slug: string, id: int) =
self.controller.update(slug, id)

method setData*(self: Module, collection: collection_item.Item, collectible: collectible_item.Item) =
self.view.setData(collection, collectible)
Loading

0 comments on commit ecd799c

Please sign in to comment.