feat(@desktop/wallet): implement partial collectibles updates

Fixes #12150
This commit is contained in:
Dario Gabriel Lipicar 2023-09-14 12:22:30 -03:00 committed by dlipicar
parent db436b4e43
commit 52e6fddcab
4 changed files with 57 additions and 13 deletions

View File

@ -1,5 +1,5 @@
import NimQml, std/json, sequtils, sugar, strutils
import stint, logging
import stint, logging, Tables
import app/modules/shared_models/collectibles_model
import app/modules/shared_models/collectibles_utils
@ -22,6 +22,8 @@ QtObject:
addresses: seq[string]
chainIds: seq[int]
stateTable: Table[string, Table[int, bool]] # Table[address][chainID] -> isUpdating
requestId: int32
autofetch: bool
@ -40,7 +42,6 @@ QtObject:
QtProperty[QVariant] model:
read = getModelAsVariant
proc loadMoreItems(self: Controller) {.slot.} =
if self.model.getIsFetching():
return
@ -87,17 +88,47 @@ QtObject:
if self.autofetch:
self.loadMoreItems()
proc resetUpdateState*(self: Controller) =
# Initialize state table
# We assume that ownership is initially not being updated. This will change if an
# update starts or a partial update is received.
# TODO: Get the update state at the time of filter switch from the backend?
self.stateTable = initTable[string, Table[int, bool]]()
for address in self.addresses:
self.stateTable[address] = initTable[int, bool]()
for chainID in self.chainIds:
self.stateTable[address][chainID] = false
self.model.setIsUpdating(false)
proc setUpdateState*(self: Controller, address: string, chainID: int, isUpdating: bool) =
if not self.stateTable.hasKey(address) or not self.stateTable[address].hasKey(chainID):
return
self.stateTable[address][chainID] = isUpdating
# If any address+chainID is updating, then the whole model is updating
for address, chainIDsPerAddress in self.stateTable.pairs:
for chainID, isUpdating in chainIDsPerAddress:
if isUpdating:
self.model.setIsUpdating(true)
return
self.model.setIsUpdating(false)
proc setupEventHandlers(self: Controller) =
self.eventsHandler.onOwnedCollectiblesFilteringDone(proc (jsonObj: JsonNode) =
self.processFilterOwnedCollectiblesResponse(jsonObj)
)
self.eventsHandler.onCollectiblesOwnershipUpdateStarted(proc () =
self.model.setIsUpdating(true)
self.eventsHandler.onCollectiblesOwnershipUpdateStarted(proc (address: string, chainID: int) =
self.setUpdateState(address, chainID, true)
)
self.eventsHandler.onCollectiblesOwnershipUpdateFinished(proc () =
self.model.setIsUpdating(false)
self.eventsHandler.onCollectiblesOwnershipUpdatePartial(proc (address: string, chainID: int) =
self.setUpdateState(address, chainID, true)
self.resetModel()
)
self.eventsHandler.onCollectiblesOwnershipUpdateFinished(proc (address: string, chainID: int) =
self.setUpdateState(address, chainID, false)
self.resetModel()
)
@ -128,6 +159,8 @@ QtObject:
self.chainIds = chainIds
self.addresses = addresses
self.resetUpdateState()
self.eventsHandler.updateSubscribedAddresses(self.addresses)
self.eventsHandler.updateSubscribedChainIDs(self.chainIds)

View File

@ -8,6 +8,7 @@ import backend/collectibles as backend_collectibles
type EventCallbackProc = proc (eventObject: JsonNode)
type WalletEventCallbackProc = proc (data: WalletSignal)
type OwnershipUpdateCallbackProc = proc (address: string, chainID: int)
# EventsHandler responsible for catching collectibles related backend events and reporting them
QtObject:
@ -20,8 +21,9 @@ QtObject:
subscribedAddresses: HashSet[string]
subscribedChainIDs: HashSet[int]
collectiblesOwnershipUpdateStartedFn: proc()
collectiblesOwnershipUpdateFinishedFn: proc()
collectiblesOwnershipUpdateStartedFn: OwnershipUpdateCallbackProc
collectiblesOwnershipUpdatePartialFn: OwnershipUpdateCallbackProc
collectiblesOwnershipUpdateFinishedFn: OwnershipUpdateCallbackProc
requestId: int32
@ -34,10 +36,13 @@ QtObject:
proc onOwnedCollectiblesFilteringDone*(self: EventsHandler, handler: EventCallbackProc) =
self.eventHandlers[backend_collectibles.eventOwnedCollectiblesFilteringDone] = handler
proc onCollectiblesOwnershipUpdateStarted*(self: EventsHandler, handler: proc()) =
proc onCollectiblesOwnershipUpdateStarted*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
self.collectiblesOwnershipUpdateStartedFn = handler
proc onCollectiblesOwnershipUpdateFinished*(self: EventsHandler, handler: proc()) =
proc onCollectiblesOwnershipUpdatePartial*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
self.collectiblesOwnershipUpdatePartialFn = handler
proc onCollectiblesOwnershipUpdateFinished*(self: EventsHandler, handler: OwnershipUpdateCallbackProc) =
self.collectiblesOwnershipUpdateFinishedFn = handler
proc handleApiEvents(self: EventsHandler, e: Args) =
@ -72,12 +77,17 @@ QtObject:
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdateStarted] = proc (data: WalletSignal) =
if self.collectiblesOwnershipUpdateStartedFn == nil or self.shouldIgnoreEvent(data):
return
self.collectiblesOwnershipUpdateStartedFn()
self.collectiblesOwnershipUpdateStartedFn(data.accounts[0], data.chainID)
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdatePartial] = proc (data: WalletSignal) =
if self.collectiblesOwnershipUpdatePartialFn == nil or self.shouldIgnoreEvent(data):
return
self.collectiblesOwnershipUpdatePartialFn(data.accounts[0], data.chainID)
self.walletEventHandlers[backend_collectibles.eventCollectiblesOwnershipUpdateFinished] = proc (data: WalletSignal) =
if self.collectiblesOwnershipUpdateFinishedFn == nil or self.shouldIgnoreEvent(data):
return
self.collectiblesOwnershipUpdateFinishedFn()
self.collectiblesOwnershipUpdateFinishedFn(data.accounts[0], data.chainID)
proc newEventsHandler*(requestId: int32, events: EventEmitter): EventsHandler =
new(result, delete)

View File

@ -16,6 +16,7 @@ type
# Declared in services/wallet/collectibles/service.go
const eventCollectiblesOwnershipUpdateStarted*: string = "wallet-collectibles-ownership-update-started"
const eventCollectiblesOwnershipUpdatePartial*: string = "wallet-collectibles-ownership-update-partial"
const eventCollectiblesOwnershipUpdateFinished*: string = "wallet-collectibles-ownership-update-finished"
const eventCollectiblesOwnershipUpdateFinishedWithError*: string = "wallet-collectibles-ownership-update-finished-with-error"

2
vendor/status-go vendored

@ -1 +1 @@
Subproject commit 5e8300d6a12fe22a97c2219dac17888499003b8f
Subproject commit e337ab4f13e196d2c83d9f99aef68b5b79066695