fix: show stickers as pending while trx is being mined

Fixes #885
This commit is contained in:
Richard Ramos 2020-09-15 20:30:03 -04:00 committed by Iuri Matias
parent 24e01ffa7c
commit d0bca7afa6
4 changed files with 30 additions and 16 deletions

View File

@ -1,4 +1,4 @@
import NimQml, Tables, json, sequtils, chronicles, times, re, sugar, strutils, os import NimQml, Tables, json, sequtils, chronicles, times, re, sugar, strutils, os, sets
import ../../status/status import ../../status/status
import ../../status/mailservers import ../../status/mailservers
import ../../status/stickers import ../../status/stickers
@ -91,8 +91,8 @@ QtObject:
self.oldestMessageTimestamp = times.toUnix(times.getTime()) self.oldestMessageTimestamp = times.toUnix(times.getTime())
self.oldestMessageTimestampChanged() self.oldestMessageTimestampChanged()
proc addStickerPackToList*(self: ChatsView, stickerPack: StickerPack, isInstalled, isBought: bool) = proc addStickerPackToList*(self: ChatsView, stickerPack: StickerPack, isInstalled, isBought, isPending: bool) =
self.stickerPacks.addStickerPackToList(stickerPack, newStickerList(stickerPack.stickers), isInstalled, isBought) self.stickerPacks.addStickerPackToList(stickerPack, newStickerList(stickerPack.stickers), isInstalled, isBought, isPending)
proc getStickerPackList(self: ChatsView): QVariant {.slot.} = proc getStickerPackList(self: ChatsView): QVariant {.slot.} =
newQVariant(self.stickerPacks) newQVariant(self.stickerPacks)
@ -118,6 +118,7 @@ QtObject:
proc buyStickerPack*(self: ChatsView, packId: int, address: string, price: string, gas: string, gasPrice: string, password: string): string {.slot.} = proc buyStickerPack*(self: ChatsView, packId: int, address: string, price: string, gas: string, gasPrice: string, password: string): string {.slot.} =
try: try:
let response = self.status.stickers.buyPack(packId, address, price, gas, gasPrice, password) let response = self.status.stickers.buyPack(packId, address, price, gas, gasPrice, password)
self.stickerPacks.updateStickerPackInList(packId, false, true)
result = $(%* { "result": %response }) result = $(%* { "result": %response })
self.transactionWasSent(response) self.transactionWasSent(response)
@ -145,11 +146,18 @@ QtObject:
purchasedStickerPacks = self.status.stickers.getPurchasedStickerPacks(address) purchasedStickerPacks = self.status.stickers.getPurchasedStickerPacks(address)
let availableStickers = JSON.decode($availableStickersJSON, seq[StickerPack]) let availableStickers = JSON.decode($availableStickersJSON, seq[StickerPack])
let pendingTransactions = status_wallet.getPendingTransactions().parseJson["result"]
var pendingStickerPacks = initHashSet[int]()
for trx in pendingTransactions.getElems():
if trx["type"].getStr == $PendingTransactionType.BuyStickerPack:
pendingStickerPacks.incl(trx["data"].getStr.parseInt)
for stickerPack in availableStickers: for stickerPack in availableStickers:
let isInstalled = installedStickerPacks.hasKey(stickerPack.id) let isInstalled = installedStickerPacks.hasKey(stickerPack.id)
let isBought = purchasedStickerPacks.contains(stickerPack.id) let isBought = purchasedStickerPacks.contains(stickerPack.id)
let isPending = pendingStickerPacks.contains(stickerPack.id) and not isBought
self.status.stickers.availableStickerPacks[stickerPack.id] = stickerPack self.status.stickers.availableStickerPacks[stickerPack.id] = stickerPack
self.addStickerPackToList(stickerPack, isInstalled, isBought) self.addStickerPackToList(stickerPack, isInstalled, isBought, isPending)
proc getChatsList(self: ChatsView): QVariant {.slot.} = proc getChatsList(self: ChatsView): QVariant {.slot.} =
newQVariant(self.chats) newQVariant(self.chats)
@ -260,12 +268,12 @@ QtObject:
proc installStickerPack*(self: ChatsView, packId: int) {.slot.} = proc installStickerPack*(self: ChatsView, packId: int) {.slot.} =
self.status.stickers.installStickerPack(packId) self.status.stickers.installStickerPack(packId)
self.stickerPacks.updateStickerPackInList(packId, true) self.stickerPacks.updateStickerPackInList(packId, true, false)
proc uninstallStickerPack*(self: ChatsView, packId: int) {.slot.} = proc uninstallStickerPack*(self: ChatsView, packId: int) {.slot.} =
self.status.stickers.uninstallStickerPack(packId) self.status.stickers.uninstallStickerPack(packId)
self.status.stickers.removeRecentStickers(packId) self.status.stickers.removeRecentStickers(packId)
self.stickerPacks.updateStickerPackInList(packId, false) self.stickerPacks.updateStickerPackInList(packId, false, false)
self.recentStickers.removeStickersFromList(packId) self.recentStickers.removeStickersFromList(packId)
proc getRecentStickerList*(self: ChatsView): QVariant {.slot.} = proc getRecentStickerList*(self: ChatsView): QVariant {.slot.} =

