Skip to content

Commit

Permalink
feat(@wallet/desktop): implement generalized collectibles api
Browse files Browse the repository at this point in the history
  • Loading branch information
dlipicar committed Nov 17, 2023
1 parent 4239f77 commit 3df3b10
Show file tree
Hide file tree
Showing 8 changed files with 309 additions and 325 deletions.
104 changes: 63 additions & 41 deletions src/app/modules/shared_models/collectible_details_entry.nim
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ QtObject:
type
CollectibleDetailsEntry* = ref object of QObject
id: backend.CollectibleUniqueID
data: backend.CollectibleDetails
data: backend.Collectible
extradata: ExtraData
traits: TraitModel

Expand All @@ -29,13 +29,15 @@ QtObject:
proc delete*(self: CollectibleDetailsEntry) =
self.QObject.delete

proc newCollectibleDetailsFullEntry*(data: backend.CollectibleDetails, extradata: ExtraData): CollectibleDetailsEntry =
proc newCollectibleDetailsFullEntry*(data: backend.Collectible, extradata: ExtraData): CollectibleDetailsEntry =
new(result, delete)
result.id = data.id
result.data = data
result.extradata = extradata
result.traits = newTraitModel()
result.traits.setItems(data.traits)
if isSome(data.collectibleData) and isSome(data.collectibleData.get().traits):
let traits = data.collectibleData.get().traits.get()
result.traits.setItems(traits)
result.setup()

