Skip to content

Commit

Permalink
fix(@desktop/wallet): fix how collectibles are shown after an error, …
Browse files Browse the repository at this point in the history
…according to the design

Part of #10063
  • Loading branch information
dlipicar committed Apr 3, 2023
1 parent 7f72f87 commit bbf2600
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 103 deletions.
2 changes: 1 addition & 1 deletion src/app/boot/app_controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ proc newAppController*(statusFoundation: StatusFoundation): AppController =
result.collectibleService = collectible_service.newService(statusFoundation.events, statusFoundation.threadpool, result.networkService)
result.walletAccountService = wallet_account_service.newService(
statusFoundation.events, statusFoundation.threadpool, result.settingsService, result.accountsService,
result.tokenService, result.networkService, result.collectibleService
result.tokenService, result.networkService
)
result.messageService = message_service.newService(
statusFoundation.events, statusFoundation.threadpool, result.contactsService, result.tokenService, result.walletAccountService, result.networkService
Expand Down
29 changes: 11 additions & 18 deletions src/app/modules/main/wallet_section/collectibles/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type
networkConnectionService: network_connection_service.Service

# Forward declaration
proc resetOwnedCollectibles*(self: Controller, chainId: int, address: string)
proc refetchOwnedCollectibles*(self: Controller, chainId: int, address: string)

proc newController*(
delegate: io_interface.AccessInterface,
Expand All @@ -45,36 +45,29 @@ proc resetCollectibles(self: Controller, chainId: int, address: string) =
let data = self.collectibleService.getOwnedCollectibles(chainId, address)
self.delegate.setCollectibles(chainId, address, data)

proc processNewCollectibles(self: Controller, chainId: int, address: string) =
proc updateCollectibles(self: Controller, chainId: int, address: string) =
let data = self.collectibleService.getOwnedCollectibles(chainId, address)
self.delegate.appendCollectibles(chainId, address, data)

if not self.nodeService.isConnected() or not self.networkConnectionService.checkIfConnected(COLLECTIBLES):
self.delegate.connectionToOpenSea(false)

proc init*(self: Controller) =
self.events.on(SIGNAL_OWNED_COLLECTIBLES_RESET) do(e:Args):
self.events.on(SIGNAL_OWNED_COLLECTIBLES_REFETCH) do(e:Args):
let args = OwnedCollectiblesUpdateArgs(e)
self.resetCollectibles(args.chainId, args.address)

self.events.on(SIGNAL_OWNED_COLLECTIBLES_UPDATE_ERROR) do(e:Args):
let args = OwnedCollectiblesUpdateArgs(e)
self.updateCollectibles(args.chainId, args.address)

self.events.on(SIGNAL_OWNED_COLLECTIBLES_UPDATE_STARTED) do(e:Args):
let args = OwnedCollectiblesUpdateArgs(e)
self.delegate.onFetchStarted(args.chainId, args.address)

self.events.on(SIGNAL_OWNED_COLLECTIBLES_UPDATE_FINISHED) do(e:Args):
let args = OwnedCollectiblesUpdateArgs(e)
self.processNewCollectibles(args.chainId, args.address)
self.updateCollectibles(args.chainId, args.address)

self.events.on(SIGNAL_REFRESH_COLLECTIBLES) do(e:Args):
self.collectibleService.resetAllOwnedCollectibles()

self.events.on(SIGNAL_NETWORK_DISCONNECTED) do(e: Args):
self.delegate.connectionToOpenSea(false)

self.events.on(SIGNAL_CONNECTION_UPDATE) do(e:Args):
let args = NetworkConnectionsArgs(e)
if args.website == COLLECTIBLES:
self.delegate.connectionToOpenSea(not args.completelyDown)
self.collectibleService.refetchAllOwnedCollectibles()

proc getWalletAccount*(self: Controller, accountIndex: int): wallet_account_service.WalletAccountDto =
return self.walletAccountService.getWalletAccount(accountIndex)
Expand All @@ -85,8 +78,8 @@ proc getNetwork*(self: Controller): network_service.NetworkDto =
proc getOwnedCollectibles*(self: Controller, chainId: int, address: string): CollectiblesData =
return self.collectibleService.getOwnedCollectibles(chainId, address)

proc resetOwnedCollectibles*(self: Controller, chainId: int, address: string) =
self.collectibleService.resetOwnedCollectibles(chainId, address)
proc refetchOwnedCollectibles*(self: Controller, chainId: int, address: string) =
self.collectibleService.refetchOwnedCollectibles(chainId, address)

proc fetchOwnedCollectibles*(self: Controller, chainId: int, address: string) =
self.collectibleService.fetchOwnedCollectibles(chainId, address)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,3 @@ method collectiblesModuleDidLoad*(self: AccessInterface) {.base.} =

method currentCollectibleModuleDidLoad*(self: AccessInterface) {.base.} =
raise newException(ValueError, "No implementation available")

method connectionToOpenSea*(self: AccessInterface, connected: bool) {.base.} =
raise newException(ValueError, "No implementation available")
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ QtObject:
allCollectiblesLoaded: bool
isFetching: bool
isError: bool
loadingItemsStartIdx: int

proc appendLoadingItems(self: Model)
proc removeLoadingItems(self: Model)
Expand All @@ -48,6 +49,7 @@ QtObject:
result.allCollectiblesLoaded = false
result.isFetching = false
result.isError = false
result.loadingItemsStartIdx = -1

proc `$`*(self: Model): string =
for i in 0 ..< self.items.len:
Expand Down Expand Up @@ -181,27 +183,32 @@ QtObject:
result = newQVariant(item.getIsPinned())

proc appendLoadingItems(self: Model) =
if not self.loadingItemsStartIdx < 0:
return

let parentModelIndex = newQModelIndex()
defer: parentModelIndex.delete

let loadingItem = initLoadingItem()
self.beginInsertRows(parentModelIndex, self.items.len, self.items.len + loadingItemsCount - 1)
self.loadingItemsStartIdx = self.items.len
self.beginInsertRows(parentModelIndex, self.loadingItemsStartIdx, self.loadingItemsStartIdx + loadingItemsCount - 1)
for i in 1..loadingItemsCount:
self.items.add(loadingItem)
self.endInsertRows()
self.countChanged()

proc removeLoadingItems(self: Model) =
if self.loadingItemsStartIdx < 0:
return

let parentModelIndex = newQModelIndex()
defer: parentModelIndex.delete

for i in 0 ..< self.items.len:
if self.items[i].getIsLoading():
self.beginRemoveRows(parentModelIndex, i, i + loadingItemsCount - 1)
self.items.delete(i, i + loadingItemsCount - 1)
self.endRemoveRows()
self.countChanged()
return
self.beginRemoveRows(parentModelIndex, self.loadingItemsStartIdx, self.loadingItemsStartIdx + loadingItemsCount - 1)
self.items.delete(self.loadingItemsStartIdx, self.loadingItemsStartIdx + loadingItemsCount - 1)
self.loadingItemsStartIdx = -1
self.endRemoveRows()
self.countChanged()

proc setItems*(self: Model, items: seq[Item]) =
if self.isFetching:
Expand All @@ -224,8 +231,3 @@ QtObject:
self.countChanged()
if self.isFetching:
self.appendLoadingItems()

# in case loading is still going on and no items are present, show loading items when there is no connection to opensea possible
proc connectionToOpenSea*(self: Model, connected: bool) =
if self.items.len == 0:
self.setIsFetching(not connected)
22 changes: 16 additions & 6 deletions src/app/modules/main/wallet_section/collectibles/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -108,27 +108,37 @@ method onFetchStarted*(self: Module, chainId: int, address: string) =

method setCollectibles*(self: Module, chainId: int, address: string, data: CollectiblesData) =
if self.chainId == chainId and self.address == address:
self.view.setIsFetching(data.isFetching)
self.view.setIsError(data.isError)

if data.isError and not data.anyLoaded:
# If fetching failed before being able to get any collectibles info,
# show loading animation
self.view.setIsFetching(true)
else:
self.view.setIsFetching(data.isFetching)

var newCollectibles = data.collectibles.map(oc => self.ownedCollectibleToItem(oc))
self.view.setCollectibles(newCollectibles)
self.view.setAllLoaded(data.allLoaded)

method appendCollectibles*(self: Module, chainId: int, address: string, data: CollectiblesData) =
if self.chainId == chainId and self.address == address:
self.view.setIsFetching(data.isFetching)
self.view.setIsError(data.isError)

if not data.isError:
if data.isError and not data.anyLoaded:
# If fetching failed before being able to get any collectibles info,
# show loading animation
self.view.setIsFetching(true)
else:
self.view.setIsFetching(data.isFetching)

if data.lastLoadCount > 0:
var ownedCollectiblesToAdd = newSeq[OwnedCollectible]()
for i in data.collectibles.len - data.lastLoadCount ..< data.collectibles.len:
ownedCollectiblesToAdd.add(data.collectibles[i])

let newCollectibles = ownedCollectiblesToAdd.map(oc => self.ownedCollectibleToItem(oc))

self.view.appendCollectibles(newCollectibles)
self.view.setAllLoaded(data.allLoaded)

method connectionToOpenSea*(self: Module, connected: bool) =
self.view.connectionToOpenSea(connected)
self.view.setAllLoaded(data.allLoaded)
2 changes: 0 additions & 2 deletions src/app/modules/main/wallet_section/collectibles/view.nim
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,3 @@ QtObject:
proc appendCollectibles*(self: View, collectibles: seq[Item]) =
self.model.appendItems(collectibles)

proc connectionToOpenSea*(self: View, connected: bool) =
self.model.connectionToOpenSea(connected)
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import ../../../../../app_service/service/wallet_account/service as wallet_accou
import ../../../../../app_service/service/network/service as network_service
import ../../../../../app_service/service/token/service as token_service
import ../../../../../app_service/service/currency/service as currency_service
import ../../../../../app_service/service/collectible/service as collectible_service

type
Controller* = ref object of RootObj
Expand All @@ -12,20 +13,23 @@ type
networkService: network_service.Service
tokenService: token_service.Service
currencyService: currency_service.Service
collectibleService: collectible_service.Service

proc newController*(
delegate: io_interface.AccessInterface,
walletAccountService: wallet_account_service.Service,
networkService: network_service.Service,
tokenService: token_service.Service,
currencyService: currency_service.Service,
collectibleService: collectible_service.Service,
): Controller =
result = Controller()
result.delegate = delegate
result.walletAccountService = walletAccountService
result.networkService = networkService
result.tokenService = tokenService
result.currencyService = currencyService
result.collectibleService = collectibleService

proc delete*(self: Controller) =
discard
Expand Down Expand Up @@ -58,4 +62,4 @@ proc getAllMigratedKeyPairs*(self: Controller): seq[KeyPairDto] =
return self.walletAccountService.getAllMigratedKeyPairs()

proc getHasCollectiblesCache*(self: Controller, address: string): bool =
return self.walletAccountService.getHasCollectiblesCache(address)
return self.collectibleService.areCollectionsLoaded(address)
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import ../../../../../app_service/service/wallet_account/service as wallet_accou
import ../../../../../app_service/service/network/service as network_service
import ../../../../../app_service/service/node/service as node_service
import ../../../../../app_service/service/network_connection/service as network_connection
import ../../../../../app_service/service/collectible/service as collectible_service
import ../../../shared_models/currency_amount_utils
import ../../../shared_models/currency_amount
import ../../../shared_models/token_model as token_model
Expand Down Expand Up @@ -43,13 +44,14 @@ proc newModule*(
networkService: network_service.Service,
tokenService: token_service.Service,
currencyService: currency_service.Service,
collectibleService: collectible_service.Service,
): Module =
result = Module()
result.delegate = delegate
result.events = events
result.currentAccountIndex = 0
result.view = newView(result)
result.controller = newController(result, walletAccountService, networkService, tokenService, currencyService)
result.controller = newController(result, walletAccountService, networkService, tokenService, currencyService, collectibleService)
result.moduleLoaded = false

method delete*(self: Module) =
Expand Down
2 changes: 1 addition & 1 deletion src/app/modules/main/wallet_section/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ proc newModule*(
result.accountsModule = accounts_module.newModule(result, events, walletAccountService, networkService, currencyService)
result.allTokensModule = all_tokens_module.newModule(result, events, tokenService, walletAccountService)
result.collectiblesModule = collectibles_module.newModule(result, events, collectibleService, walletAccountService, networkService, nodeService, networkConnectionService)
result.currentAccountModule = current_account_module.newModule(result, events, walletAccountService, networkService, tokenService, currencyService)
result.currentAccountModule = current_account_module.newModule(result, events, walletAccountService, networkService, tokenService, currencyService, collectibleService)
result.transactionsModule = transactions_module.newModule(result, events, transactionService, walletAccountService, networkService, currencyService)
result.savedAddressesModule = saved_addresses_module.newModule(result, events, savedAddressService)
result.buySellCryptoModule = buy_sell_crypto_module.newModule(result, events, transactionService)
Expand Down
Loading

0 comments on commit bbf2600

Please sign in to comment.