View File

@ -1,4 +1,4 @@
import NimQml, Tables, sequtils, sugar import NimQml, Tables, sequtils, sugar, sets
import ../../../status/chat/stickers, ./sticker_list import ../../../status/chat/stickers, ./sticker_list
import ../../../status/libstatus/types, ../../../status/libstatus/utils import ../../../status/libstatus/types, ../../../status/libstatus/utils
@ -13,9 +13,10 @@ type
Thumbnail = UserRole + 7 Thumbnail = UserRole + 7
Installed = UserRole + 8 Installed = UserRole + 8
Bought = UserRole + 9 Bought = UserRole + 9
Pending = UserRole + 10
type type
StickerPackView* = tuple[pack: StickerPack, stickers: StickerList, installed, bought: bool] StickerPackView* = tuple[pack: StickerPack, stickers: StickerList, installed, bought, pending: bool]
QtObject: QtObject:
type type
@ -52,6 +53,7 @@ QtObject:
of StickerPackRoles.Thumbnail: result = newQVariant(decodeContentHash(stickerPack.thumbnail)) of StickerPackRoles.Thumbnail: result = newQVariant(decodeContentHash(stickerPack.thumbnail))
of StickerPackRoles.Installed: result = newQVariant(packInfo.installed) of StickerPackRoles.Installed: result = newQVariant(packInfo.installed)
of StickerPackRoles.Bought: result = newQVariant(packInfo.bought) of StickerPackRoles.Bought: result = newQVariant(packInfo.bought)
of StickerPackRoles.Pending: result = newQVariant(packInfo.pending)
method roleNames(self: StickerPackList): Table[int, string] = method roleNames(self: StickerPackList): Table[int, string] =
{ {
@ -63,7 +65,8 @@ QtObject:
StickerPackRoles.Stickers.int: "stickers", StickerPackRoles.Stickers.int: "stickers",
StickerPackRoles.Thumbnail.int: "thumbnail", StickerPackRoles.Thumbnail.int: "thumbnail",
StickerPackRoles.Installed.int: "installed", StickerPackRoles.Installed.int: "installed",
StickerPackRoles.Bought.int: "bought" StickerPackRoles.Bought.int: "bought",
StickerPackRoles.Pending.int: "pending"
}.toTable }.toTable
@ -85,9 +88,9 @@ QtObject:
raise newException(ValueError, "Sticker pack list does not have a pack with id " & $packId) raise newException(ValueError, "Sticker pack list does not have a pack with id " & $packId)
result = find(self.packs, (view: StickerPackView) => view.pack.id == packId).pack result = find(self.packs, (view: StickerPackView) => view.pack.id == packId).pack
proc addStickerPackToList*(self: StickerPackList, pack: StickerPack, stickers: StickerList, installed, bought: bool) = proc addStickerPackToList*(self: StickerPackList, pack: StickerPack, stickers: StickerList, installed, bought, pending: bool) =
self.beginInsertRows(newQModelIndex(), 0, 0) self.beginInsertRows(newQModelIndex(), 0, 0)
self.packs.insert((pack: pack, stickers: stickers, installed: installed, bought: bought), 0) self.packs.insert((pack: pack, stickers: stickers, installed: installed, bought: bought, pending: pending), 0)
self.endInsertRows() self.endInsertRows()
proc removeStickerPackFromList*(self: StickerPackList, packId: int) = proc removeStickerPackFromList*(self: StickerPackList, packId: int) =
@ -96,7 +99,7 @@ QtObject:
self.packs.keepItIf(it.pack.id != packId) self.packs.keepItIf(it.pack.id != packId)
self.endRemoveRows() self.endRemoveRows()
proc updateStickerPackInList*(self: StickerPackList, packId: int, installed: bool) = proc updateStickerPackInList*(self: StickerPackList, packId: int, installed: bool, pending: bool) =
if not self.hasKey(packId): if not self.hasKey(packId):
return return
@ -104,7 +107,7 @@ QtObject:
let bottomRight = self.createIndex(self.packs.len, 0, nil) let bottomRight = self.createIndex(self.packs.len, 0, nil)
self.packs.apply(proc(it: var StickerPackView) = self.packs.apply(proc(it: var StickerPackView) =
if it.pack.id == packId: if it.pack.id == packId:
it.installed = installed) it.installed = installed
it.pending = pending)
self.dataChanged(topLeft, bottomRight, @[StickerPackRoles.Installed.int])
self.dataChanged(topLeft, bottomRight, @[StickerPackRoles.Installed.int, StickerPackRoles.Pending.int])