proc newCollectibleDetailsBasicEntry*(id: backend.CollectibleUniqueID, extradata: ExtraData): CollectibleDetailsEntry =
Expand Down Expand Up @@ -64,6 +66,24 @@ QtObject:
traits:{self.traits}
)"""

proc hasCollectibleData(self: CollectibleDetailsEntry): bool =
return self.data != nil and isSome(self.data.collectibleData)

proc getCollectibleData(self: CollectibleDetailsEntry): backend.CollectibleData =
return self.data.collectibleData.get()

proc hasCollectionData(self: CollectibleDetailsEntry): bool =
return self.data != nil and isSome(self.data.collectionData)

proc getCollectionData(self: CollectibleDetailsEntry): backend.CollectionData =
return self.data.collectionData.get()

proc hasCommunityData(self: CollectibleDetailsEntry): bool =
return self.data != nil and isSome(self.data.communityData)

proc getCommunityData(self: CollectibleDetailsEntry): backend.CommunityData =
return self.data.communityData.get()

proc getChainID*(self: CollectibleDetailsEntry): int {.slot.} =
return self.id.contractID.chainID

Expand All @@ -83,66 +103,68 @@ QtObject:
read = getTokenID

proc getName*(self: CollectibleDetailsEntry): string {.slot.} =
if self.data == nil:
if not self.hasCollectibleData():
return ""
return self.data.name
return self.data.collectibleData.get().name

QtProperty[string] name:
read = getName

proc getImageURL*(self: CollectibleDetailsEntry): string {.slot.} =
if self.data == nil:
if not self.hasCollectibleData() or isNone(self.getCollectibleData().imageUrl):
return ""
return self.data.imageUrl
return self.getCollectibleData().imageUrl.get()

QtProperty[string] imageUrl:
read = getImageURL

proc getMediaURL*(self: CollectibleDetailsEntry): string {.slot.} =
if self.data == nil:
if not self.hasCollectibleData() or isNone(self.getCollectibleData().animationUrl):
return ""
return self.data.animationUrl
return self.getCollectibleData().animationUrl.get()

QtProperty[string] mediaUrl:
read = getMediaURL

proc getMediaType*(self: CollectibleDetailsEntry): string {.slot.} =
if self.data == nil:
if not self.hasCollectibleData() or isNone(self.getCollectibleData().animationMediaType):
return ""
return self.data.animationMediaType
return self.getCollectibleData().animationMediaType.get()

QtProperty[string] mediaType:
read = getMediaType

proc getBackgroundColor*(self: CollectibleDetailsEntry): string {.slot.} =
var color = "transparent"
if self.data != nil and self.data.backgroundColor != "":
color = "#" & self.data.backgroundColor
if self.hasCollectibleData() and isSome(self.getCollectibleData().backgroundColor):
let backgroundColor = self.getCollectibleData().backgroundColor.get()
if backgroundColor != "":
color = "#" & backgroundColor
return color

QtProperty[string] backgroundColor:
read = getBackgroundColor

proc getCollectionName*(self: CollectibleDetailsEntry): string {.slot.} =
if self.data == nil:
return ""
return self.data.collectionName

QtProperty[string] collectionName:
read = getCollectionName

proc getDescription*(self: CollectibleDetailsEntry): string {.slot.} =
if self.data == nil:
if not self.hasCollectibleData() or isNone(self.getCollectibleData().description):
return ""
return self.data.description
return self.getCollectibleData().description.get()

QtProperty[string] description:
read = getDescription

proc getCollectionName*(self: CollectibleDetailsEntry): string {.slot.} =
if not self.hasCollectionData():
return ""
return self.getCollectionData().name

QtProperty[string] collectionName:
read = getCollectionName

proc getCollectionImageURL*(self: CollectibleDetailsEntry): string {.slot.} =
if self.data == nil:
if not self.hasCollectionData():
return ""
return self.data.collectionImageUrl
return self.getCollectionData().imageUrl

QtProperty[string] collectionImageUrl:
read = getCollectionImageURL
Expand All @@ -160,45 +182,45 @@ QtObject:
read = getNetworkShortName

proc getCommunityId*(self: CollectibleDetailsEntry): string {.slot.} =
if self.data == nil or isNone(self.data.communityInfo):
if not self.hasCommunityData():
return ""
return self.data.communityInfo.get().communityId
return self.getCommunityData().id

QtProperty[string] communityId:
read = getCommunityId

proc getCommunityName*(self: CollectibleDetailsEntry): string {.slot.} =
if self.data == nil or isNone(self.data.communityInfo):
if not self.hasCommunityData():
return ""
return self.data.communityInfo.get().communityName
return self.getCommunityData().name

QtProperty[string] communityName:
read = getCommunityName

proc getCommunityColor*(self: CollectibleDetailsEntry): string {.slot.} =
if self.data == nil or isNone(self.data.communityInfo):
if not self.hasCommunityData():
return ""
return self.data.communityInfo.get().communityColor
return self.getCommunityData().color

QtProperty[string] communityColor:
read = getCommunityColor

proc getCommunityImage*(self: CollectibleDetailsEntry): string {.slot.} =
if self.data == nil or isNone(self.data.communityInfo):
return ""
return self.data.communityInfo.get().communityImage

QtProperty[string] communityImage:
read = getCommunityImage

proc getCommunityPrivilegesLevel*(self: CollectibleDetailsEntry): int {.slot.} =
if self.data == nil or isNone(self.data.communityInfo):
if not self.hasCommunityData():
return PrivilegesLevel.Community.int
return int(self.data.communityInfo.get().privilegesLevel)
return int(self.getCommunityData().privilegesLevel)

QtProperty[int] communityPrivilegesLevel:
read = getCommunityPrivilegesLevel

proc getCommunityImage*(self: CollectibleDetailsEntry): string {.slot.} =
if not self.hasCommunityData() or isNone(self.getCommunityData().imageUrl):
return ""
return self.getCommunityData().imageUrl.get()

QtProperty[string] communityImage:
read = getCommunityImage

proc getNetworkColor*(self: CollectibleDetailsEntry): string {.slot.} =
return self.extradata.networkColor

Expand Down
66 changes: 46 additions & 20 deletions src/app/modules/shared_models/collectibles_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,62 @@ import backend/collectibles as backend
import collectibles_item
import ../../../app_service/service/community_tokens/dto/community_token

proc collectibleToItem*(c: backend.CollectibleHeader, isPinned: bool = false) : Item =
var mediaUrl = c.animationUrl
var mediaType = c.animationMediaType
if mediaUrl == "":
mediaUrl = c.imageUrl
mediaType = "image"
proc collectibleToItem*(c: backend.Collectible, isPinned: bool = false) : Item =
var collectibleName = ""
var collectibleDescription = ""
var collectibleMediaUrl = ""
var collectibleMediaType = ""
var collectibleImageUrl = ""
var collectibleBackgroundColor = ""
if isSome(c.collectibleData):
let collectibleData = c.collectibleData.get()
collectibleName = collectibleData.name
if isSome(collectibleData.description):
collectibleDescription = collectibleData.description.get()
if isSome(collectibleData.animationUrl):
collectibleMediaUrl = collectibleData.animationUrl.get()
if isSome(collectibleData.animationMediaType):
collectibleMediaType = collectibleData.animationMediaType.get()
if isSome(collectibleData.imageUrl):
collectibleImageUrl = collectibleData.imageUrl.get()
if isSome(collectibleData.backgroundColor):
collectibleBackgroundColor = collectibleData.backgroundColor.get()
if collectibleMediaUrl == "":
collectibleMediaUrl = collectibleImageUrl
collectibleMediaType = "image"

var collectionName = ""
var collectionSlug = ""
var collectionImageUrl = ""
if isSome(c.collectionData):
let collectionData = c.collectionData.get()
collectionName = collectionData.name
collectionSlug = collectionData.slug
collectionImageUrl = collectionData.imageUrl

var communityId = ""
var communityName = ""
var communityColor = ""
var communityPrivilegesLevel = PrivilegesLevel.Community.int
if isSome(c.communityHeader):
let communityHeader = c.communityHeader.get()
communityId = communityHeader.communityId
communityName = communityHeader.communityName
communityColor = communityHeader.communityColor
communityPrivilegesLevel = int(communityHeader.privilegesLevel)
if isSome(c.communityData):
let communityData = c.communityData.get()
communityId = communityData.id
communityName = communityData.name
communityColor = communityData.color
communityPrivilegesLevel = int(communityData.privilegesLevel)

return initItem(
c.id.contractID.chainID,
c.id.contractID.address,
c.id.tokenID,
c.name,
mediaUrl,
mediaType,
c.imageUrl,
c.backgroundColor,
c.collectionName,
c.collectionSlug,
c.collectionImageUrl,
collectibleName,
collectibleMediaUrl,
collectibleMediaType,
collectibleImageUrl,
collectibleBackgroundColor,
collectionName,
collectionSlug,
collectionImageUrl,
isPinned,
communityId,
communityName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ QtObject:

requestId: int32

dataType: backend_collectibles.CollectibleDataType

proc setup(self: Controller) =
self.QObject.setup

Expand Down Expand Up @@ -62,7 +64,7 @@ QtObject:
proc processGetCollectiblesDetailsResponse(self: Controller, response: JsonNode) =
defer: self.setIsDetailedEntryLoading(false)

let res = fromJson(response, backend_collectibles.GetCollectiblesDetailsResponse)
let res = fromJson(response, backend_collectibles.GetCollectiblesByUniqueIDResponse)

if res.errorCode != ErrorCodeSuccess:
error "error fetching collectible details: ", res.errorCode
Expand Down Expand Up @@ -93,7 +95,7 @@ QtObject:
self.detailedEntry = newCollectibleDetailsBasicEntry(id, extradata)
self.detailedEntryChanged()

let response = backend_collectibles.getCollectiblesDetailsAsync(self.requestId, @[id])
let response = backend_collectibles.getCollectiblesByUniqueIDAsync(self.requestId, @[id], self.dataType)
if response.error != nil:
self.setIsDetailedEntryLoading(false)
error "error fetching collectible details: ", response.error
Expand All @@ -106,12 +108,15 @@ QtObject:

proc newController*(requestId: int32,
networkService: network_service.Service,
events: EventEmitter
events: EventEmitter,
dataType: backend_collectibles.CollectibleDataType = backend_collectibles.CollectibleDataType.Details
): Controller =
new(result, delete)

result.requestId = requestId

result.dataType = dataType

result.networkService = networkService

result.detailedEntry = newCollectibleDetailsEmptyEntry()
Expand Down
23 changes: 17 additions & 6 deletions src/app/modules/shared_modules/collectibles/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ QtObject:
requestId: int32
autofetch: bool

dataType: backend_collectibles.CollectibleDataType
fetchCriteria: backend_collectibles.FetchCriteria

proc setup(self: Controller) =
self.QObject.setup

Expand Down Expand Up @@ -112,17 +115,17 @@ QtObject:
offset = self.model.getCollectiblesCount()
self.fetchFromStart = false

let response = backend_collectibles.filterOwnedCollectiblesAsync(self.requestId, self.chainIds, self.addresses, self.filter, offset, FETCH_BATCH_COUNT_DEFAULT)
let response = backend_collectibles.getOwnedCollectiblesAsync(self.requestId, self.chainIds, self.addresses, self.filter, offset, FETCH_BATCH_COUNT_DEFAULT, self.dataType, self.fetchCriteria)
if response.error != nil:
self.model.setIsFetching(false)
self.model.setIsError(true)
self.fetchFromStart = true
error "error fetching collectibles entries: ", response.error

proc processFilterOwnedCollectiblesResponse(self: Controller, response: JsonNode) =
proc processGetOwnedCollectiblesResponse(self: Controller, response: JsonNode) =
defer: self.model.setIsFetching(false)

let res = fromJson(response, backend_collectibles.FilterOwnedCollectiblesResponse)
let res = fromJson(response, backend_collectibles.GetOwnedCollectiblesResponse)

let isError = res.errorCode != backend_collectibles.ErrorCodeSuccess

Expand Down Expand Up @@ -150,7 +153,7 @@ QtObject:

proc setupEventHandlers(self: Controller) =
self.eventsHandler.onOwnedCollectiblesFilteringDone(proc (jsonObj: JsonNode) =
self.processFilterOwnedCollectiblesResponse(jsonObj)
self.processGetOwnedCollectiblesResponse(jsonObj)
)

self.eventsHandler.onCollectiblesOwnershipUpdateStarted(proc (address: string, chainID: int) =
Expand All @@ -170,12 +173,20 @@ QtObject:
self.eventsHandler.onCollectiblesOwnershipUpdateFinishedWithError(proc (address: string, chainID: int) =
self.setOwnershipState(address, chainID, OwnershipStateError)
)

proc newController*(requestId: int32, autofetch: bool, events: EventEmitter): Controller =
proc newController*(
requestId: int32,
events: EventEmitter,
autofetch: bool = true,
dataType: backend_collectibles.CollectibleDataType = backend_collectibles.CollectibleDataType.Header,
fetchCriteria: backend_collectibles.FetchCriteria = backend_collectibles.FetchCriteria(
fetchType: backend_collectibles.FetchType.NeverFetch,
)): Controller =
new(result, delete)

result.requestId = requestId
result.autofetch = autofetch
result.dataType = dataType
result.fetchCriteria = fetchCriteria

result.model = newModel()
result.fetchFromStart = true
Expand Down
Loading

0 comments on commit 3df3b10

Please sign in to comment.