refactor(stickers): remove dep on dto in view and use Item instead

This commit is contained in:
Jonathan Rainville 2021-12-01 11:56:15 -05:00 committed by Sale Djenic
parent 80fcb95245
commit 206e0e5504
7 changed files with 143 additions and 35 deletions

View File

@ -1,4 +1,5 @@
import Tables, stint
import ./item
import ../../../../app_service/service/wallet_account/service as wallet_account_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.} =
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")
method populateInstalledStickerPacks*(self: AccessInterface, stickers: Table[int, StickerPackDto]) {.base.} =

View 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

View File

@ -1,6 +1,5 @@
import NimQml, Tables, sequtils
import ../../../../../app_service/service/stickers/dto/stickers as stickers_dto
import ../io_interface
import ../io_interface, ../item
type
StickerRoles {.pure.} = enum
@ -12,13 +11,13 @@ QtObject:
type
StickerList* = ref object of QAbstractListModel
delegate: io_interface.AccessInterface
stickers*: seq[StickerDto]
stickers*: seq[Item]
proc setup(self: StickerList) = self.QAbstractListModel.setup
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)
result.delegate = delegate
result.stickers = stickers
@ -35,9 +34,9 @@ QtObject:
let sticker = self.stickers[index.row]
let stickerRole = role.StickerRoles
case stickerRole:
of StickerRoles.Url: result = newQVariant(self.delegate.decodeContentHash(sticker.hash))
of StickerRoles.Hash: result = newQVariant(sticker.hash)
of StickerRoles.PackId: result = newQVariant(sticker.packId)
of StickerRoles.Url: result = newQVariant(self.delegate.decodeContentHash(sticker.getHash))
of StickerRoles.Hash: result = newQVariant(sticker.getHash)
of StickerRoles.PackId: result = newQVariant(sticker.getPackId)
method roleNames(self: StickerList): Table[int, string] =
{
@ -46,16 +45,16 @@ QtObject:
StickerRoles.PackId.int:"packId"
}.toTable
proc addStickerToList*(self: StickerList, sticker: StickerDto) =
if(self.stickers.any(proc(existingSticker: StickerDto): bool = return existingSticker.hash == sticker.hash)):
proc addStickerToList*(self: StickerList, sticker: Item) =
if(self.stickers.any(proc(existingSticker: Item): bool = return existingSticker.getHash == sticker.getHash)):
return
self.beginInsertRows(newQModelIndex(), 0, 0)
self.stickers.insert(sticker, 0)
self.endInsertRows()
proc removeStickersFromList*(self: StickerList, packId: int) =
if not self.stickers.anyIt(it.packId == packId):
if not self.stickers.anyIt(it.getPackId == packId):
return
self.beginRemoveRows(newQModelIndex(), 0, 0)
self.stickers.keepItIf(it.packId != packId)
self.stickers.keepItIf(it.getPackId != packId)
self.endRemoveRows()

View File

@ -1,8 +1,7 @@
import NimQml, Tables, sequtils, sugar
import ./sticker_list
import ../io_interface
import ../io_interface, ../item
# 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
type
@ -19,7 +18,7 @@ type
Pending = UserRole + 10
type
StickerPackView* = tuple[pack: StickerPackDto, stickers: StickerList, installed, bought, pending: bool]
StickerPackView* = tuple[pack: PackItem, stickers: StickerList, installed, bought, pending: bool]
QtObject:
type
@ -93,12 +92,12 @@ QtObject:
proc hasKey*(self: StickerPackList, packId: int): bool =
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):
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
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.packs.insert((pack: pack, stickers: stickers, installed: installed, bought: bought, pending: pending), 0)
self.endInsertRows()

View File

@ -1,7 +1,7 @@
import NimQml, Tables, stint
import NimQml, Tables, stint, sugar, sequtils
import eventemitter
import ./io_interface, ./view, ./controller
import ./io_interface, ./view, ./controller, ./item
import ../../../global/global_singleton
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 =
self.controller.wei2Eth(price)
method sendSticker*[T](self: Module[T], channelId: string, replyTo: string, sticker: StickerDto) =
self.controller.sendSticker(channelId, replyTo, sticker)
method sendSticker*[T](self: Module[T], channelId: string, replyTo: string, sticker: Item) =
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) =
self.controller.estimate(packId, address, price, uuid)
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]) =
self.view.clearStickerPacks()
@ -89,10 +90,31 @@ method allPacksLoaded*[T](self: Module[T]) =
self.view.allPacksLoaded()
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) =
self.view.gasEstimateReturned(estimate, uuid)
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)

View File

@ -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 ./io_interface
import ../../../../app_service/service/stickers/dto/stickers
import ./io_interface, ./item
QtObject:
type
@ -24,7 +23,7 @@ QtObject:
proc load*(self: View) =
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(
stickerPack,
newStickerList(self.delegate, stickerPack.stickers),
@ -71,11 +70,10 @@ QtObject:
proc clearStickerPacks*(self: View) =
self.stickerPacks.clear()
proc populateInstalledStickerPacks*(self: View, installedStickerPacks: Table[int, StickerPackDto]) =
for stickerPack in installedStickerPacks.values:
proc populateInstalledStickerPacks*(self: View, installedStickerPacks: seq[PackItem]) =
for stickerPack in installedStickerPacks:
self.addStickerPackToList(stickerPack, isInstalled = true, isBought = true, isPending = false)
proc getNumInstalledStickerPacks(self: View): int {.slot.} =
self.delegate.getNumInstalledStickerPacks()
@ -99,7 +97,7 @@ QtObject:
self.installedStickerPacksUpdated()
self.recentStickersUpdated()
proc addRecentStickerToList*(self: View, sticker: StickerDto) =
proc addRecentStickerToList*(self: View, sticker: Item) =
self.recentStickers.addStickerToList(sticker)
proc allPacksLoaded*(self: View) =
@ -107,7 +105,7 @@ QtObject:
self.installedStickerPacksUpdated()
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.delegate.sendSticker(channelId, replyTo, sticker)

View File

@ -38,8 +38,7 @@ proc initItem*(
result.assets = assets
proc `$`*(self: Item): string =
result = fmt"""AllTokensItem(
name: {self.name},
result = fmt"""WalletAccountItem(
name: {self.name},
address: {self.address},
path: {self.path},