View File

@ -242,8 +242,9 @@ Item {
id: mouseArea id: mouseArea
anchors.fill: parent anchors.fill: parent
acceptedButtons: Qt.LeftButton | Qt.RightButton acceptedButtons: Qt.LeftButton | Qt.RightButton
cursorShape: Qt.PointingHandCursor cursorShape: root.isPending ? Qt.ArrowCursor : Qt.PointingHandCursor
onClicked: { onClicked: {
if (root.isPending) return;
if (root.isInstalled) return root.uninstallClicked(); if (root.isInstalled) return root.uninstallClicked();
if (root.packPrice === 0 || root.isBought) return root.installClicked() if (root.packPrice === 0 || root.isBought) return root.installClicked()
if (root.isTimedOut) return root.cancelClicked() if (root.isTimedOut) return root.cancelClicked()

View File

@ -73,6 +73,7 @@ Item {
packPrice: price packPrice: price
isInstalled: installed isInstalled: installed
isBought: bought isBought: bought
isPending: pending
onInstallClicked: root.installClicked(stickers, packId, index) onInstallClicked: root.installClicked(stickers, packId, index)
onUninstallClicked: root.uninstallClicked(packId) onUninstallClicked: root.uninstallClicked(packId)
onCancelClicked: root.cancelClicked(packId) onCancelClicked: root.cancelClicked(packId)
@ -117,6 +118,7 @@ Item {
width: 75 // only needed for Qt Creator width: 75 // only needed for Qt Creator
isInstalled: installed isInstalled: installed
isBought: bought isBought: bought
isPending: pending
onInstallClicked: root.installClicked(stickers, packId, index) onInstallClicked: root.installClicked(stickers, packId, index)
onUninstallClicked: root.uninstallClicked(packId) onUninstallClicked: root.uninstallClicked(packId)
onCancelClicked: root.cancelClicked(packId) onCancelClicked: root.cancelClicked(packId)