From 9d5c71fb4e8a0526a968269e32cd9c2a433f7547 Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 30 Jul 2020 15:28:31 -0400 Subject: [PATCH] feat: load stickerpacks async --- src/app/chat/core.nim | 20 ++--------- src/app/chat/view.nim | 28 +++++++++++++-- src/status/chat.nim | 10 +++--- src/status/libstatus/stickers.nim | 57 ++++++++++++++++--------------- 4 files changed, 60 insertions(+), 55 deletions(-) diff --git a/src/app/chat/core.nim b/src/app/chat/core.nim index 4d3a9468d3..e98c64fb69 100644 --- a/src/app/chat/core.nim +++ b/src/app/chat/core.nim @@ -8,8 +8,6 @@ import ../../status/libstatus/wallet as status_wallet import ../../status/[chat, contacts, status] import view, views/channels_list, views/message_list -from eth/common/utils import parseAddress - logScope: topics = "chat-controller" @@ -36,22 +34,8 @@ proc init*(self: ChatController) = self.handleChatEvents() self.status.mailservers.init() self.status.chat.init() - - let currAcct = status_wallet.getWalletAccounts()[0] # TODO: make generic - let currAddr = parseAddress(currAcct.address) - - let installedStickerPacks = self.status.chat.getInstalledStickerPacks() - - let purchasedStickerPacks = self.status.chat.getPurchasedStickerPacks(currAddr) - - # TODO: getting available stickers should be done in a separate thread as there - # a long wait for contract response, decoded, downloading from IPFS, EDN decoding, - # etc - let availableStickerPacks = self.status.chat.getAvailableStickerPacks() - for packId, stickerPack in availableStickerPacks.pairs: - let isInstalled = installedStickerPacks.hasKey(packId) - let isBought = purchasedStickerPacks.contains(packId) - self.view.addStickerPackToList(stickerPack, isInstalled, isBought) + + self.view.obtainAvailableStickerPacks() let recentStickers = self.status.chat.getRecentStickers() for sticker in recentStickers: diff --git a/src/app/chat/view.nim b/src/app/chat/view.nim index d56c4a8741..c83a05050f 100644 --- a/src/app/chat/view.nim +++ b/src/app/chat/view.nim @@ -1,19 +1,20 @@ import NimQml, Tables, json, sequtils, chronicles, times, re, sugar, strutils, os - import ../../status/status import ../../status/libstatus/accounts/constants import ../../status/accounts as status_accounts import ../../status/chat as status_chat import ../../status/messages as status_messages +import ../../status/libstatus/wallet as status_wallet import ../../status/contacts as status_contacts import ../../status/ens as status_ens import ../../status/chat/[chat, message] import ../../status/libstatus/types import ../../status/profile/profile - +import eth/common/eth_types import ../../status/threads - import views/channels_list, views/message_list, views/chat_item, views/sticker_pack_list, views/sticker_list, views/suggestions_list +import json_serialization +from eth/common/utils import parseAddress logScope: topics = "chats-view" @@ -68,6 +69,27 @@ QtObject: QtProperty[QVariant] stickerPacks: read = getStickerPackList + proc obtainAvailableStickerPacks*(self: ChatsView) = + spawnAndSend(self, "setAvailableStickerPacks") do: + let availableStickerPacks = status_chat.getAvailableStickerPacks() + var packs: seq[StickerPack] = @[] + for packId, stickerPack in availableStickerPacks.pairs: + packs.add(stickerPack) + $(%*(packs)) + + proc setAvailableStickerPacks*(self: ChatsView, availableStickersJSON: string) {.slot.} = + let currAcct = status_wallet.getWalletAccounts()[0] # TODO: make generic + let currAddr = parseAddress(currAcct.address) + let installedStickerPacks = self.status.chat.getInstalledStickerPacks() + let purchasedStickerPacks = self.status.chat.getPurchasedStickerPacks(currAddr) + let availableStickers = JSON.decode($availableStickersJSON, seq[StickerPack]) + + for stickerPack in availableStickers: + let isInstalled = installedStickerPacks.hasKey(stickerPack.id) + let isBought = purchasedStickerPacks.contains(stickerPack.id) + self.status.chat.availableStickerPacks[stickerPack.id] = stickerPack + self.addStickerPackToList(stickerPack, isInstalled, isBought) + proc getChatsList(self: ChatsView): QVariant {.slot.} = newQVariant(self.chats) diff --git a/src/status/chat.nim b/src/status/chat.nim index 0e32986265..8289bdb00e 100644 --- a/src/status/chat.nim +++ b/src/status/chat.nim @@ -126,19 +126,17 @@ proc getInstalledStickerPacks*(self: ChatModel): Table[int, StickerPack] = self.installedStickerPacks = status_stickers.getInstalledStickerPacks() result = self.installedStickerPacks -proc getAvailableStickerPacks*(self: ChatModel): Table[int, StickerPack] = - if self.availableStickerPacks != initTable[int, StickerPack](): - return self.availableStickerPacks - +proc getAvailableStickerPacks*(): Table[int, StickerPack] = + var availableStickerPacks = initTable[int, StickerPack]() try: let numPacks = status_stickers.getPackCount() for i in 0..