refactor: load recent stickers asynchronously

This postpones the loading of recently used stickers to the point
when a) the stickers popup is opened and the recent stickers tab was the
last visited one or b) when the sticker popup is open and one switches
to the recent stickers tab.

Partially closes #9435
This commit is contained in:
Pascal Precht 2023-02-08 14:31:14 +01:00 committed by Jonathan Rainville
parent 5c0f1981ad
commit 433ea71d62
7 changed files with 79 additions and 3 deletions

View File

@ -53,9 +53,6 @@ proc delete*(self: Controller) =
discard discard
proc init*(self: Controller) = proc init*(self: Controller) =
let recentStickers = self.stickerService.getRecentStickers()
for sticker in recentStickers:
self.delegate.addRecentStickerToList(sticker)
let installedStickers = self.stickerService.getInstalledStickerPacks() let installedStickers = self.stickerService.getInstalledStickerPacks()
self.delegate.populateInstalledStickerPacks(installedStickers) self.delegate.populateInstalledStickerPacks(installedStickers)
@ -74,6 +71,11 @@ proc init*(self: Controller) =
self.obtainMarketStickerPacks() self.obtainMarketStickerPacks()
self.disconnected = false self.disconnected = false
self.events.on(SIGNAL_LOAD_RECENT_STICKERS_DONE) do(e: Args):
let args = StickersArgs(e)
for sticker in args.stickers:
self.delegate.addRecentStickerToList(sticker)
self.events.on(SIGNAL_STICKER_PACK_LOADED) do(e: Args): self.events.on(SIGNAL_STICKER_PACK_LOADED) do(e: Args):
let args = StickerPackLoadedArgs(e) let args = StickerPackLoadedArgs(e)
self.delegate.addStickerPackToList( self.delegate.addStickerPackToList(
@ -114,6 +116,12 @@ proc init*(self: Controller) =
proc buy*(self: Controller, packId: string, address: string, gas: string, gasPrice: string, maxPriorityFeePerGas: string, maxFeePerGas: string, password: string, eip1559Enabled: bool): tuple[response: string, success: bool] = proc buy*(self: Controller, packId: string, address: string, gas: string, gasPrice: string, maxPriorityFeePerGas: string, maxFeePerGas: string, password: string, eip1559Enabled: bool): tuple[response: string, success: bool] =
self.stickerService.buy(packId, address, gas, gasPrice, maxPriorityFeePerGas, maxFeePerGas, password, eip1559Enabled) self.stickerService.buy(packId, address, gas, gasPrice, maxPriorityFeePerGas, maxFeePerGas, password, eip1559Enabled)
proc getRecentStickers*(self: Controller): seq[StickerDto] =
return self.stickerService.getRecentStickers()
proc loadRecentStickers*(self: Controller) =
self.stickerService.asyncLoadRecentStickers()
proc estimate*(self: Controller, packId: string, address: string, price: string, uuid: string) = proc estimate*(self: Controller, packId: string, address: string, price: string, uuid: string) =
self.stickerService.estimate(packId, address, price, uuid) self.stickerService.estimate(packId, address, price, uuid)

View File

@ -22,6 +22,12 @@ method viewDidLoad*(self: AccessInterface) {.base.} =
method authenticateAndBuy*(self: AccessInterface, packId: string, address: string, gas: string, gasPrice: string, maxPriorityFeePerGas: string, maxFeePerGas: string, eip1559Enabled: bool){.base.} = method authenticateAndBuy*(self: AccessInterface, packId: string, address: string, gas: string, gasPrice: string, maxPriorityFeePerGas: string, maxFeePerGas: string, eip1559Enabled: bool){.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")
method getRecentStickers*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method loadRecentStickers*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")
method getInstalledStickerPacks*(self: AccessInterface): Table[string, StickerPackDto] {.base.} = method getInstalledStickerPacks*(self: AccessInterface): Table[string, StickerPackDto] {.base.} =
raise newException(ValueError, "No implementation available") raise newException(ValueError, "No implementation available")

View File

@ -163,6 +163,15 @@ method estimate*(self: Module, packId: string, address: string, price: string, u
method addRecentStickerToList*(self: Module, sticker: StickerDto) = method addRecentStickerToList*(self: Module, sticker: StickerDto) =
self.view.addRecentStickerToList(initItem(sticker.hash, sticker.packId, sticker.url)) self.view.addRecentStickerToList(initItem(sticker.hash, sticker.packId, sticker.url))
method getRecentStickers*(self: Module) =
let data = self.controller.getRecentStickers()
if data.len > 0:
for sticker in data:
self.addRecentStickerToList(sticker)
return
self.controller.loadRecentStickers()
method clearStickerPacks*(self: Module) = method clearStickerPacks*(self: Module) =
self.view.clearStickerPacks() self.view.clearStickerPacks()

View File

@ -108,6 +108,7 @@ QtObject:
proc addRecentStickerToList*(self: View, sticker: Item) = proc addRecentStickerToList*(self: View, sticker: Item) =
self.recentStickers.addStickerToList(sticker) self.recentStickers.addStickerToList(sticker)
self.recentStickersUpdated()
proc getAllPacksLoaded(self: View): bool {.slot.} = proc getAllPacksLoaded(self: View): bool {.slot.} =
self.packsLoaded self.packsLoaded
@ -143,6 +144,9 @@ QtObject:
self.delegate.obtainMarketStickerPacks() self.delegate.obtainMarketStickerPacks()
proc getRecentStickers(self: View) {.slot.} =
self.delegate.getRecentStickers()
proc send*(self: View, channelId: string, hash: string, replyTo: string, pack: string, url: string) {.slot.} = proc send*(self: View, channelId: string, hash: string, replyTo: string, pack: string, url: string) {.slot.} =
let sticker = initItem(hash, pack, url) let sticker = initItem(hash, pack, url)
self.addRecentStickerToList(sticker) self.addRecentStickerToList(sticker)

View File

@ -13,6 +13,12 @@ type
packId*: string packId*: string
chainId*: int chainId*: int
hasKey*: bool hasKey*: bool
AsyncGetRecentStickersTaskArg* = ref object of QObjectTaskArg
const asyncGetRecentStickersTask: Task = proc(argEncoded: string) {.gcsafe, nimcall.} =
let arg = decode[AsyncGetRecentStickersTaskArg](argEncoded)
let response = status_stickers.recent()
arg.finish(response)
proc getMarketStickerPacks*(chainId: int): proc getMarketStickerPacks*(chainId: int):
tuple[stickers: Table[string, StickerPackDto], error: string] = tuple[stickers: Table[string, StickerPackDto], error: string] =

View File

@ -51,6 +51,8 @@ type
transactionType*: string transactionType*: string
StickerPackInstalledArgs* = ref object of Args StickerPackInstalledArgs* = ref object of Args
packId*: string packId*: string
StickersArgs* = ref object of Args
stickers*: seq[StickerDto]
# Signals which may be emitted by this service: # Signals which may be emitted by this service:
const SIGNAL_STICKER_PACK_LOADED* = "stickerPackLoaded" const SIGNAL_STICKER_PACK_LOADED* = "stickerPackLoaded"
@ -60,6 +62,9 @@ const SIGNAL_STICKER_GAS_ESTIMATED* = "stickerGasEstimated"
const SIGNAL_STICKER_TRANSACTION_CONFIRMED* = "stickerTransactionConfirmed" const SIGNAL_STICKER_TRANSACTION_CONFIRMED* = "stickerTransactionConfirmed"
const SIGNAL_STICKER_TRANSACTION_REVERTED* = "stickerTransactionReverted" const SIGNAL_STICKER_TRANSACTION_REVERTED* = "stickerTransactionReverted"
const SIGNAL_STICKER_PACK_INSTALLED* = "stickerPackInstalled" const SIGNAL_STICKER_PACK_INSTALLED* = "stickerPackInstalled"
const SIGNAL_LOAD_RECENT_STICKERS_STARTED* = "loadRecentStickersStarted"
const SIGNAL_LOAD_RECENT_STICKERS_FAILED* = "loadRecentStickersFailed"
const SIGNAL_LOAD_RECENT_STICKERS_DONE* = "loadRecentStickersDone"
QtObject: QtObject:
type Service* = ref object of QObject type Service* = ref object of QObject
@ -330,6 +335,33 @@ QtObject:
except RpcException: except RpcException:
error "Error getting recent stickers", message = getCurrentExceptionMsg() error "Error getting recent stickers", message = getCurrentExceptionMsg()
proc asyncLoadRecentStickers*(self: Service) =
self.events.emit(SIGNAL_LOAD_RECENT_STICKERS_STARTED, Args())
try:
let arg = AsyncGetRecentStickersTaskArg(
tptr: cast[ByteAddress](asyncGetRecentStickersTask),
vptr: cast[ByteAddress](self.vptr),
slot: "onAsyncGetRecentStickersDone"
)
self.threadpool.start(arg)
except Exception as e:
error "Error loading recent stickers", msg = e.msg
proc onAsyncGetRecentStickersDone*(self: Service, response: string) {.slot.} =
try:
let rpcResponseObj = response.parseJson
if (rpcResponseObj{"error"}.kind != JNull):
let error = Json.decode($rpcResponseObj["error"], RpcError)
error "error loading recent stickers", msg = error.message
return
let recentStickers = map(rpcResponseObj{"result"}.getElems(), toStickerDto)
self.events.emit(SIGNAL_LOAD_RECENT_STICKERS_DONE, StickersArgs(stickers: recentStickers))
except Exception as e:
let errMsg = e.msg
error "error: ", errMsg
self.events.emit(SIGNAL_LOAD_RECENT_STICKERS_FAILED, Args())
proc getNumInstalledStickerPacks*(self: Service): int = proc getNumInstalledStickerPacks*(self: Service): int =
try: try:
let installedResponse = status_stickers.installed() let installedResponse = status_stickers.installed()

View File

@ -34,6 +34,10 @@ Popup {
function loadStickers() { function loadStickers() {
store.stickersModuleInst.loadStickers() store.stickersModuleInst.loadStickers()
} }
function getRecentStickers() {
store.stickersModuleInst.getRecentStickers()
}
} }
enabled: !!d.recentStickers && !!d.stickerPackList enabled: !!d.recentStickers && !!d.stickerPackList
@ -56,6 +60,12 @@ Popup {
} }
} }
onAboutToShow: {
if (stickerGrid.packId == -1) {
d.getRecentStickers()
}
}
onClosed: { onClosed: {
stickerMarket.visible = false stickerMarket.visible = false
footerContent.visible = true footerContent.visible = true
@ -261,6 +271,7 @@ Popup {
onClicked: { onClicked: {
highlighted = true highlighted = true
stickerPackListView.selectedPackId = -1 stickerPackListView.selectedPackId = -1
d.getRecentStickers()
stickerGrid.model = d.recentStickers stickerGrid.model = d.recentStickers
} }
} }