mirror of
https://github.com/status-im/status-desktop.git
synced 2025-01-15 00:55:22 +00:00
refactor(stickers): remove dep on dto in view and use Item instead
This commit is contained in:
parent
80fcb95245
commit
206e0e5504
@ -1,4 +1,5 @@
|
|||||||
import Tables, stint
|
import Tables, stint
|
||||||
|
import ./item
|
||||||
import ../../../../app_service/service/wallet_account/service as wallet_account_service
|
import ../../../../app_service/service/wallet_account/service as wallet_account_service
|
||||||
import ../../../../app_service/service/stickers/service as stickers_service
|
import ../../../../app_service/service/stickers/service as stickers_service
|
||||||
|
|
||||||
@ -60,7 +61,7 @@ method wei2Eth*(self: AccessInterface, price: Stuint[256]): string {.base.} =
|
|||||||
method removeRecentStickers*(self: AccessInterface, packId: int) {.base.} =
|
method removeRecentStickers*(self: AccessInterface, packId: int) {.base.} =
|
||||||
raise newException(ValueError, "No implementation available")
|
raise newException(ValueError, "No implementation available")
|
||||||
|
|
||||||
method sendSticker*(self: AccessInterface, channelId: string, replyTo: string, sticker: StickerDto) {.base.} =
|
method sendSticker*(self: AccessInterface, channelId: string, replyTo: string, sticker: Item) {.base.} =
|
||||||
raise newException(ValueError, "No implementation available")
|
raise newException(ValueError, "No implementation available")
|
||||||
|
|
||||||
method populateInstalledStickerPacks*(self: AccessInterface, stickers: Table[int, StickerPackDto]) {.base.} =
|
method populateInstalledStickerPacks*(self: AccessInterface, stickers: Table[int, StickerPackDto]) {.base.} =
|
||||||
|
90
src/app/modules/main/stickers/item.nim
Normal file
90
src/app/modules/main/stickers/item.nim
Normal file
@ -0,0 +1,90 @@
|
|||||||
|
import strformat, stint
|
||||||
|
|
||||||
|
#####
|
||||||
|
# Sticker Item
|
||||||
|
#####
|
||||||
|
type
|
||||||
|
Item* = object
|
||||||
|
hash: string
|
||||||
|
packId: int
|
||||||
|
|
||||||
|
proc initItem*(
|
||||||
|
hash: string,
|
||||||
|
packId: int
|
||||||
|
): Item =
|
||||||
|
result.hash = hash
|
||||||
|
result.packId = packId
|
||||||
|
|
||||||
|
proc `$`*(self: Item): string =
|
||||||
|
result = fmt"""StickerItem(
|
||||||
|
hash: {self.hash},
|
||||||
|
packId: {$self.packId}
|
||||||
|
]"""
|
||||||
|
|
||||||
|
proc getHash*(self: Item): string =
|
||||||
|
return self.hash
|
||||||
|
|
||||||
|
proc getPackId*(self: Item): int =
|
||||||
|
return self.packId
|
||||||
|
|
||||||
|
#####
|
||||||
|
# Sticker Pack Item
|
||||||
|
#####
|
||||||
|
type
|
||||||
|
PackItem* = object
|
||||||
|
id*: int
|
||||||
|
name*: string
|
||||||
|
author*: string
|
||||||
|
price*: Stuint[256]
|
||||||
|
preview*: string
|
||||||
|
stickers*: seq[Item]
|
||||||
|
thumbnail*: string
|
||||||
|
|
||||||
|
proc initPackItem*(
|
||||||
|
id: int,
|
||||||
|
name: string,
|
||||||
|
author: string,
|
||||||
|
price: Stuint[256],
|
||||||
|
preview: string,
|
||||||
|
stickers: seq[Item],
|
||||||
|
thumbnail: string
|
||||||
|
): PackItem =
|
||||||
|
result.id = id
|
||||||
|
result.name = name
|
||||||
|
result.author = author
|
||||||
|
result.price = price
|
||||||
|
result.preview = preview
|
||||||
|
result.stickers = stickers
|
||||||
|
result.thumbnail = thumbnail
|
||||||
|
|
||||||
|
proc `$`*(self: PackItem): string =
|
||||||
|
result = fmt"""StickerItem(
|
||||||
|
id: {self.id},
|
||||||
|
name: {$self.name},
|
||||||
|
author: {$self.author},
|
||||||
|
price: {$self.price},
|
||||||
|
preview: {$self.preview},
|
||||||
|
stickers: {$self.stickers},
|
||||||
|
thumbnail: {$self.thumbnail},
|
||||||
|
]"""
|
||||||
|
|
||||||
|
proc getId*(self: PackItem): int =
|
||||||
|
return self.id
|
||||||
|
|
||||||
|
proc getName*(self: PackItem): string =
|
||||||
|
return self.name
|
||||||
|
|
||||||
|
proc getAuthor*(self: PackItem): string =
|
||||||
|
return self.author
|
||||||
|
|
||||||
|
proc getPrice*(self: PackItem): Stuint[256] =
|
||||||
|
return self.price
|
||||||
|
|
||||||
|
proc getPreview*(self: PackItem): string =
|
||||||
|
return self.preview
|
||||||
|
|
||||||
|
proc getThumbnail*(self: PackItem): string =
|
||||||
|
return self.thumbnail
|
||||||
|
|
||||||
|
proc getStickers*(self: PackItem): seq[Item] =
|
||||||
|
return self.stickers
|
@ -1,6 +1,5 @@
|
|||||||
import NimQml, Tables, sequtils
|
import NimQml, Tables, sequtils
|
||||||
import ../../../../../app_service/service/stickers/dto/stickers as stickers_dto
|
import ../io_interface, ../item
|
||||||
import ../io_interface
|
|
||||||
|
|
||||||
type
|
type
|
||||||
StickerRoles {.pure.} = enum
|
StickerRoles {.pure.} = enum
|
||||||
@ -12,13 +11,13 @@ QtObject:
|
|||||||
type
|
type
|
||||||
StickerList* = ref object of QAbstractListModel
|
StickerList* = ref object of QAbstractListModel
|
||||||
delegate: io_interface.AccessInterface
|
delegate: io_interface.AccessInterface
|
||||||
stickers*: seq[StickerDto]
|
stickers*: seq[Item]
|
||||||
|
|
||||||
proc setup(self: StickerList) = self.QAbstractListModel.setup
|
proc setup(self: StickerList) = self.QAbstractListModel.setup
|
||||||
|
|
||||||
proc delete(self: StickerList) = self.QAbstractListModel.delete
|
proc delete(self: StickerList) = self.QAbstractListModel.delete
|
||||||
|
|
||||||
proc newStickerList*(delegate: io_interface.AccessInterface, stickers: seq[StickerDto] = @[]): StickerList =
|
proc newStickerList*(delegate: io_interface.AccessInterface, stickers: seq[Item] = @[]): StickerList =
|
||||||
new(result, delete)
|
new(result, delete)
|
||||||
result.delegate = delegate
|
result.delegate = delegate
|
||||||
result.stickers = stickers
|
result.stickers = stickers
|
||||||
@ -35,9 +34,9 @@ QtObject:
|
|||||||
let sticker = self.stickers[index.row]
|
let sticker = self.stickers[index.row]
|
||||||
let stickerRole = role.StickerRoles
|
let stickerRole = role.StickerRoles
|
||||||
case stickerRole:
|
case stickerRole:
|
||||||
of StickerRoles.Url: result = newQVariant(self.delegate.decodeContentHash(sticker.hash))
|
of StickerRoles.Url: result = newQVariant(self.delegate.decodeContentHash(sticker.getHash))
|
||||||
of StickerRoles.Hash: result = newQVariant(sticker.hash)
|
of StickerRoles.Hash: result = newQVariant(sticker.getHash)
|
||||||
of StickerRoles.PackId: result = newQVariant(sticker.packId)
|
of StickerRoles.PackId: result = newQVariant(sticker.getPackId)
|
||||||
|
|
||||||
method roleNames(self: StickerList): Table[int, string] =
|
method roleNames(self: StickerList): Table[int, string] =
|
||||||
{
|
{
|
||||||
@ -46,16 +45,16 @@ QtObject:
|
|||||||
StickerRoles.PackId.int:"packId"
|
StickerRoles.PackId.int:"packId"
|
||||||
}.toTable
|
}.toTable
|
||||||
|
|
||||||
proc addStickerToList*(self: StickerList, sticker: StickerDto) =
|
proc addStickerToList*(self: StickerList, sticker: Item) =
|
||||||
if(self.stickers.any(proc(existingSticker: StickerDto): bool = return existingSticker.hash == sticker.hash)):
|
if(self.stickers.any(proc(existingSticker: Item): bool = return existingSticker.getHash == sticker.getHash)):
|
||||||
return
|
return
|
||||||
self.beginInsertRows(newQModelIndex(), 0, 0)
|
self.beginInsertRows(newQModelIndex(), 0, 0)
|
||||||
self.stickers.insert(sticker, 0)
|
self.stickers.insert(sticker, 0)
|
||||||
self.endInsertRows()
|
self.endInsertRows()
|
||||||
|
|
||||||
proc removeStickersFromList*(self: StickerList, packId: int) =
|
proc removeStickersFromList*(self: StickerList, packId: int) =
|
||||||
if not self.stickers.anyIt(it.packId == packId):
|
if not self.stickers.anyIt(it.getPackId == packId):
|
||||||
return
|
return
|
||||||
self.beginRemoveRows(newQModelIndex(), 0, 0)
|
self.beginRemoveRows(newQModelIndex(), 0, 0)
|
||||||
self.stickers.keepItIf(it.packId != packId)
|
self.stickers.keepItIf(it.getPackId != packId)
|
||||||
self.endRemoveRows()
|
self.endRemoveRows()
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import NimQml, Tables, sequtils, sugar
|
import NimQml, Tables, sequtils, sugar
|
||||||
import ./sticker_list
|
import ./sticker_list
|
||||||
import ../io_interface
|
import ../io_interface, ../item
|
||||||
# TODO remove those uses of services stuff
|
# TODO remove those uses of services stuff
|
||||||
import ../../../../../app_service/service/stickers/dto/stickers as stickers_dto
|
|
||||||
import ../../../../../app_service/service/eth/utils as eth_utils
|
import ../../../../../app_service/service/eth/utils as eth_utils
|
||||||
|
|
||||||
type
|
type
|
||||||
@ -19,7 +18,7 @@ type
|
|||||||
Pending = UserRole + 10
|
Pending = UserRole + 10
|
||||||
|
|
||||||
type
|
type
|
||||||
StickerPackView* = tuple[pack: StickerPackDto, stickers: StickerList, installed, bought, pending: bool]
|
StickerPackView* = tuple[pack: PackItem, stickers: StickerList, installed, bought, pending: bool]
|
||||||
|
|
||||||
QtObject:
|
QtObject:
|
||||||
type
|
type
|
||||||
@ -93,12 +92,12 @@ QtObject:
|
|||||||
proc hasKey*(self: StickerPackList, packId: int): bool =
|
proc hasKey*(self: StickerPackList, packId: int): bool =
|
||||||
result = self.packs.anyIt(it.pack.id == packId)
|
result = self.packs.anyIt(it.pack.id == packId)
|
||||||
|
|
||||||
proc `[]`*(self: StickerPackList, packId: int): StickerPackDto =
|
proc `[]`*(self: StickerPackList, packId: int): PackItem =
|
||||||
if not self.hasKey(packId):
|
if not self.hasKey(packId):
|
||||||
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 = eth_utils.find(self.packs, (view: StickerPackView) => view.pack.id == packId).pack
|
result = eth_utils.find(self.packs, (view: StickerPackView) => view.pack.id == packId).pack
|
||||||
|
|
||||||
proc addStickerPackToList*(self: StickerPackList, pack: StickerPackDto, stickers: StickerList, installed, bought, pending: bool) =
|
proc addStickerPackToList*(self: StickerPackList, pack: PackItem, 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, pending: pending), 0)
|
self.packs.insert((pack: pack, stickers: stickers, installed: installed, bought: bought, pending: pending), 0)
|
||||||
self.endInsertRows()
|
self.endInsertRows()
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import NimQml, Tables, stint
|
import NimQml, Tables, stint, sugar, sequtils
|
||||||
|
|
||||||
import eventemitter
|
import eventemitter
|
||||||
import ./io_interface, ./view, ./controller
|
import ./io_interface, ./view, ./controller, ./item
|
||||||
import ../../../global/global_singleton
|
import ../../../global/global_singleton
|
||||||
import ../../../../app_service/service/stickers/service as stickers_service
|
import ../../../../app_service/service/stickers/service as stickers_service
|
||||||
|
|
||||||
@ -73,14 +73,15 @@ method decodeContentHash*[T](self: Module[T], hash: string): string =
|
|||||||
method wei2Eth*[T](self: Module[T], price: Stuint[256]): string =
|
method wei2Eth*[T](self: Module[T], price: Stuint[256]): string =
|
||||||
self.controller.wei2Eth(price)
|
self.controller.wei2Eth(price)
|
||||||
|
|
||||||
method sendSticker*[T](self: Module[T], channelId: string, replyTo: string, sticker: StickerDto) =
|
method sendSticker*[T](self: Module[T], channelId: string, replyTo: string, sticker: Item) =
|
||||||
self.controller.sendSticker(channelId, replyTo, sticker)
|
let stickerDto = StickerDto(hash: sticker.getHash, packId: sticker.getPackId)
|
||||||
|
self.controller.sendSticker(channelId, replyTo, stickerDto)
|
||||||
|
|
||||||
method estimate*[T](self: Module[T], packId: int, address: string, price: string, uuid: string) =
|
method estimate*[T](self: Module[T], packId: int, address: string, price: string, uuid: string) =
|
||||||
self.controller.estimate(packId, address, price, uuid)
|
self.controller.estimate(packId, address, price, uuid)
|
||||||
|
|
||||||
method addRecentStickerToList*[T](self: Module[T], sticker: StickerDto) =
|
method addRecentStickerToList*[T](self: Module[T], sticker: StickerDto) =
|
||||||
self.view.addRecentStickerToList(sticker)
|
self.view.addRecentStickerToList(initItem(sticker.hash, sticker.packId))
|
||||||
|
|
||||||
method clearStickerPacks*[T](self: Module[T]) =
|
method clearStickerPacks*[T](self: Module[T]) =
|
||||||
self.view.clearStickerPacks()
|
self.view.clearStickerPacks()
|
||||||
@ -89,10 +90,31 @@ method allPacksLoaded*[T](self: Module[T]) =
|
|||||||
self.view.allPacksLoaded()
|
self.view.allPacksLoaded()
|
||||||
|
|
||||||
method populateInstalledStickerPacks*[T](self: Module[T], stickers: Table[int, StickerPackDto]) =
|
method populateInstalledStickerPacks*[T](self: Module[T], stickers: Table[int, StickerPackDto]) =
|
||||||
self.view.populateInstalledStickerPacks(stickers)
|
var stickerPackItems: seq[PackItem] = @[]
|
||||||
|
for stickerPack in stickers.values:
|
||||||
|
stickerPackItems.add(initPackItem(
|
||||||
|
stickerPack.id,
|
||||||
|
stickerPack.name,
|
||||||
|
stickerPack.author,
|
||||||
|
stickerPack.price,
|
||||||
|
stickerPack.preview,
|
||||||
|
stickerPack.stickers.map(s => initItem(s.hash, s.packId)),
|
||||||
|
stickerPack.thumbnail
|
||||||
|
))
|
||||||
|
|
||||||
|
self.view.populateInstalledStickerPacks(stickerPackItems)
|
||||||
|
|
||||||
method gasEstimateReturned*[T](self: Module[T], estimate: int, uuid: string) =
|
method gasEstimateReturned*[T](self: Module[T], estimate: int, uuid: string) =
|
||||||
self.view.gasEstimateReturned(estimate, uuid)
|
self.view.gasEstimateReturned(estimate, uuid)
|
||||||
|
|
||||||
method addStickerPackToList*[T](self: Module[T], stickerPack: StickerPackDto, isInstalled: bool, isBought: bool, isPending: bool) =
|
method addStickerPackToList*[T](self: Module[T], stickerPack: StickerPackDto, isInstalled: bool, isBought: bool, isPending: bool) =
|
||||||
self.view.addStickerPackToList(stickerPack, isInstalled, isBought, isPending)
|
let stickerPackItem = initPackItem(
|
||||||
|
stickerPack.id,
|
||||||
|
stickerPack.name,
|
||||||
|
stickerPack.author,
|
||||||
|
stickerPack.price,
|
||||||
|
stickerPack.preview,
|
||||||
|
stickerPack.stickers.map(s => initItem(s.hash, s.packId)),
|
||||||
|
stickerPack.thumbnail
|
||||||
|
)
|
||||||
|
self.view.addStickerPackToList(stickerPackItem, isInstalled, isBought, isPending)
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
import NimQml, json, strutils, tables, json_serialization
|
import NimQml, json, strutils, json_serialization
|
||||||
|
|
||||||
import ./models/[sticker_list, sticker_pack_list]
|
import ./models/[sticker_list, sticker_pack_list]
|
||||||
import ./io_interface
|
import ./io_interface, ./item
|
||||||
import ../../../../app_service/service/stickers/dto/stickers
|
|
||||||
|
|
||||||
QtObject:
|
QtObject:
|
||||||
type
|
type
|
||||||
@ -24,7 +23,7 @@ QtObject:
|
|||||||
proc load*(self: View) =
|
proc load*(self: View) =
|
||||||
self.delegate.viewDidLoad()
|
self.delegate.viewDidLoad()
|
||||||
|
|
||||||
proc addStickerPackToList*(self: View, stickerPack: StickerPackDto, isInstalled, isBought, isPending: bool) =
|
proc addStickerPackToList*(self: View, stickerPack: PackItem, isInstalled, isBought, isPending: bool) =
|
||||||
self.stickerPacks.addStickerPackToList(
|
self.stickerPacks.addStickerPackToList(
|
||||||
stickerPack,
|
stickerPack,
|
||||||
newStickerList(self.delegate, stickerPack.stickers),
|
newStickerList(self.delegate, stickerPack.stickers),
|
||||||
@ -71,11 +70,10 @@ QtObject:
|
|||||||
proc clearStickerPacks*(self: View) =
|
proc clearStickerPacks*(self: View) =
|
||||||
self.stickerPacks.clear()
|
self.stickerPacks.clear()
|
||||||
|
|
||||||
proc populateInstalledStickerPacks*(self: View, installedStickerPacks: Table[int, StickerPackDto]) =
|
proc populateInstalledStickerPacks*(self: View, installedStickerPacks: seq[PackItem]) =
|
||||||
for stickerPack in installedStickerPacks.values:
|
for stickerPack in installedStickerPacks:
|
||||||
self.addStickerPackToList(stickerPack, isInstalled = true, isBought = true, isPending = false)
|
self.addStickerPackToList(stickerPack, isInstalled = true, isBought = true, isPending = false)
|
||||||
|
|
||||||
|
|
||||||
proc getNumInstalledStickerPacks(self: View): int {.slot.} =
|
proc getNumInstalledStickerPacks(self: View): int {.slot.} =
|
||||||
self.delegate.getNumInstalledStickerPacks()
|
self.delegate.getNumInstalledStickerPacks()
|
||||||
|
|
||||||
@ -99,7 +97,7 @@ QtObject:
|
|||||||
self.installedStickerPacksUpdated()
|
self.installedStickerPacksUpdated()
|
||||||
self.recentStickersUpdated()
|
self.recentStickersUpdated()
|
||||||
|
|
||||||
proc addRecentStickerToList*(self: View, sticker: StickerDto) =
|
proc addRecentStickerToList*(self: View, sticker: Item) =
|
||||||
self.recentStickers.addStickerToList(sticker)
|
self.recentStickers.addStickerToList(sticker)
|
||||||
|
|
||||||
proc allPacksLoaded*(self: View) =
|
proc allPacksLoaded*(self: View) =
|
||||||
@ -107,7 +105,7 @@ QtObject:
|
|||||||
self.installedStickerPacksUpdated()
|
self.installedStickerPacksUpdated()
|
||||||
|
|
||||||
proc send*(self: View, channelId: string, hash: string, replyTo: string, pack: int) {.slot.} =
|
proc send*(self: View, channelId: string, hash: string, replyTo: string, pack: int) {.slot.} =
|
||||||
let sticker = StickerDto(hash: hash, packId: pack)
|
let sticker = initItem(hash, pack)
|
||||||
self.addRecentStickerToList(sticker)
|
self.addRecentStickerToList(sticker)
|
||||||
self.delegate.sendSticker(channelId, replyTo, sticker)
|
self.delegate.sendSticker(channelId, replyTo, sticker)
|
||||||
|
|
||||||
|
@ -38,8 +38,7 @@ proc initItem*(
|
|||||||
result.assets = assets
|
result.assets = assets
|
||||||
|
|
||||||
proc `$`*(self: Item): string =
|
proc `$`*(self: Item): string =
|
||||||
result = fmt"""AllTokensItem(
|
result = fmt"""WalletAccountItem(
|
||||||
name: {self.name},
|
|
||||||
name: {self.name},
|
name: {self.name},
|
||||||
address: {self.address},
|
address: {self.address},
|
||||||
path: {self.path},
|
path: {self.path},
|
||||||
|
Loading…
x
Reference in New Issue
Block a user