chore(@desktop/wallet): Added subscriber/callback mechanism in order to forward events from the service to the model

fixes #16313
This commit is contained in:
Khushboo Mehta 2024-09-26 12:12:13 +02:00
parent fed6f79a66
commit 93efa1539f
7 changed files with 119 additions and 91 deletions

View File

@ -40,6 +40,13 @@ QtObject:
marketValuesDelegate: io_interface.TokenMarketValuesDataSource
tokenMarketDetails: seq[MarketDetailsItem]
# Forward declaration
proc modelUpdatedCallback(self: FlatTokensModel): proc()
proc tokenDetailsUpdatedCallback(self: FlatTokensModel): proc()
proc tokenMarketValuesUpdatedCallback(self: FlatTokensModel): proc()
proc tokenPreferencesUpdatedCallback(self: FlatTokensModel): proc()
proc currencyFormatsUpdatedCallback(self: FlatTokensModel): proc()
proc setup(self: FlatTokensModel) =
self.QAbstractListModel.setup
@ -56,6 +63,11 @@ QtObject:
result.delegate = delegate
result.marketValuesDelegate = marketValuesDelegate
result.tokenMarketDetails = @[]
result.delegate.subscribeToModelUpdates(result.modelUpdatedCallback())
result.delegate.subscribeToTokenDetailsUpdates(result.tokenDetailsUpdatedCallback())
result.delegate.subscribeToTokenMarketValuesUpdates(result.tokenMarketValuesUpdatedCallback())
result.delegate.subscribeToTokenPreferencesUpdates(result.tokenPreferencesUpdatedCallback())
result.delegate.subscribeToCurrencyFormatsUpdates(result.currencyFormatsUpdatedCallback())
method rowCount(self: FlatTokensModel, index: QModelIndex = nil): int =
return self.delegate.getFlatTokensList().len
@ -138,7 +150,7 @@ QtObject:
of ModelRole.Position:
result = newQVariant(self.delegate.getTokenPreferences(item.symbol).position)
proc modelsUpdated*(self: FlatTokensModel) =
proc modelsUpdated(self: FlatTokensModel) =
self.beginResetModel()
self.tokenMarketDetails = @[]
for token in self.delegate.getFlatTokensList():
@ -155,12 +167,6 @@ QtObject:
defer: lastindex.delete
self.dataChanged(index, lastindex, @[ModelRole.MarketDetails.int, ModelRole.MarketDetailsLoading.int])
proc tokensMarketValuesUpdated*(self: FlatTokensModel) =
self.marketDetailsDataChanged()
proc tokensMarketValuesAboutToUpdate*(self: FlatTokensModel) =
self.marketDetailsDataChanged()
proc detailsDataChanged(self: FlatTokensModel) =
if self.delegate.getFlatTokensList().len > 0:
let index = self.createIndex(0, 0, nil)
@ -169,20 +175,29 @@ QtObject:
defer: lastindex.delete
self.dataChanged(index, lastindex, @[ModelRole.Description.int, ModelRole.WebsiteUrl.int, ModelRole.DetailsLoading.int])
proc tokensDetailsAboutToUpdate*(self: FlatTokensModel) =
self.detailsDataChanged()
proc tokensDetailsUpdated*(self: FlatTokensModel) =
self.detailsDataChanged()
proc currencyFormatsUpdated*(self: FlatTokensModel) =
proc currencyFormatsUpdated(self: FlatTokensModel) =
for marketDetails in self.tokenMarketDetails:
marketDetails.updateCurrencyFormat()
proc tokenPreferencesUpdated*(self: FlatTokensModel) =
proc tokenPreferencesUpdated(self: FlatTokensModel) =
if self.delegate.getFlatTokensList().len > 0:
let index = self.createIndex(0, 0, nil)
let lastindex = self.createIndex(self.delegate.getFlatTokensList().len-1, 0, nil)
defer: index.delete
defer: lastindex.delete
self.dataChanged(index, lastindex, @[ModelRole.Visible.int, ModelRole.Position.int])
proc modelUpdatedCallback(self: FlatTokensModel): proc() =
return proc() = self.modelsUpdated()
proc tokenDetailsUpdatedCallback(self: FlatTokensModel): proc() =
return proc() = self.detailsDataChanged()
proc tokenMarketValuesUpdatedCallback(self: FlatTokensModel): proc() =
return proc() = self.marketDetailsDataChanged()
proc tokenPreferencesUpdatedCallback(self: FlatTokensModel): proc() =
return proc() = self.tokenPreferencesUpdated()
proc currencyFormatsUpdatedCallback(self: FlatTokensModel): proc() =
return proc() = self.currencyFormatsUpdated()

View File

@ -4,7 +4,8 @@ import app/modules/shared_models/currency_amount
type
SourcesOfTokensModelDataSource* = tuple[
getSourcesOfTokensList: proc(): var seq[SupportedSourcesItem]
getSourcesOfTokensList: proc(): var seq[SupportedSourcesItem],
subscribeToModelUpdates: proc(callback: proc())
]
type
FlatTokenModelDataSource* = tuple[
@ -14,6 +15,11 @@ type
getCommunityTokenDescription: proc(chainId: int, address: string): string,
getTokensDetailsLoading: proc(): bool,
getTokensMarketValuesLoading: proc(): bool,
subscribeToModelUpdates: proc(callback: proc()),
subscribeToTokenDetailsUpdates: proc(callback: proc()),
subscribeToTokenMarketValuesUpdates: proc(callback: proc()),
subscribeToTokenPreferencesUpdates: proc(callback: proc()),
subscribeToCurrencyFormatsUpdates: proc(callback: proc())
]
type
TokenBySymbolModelDataSource* = tuple[
@ -23,6 +29,11 @@ type
getCommunityTokenDescription: proc(addressPerChain: seq[AddressPerChain]): string,
getTokensDetailsLoading: proc(): bool,
getTokensMarketValuesLoading: proc(): bool,
subscribeToModelUpdates: proc(callback: proc()),
subscribeToTokenDetailsUpdates: proc(callback: proc()),
subscribeToTokenMarketValuesUpdates: proc(callback: proc()),
subscribeToTokenPreferencesUpdates: proc(callback: proc()),
subscribeToCurrencyFormatsUpdates: proc(callback: proc())
]
type
TokenMarketValuesDataSource* = tuple[

View File

@ -24,6 +24,11 @@ type
controller: Controller
moduleLoaded: bool
addresses: seq[string]
modelUpdatedSubscribers: seq[proc()]
tokensDetailsUpdatedSubscribers: seq[proc()]
tokensMarketValuesUpdatedSubscribers: seq[proc()]
tokenPreferencesUpdatedSubscribers: seq[proc()]
currencyFormatsUpdatedSubscribers: seq[proc()]
proc newModule*(
delegate: delegate_interface.AccessInterface,
@ -55,27 +60,28 @@ method load*(self: Module) =
# Passing on the events for changes in model to abstract model
self.events.on(SIGNAL_TOKENS_LIST_UPDATED) do(e: Args):
self.view.modelsUpdated()
self.view.setTokenListUpdatedAt(self.controller.getTokenListUpdatedAt())
self.events.on(SIGNAL_TOKENS_DETAILS_ABOUT_TO_BE_UPDATED) do(e: Args):
self.view.tokensDetailsAboutToUpdate()
for modelUpdatedSubscriber in self.modelUpdatedSubscribers:
modelUpdatedSubscriber()
self.events.on(SIGNAL_TOKENS_DETAILS_UPDATED) do(e: Args):
self.view.tokensDetailsUpdated()
self.events.on(SIGNAL_TOKENS_MARKET_VALUES_ABOUT_TO_BE_UPDATED) do(e: Args):
self.view.tokensMarketValuesAboutToUpdate()
for tokensDetailsUpdatedSubscriber in self.tokensDetailsUpdatedSubscribers:
tokensDetailsUpdatedSubscriber()
self.events.on(SIGNAL_TOKENS_MARKET_VALUES_UPDATED) do(e: Args):
self.view.tokensMarketValuesUpdated()
self.events.on(SIGNAL_TOKENS_PRICES_ABOUT_TO_BE_UPDATED) do(e: Args):
self.view.tokensMarketValuesAboutToUpdate()
for tokensMarketValuesUpdatedSubscriber in self.tokensMarketValuesUpdatedSubscribers:
tokensMarketValuesUpdatedSubscriber()
self.events.on(SIGNAL_TOKENS_PRICES_UPDATED) do(e: Args):
self.view.tokensMarketValuesUpdated()
for tokensMarketValuesUpdatedSubscriber in self.tokensMarketValuesUpdatedSubscribers:
tokensMarketValuesUpdatedSubscriber()
self.events.on(SIGNAL_TOKEN_PREFERENCES_UPDATED) do(e: Args):
self.view.tokenPreferencesUpdated()
for tokenPreferencesUpdatedSubscriber in self.tokenPreferencesUpdatedSubscribers:
tokenPreferencesUpdatedSubscriber()
self.events.on(SIGNAL_COMMUNITY_TOKENS_DETAILS_LOADED) do(e: Args):
self.view.tokensDetailsUpdated()
for tokensDetailsUpdatedSubscriber in self.tokensDetailsUpdatedSubscribers:
tokensDetailsUpdatedSubscriber()
self.events.on(SIGNAL_CURRENCY_FORMATS_UPDATED) do(e:Args):
self.view.currencyFormatsUpdated()
for currencyFormatsUpdatedSubscriber in self.currencyFormatsUpdatedSubscribers:
currencyFormatsUpdatedSubscriber()
self.controller.init()
self.view.load()
@ -108,7 +114,8 @@ method tokenBalanceHistoryDataResolved*(self: Module, balanceHistoryJson: string
method getSourcesOfTokensModelDataSource*(self: Module): SourcesOfTokensModelDataSource =
return (
getSourcesOfTokensList: proc(): var seq[SupportedSourcesItem] = self.controller.getSourcesOfTokensList()
getSourcesOfTokensList: proc(): var seq[SupportedSourcesItem] = self.controller.getSourcesOfTokensList(),
subscribeToModelUpdates: proc(callback: proc()) = self.modelUpdatedSubscribers.add(callback)
)
method getFlatTokenModelDataSource*(self: Module): FlatTokenModelDataSource =
@ -118,7 +125,12 @@ method getFlatTokenModelDataSource*(self: Module): FlatTokenModelDataSource =
getTokenPreferences: proc(symbol: string): TokenPreferencesItem = self.controller.getTokenPreferences(symbol),
getCommunityTokenDescription: proc(chainId: int, address: string): string = self.controller.getCommunityTokenDescription(chainId, address),
getTokensDetailsLoading: proc(): bool = self.controller.getTokensDetailsLoading(),
getTokensMarketValuesLoading: proc(): bool = self.controller.getTokensMarketValuesLoading()
getTokensMarketValuesLoading: proc(): bool = self.controller.getTokensMarketValuesLoading(),
subscribeToModelUpdates: proc(callback: proc()) = self.modelUpdatedSubscribers.add(callback),
subscribeToTokenDetailsUpdates: proc(callback: proc()) = self.tokensDetailsUpdatedSubscribers.add(callback),
subscribeToTokenMarketValuesUpdates: proc(callback: proc()) = self.tokensMarketValuesUpdatedSubscribers.add(callback),
subscribeToTokenPreferencesUpdates: proc(callback: proc()) = self.tokenPreferencesUpdatedSubscribers.add(callback),
subscribeToCurrencyFormatsUpdates: proc(callback: proc()) = self.currencyFormatsUpdatedSubscribers.add(callback)
)
method getTokenBySymbolModelDataSource*(self: Module): TokenBySymbolModelDataSource =
@ -128,7 +140,12 @@ method getTokenBySymbolModelDataSource*(self: Module): TokenBySymbolModelDataSou
getTokenPreferences: proc(symbol: string): TokenPreferencesItem = self.controller.getTokenPreferences(symbol),
getCommunityTokenDescription: proc(addressPerChain: seq[AddressPerChain]): string = self.controller.getCommunityTokenDescription(addressPerChain),
getTokensDetailsLoading: proc(): bool = self.controller.getTokensDetailsLoading(),
getTokensMarketValuesLoading: proc(): bool = self.controller.getTokensMarketValuesLoading()
getTokensMarketValuesLoading: proc(): bool = self.controller.getTokensMarketValuesLoading(),
subscribeToModelUpdates: proc(callback: proc()) = self.modelUpdatedSubscribers.add(callback),
subscribeToTokenDetailsUpdates: proc(callback: proc()) = self.tokensDetailsUpdatedSubscribers.add(callback),
subscribeToTokenMarketValuesUpdates: proc(callback: proc()) = self.tokensMarketValuesUpdatedSubscribers.add(callback),
subscribeToTokenPreferencesUpdates: proc(callback: proc()) = self.tokenPreferencesUpdatedSubscribers.add(callback),
subscribeToCurrencyFormatsUpdates: proc(callback: proc()) = self.currencyFormatsUpdatedSubscribers.add(callback)
)
method getTokenMarketValuesDataSource*(self: Module): TokenMarketValuesDataSource =

View File

@ -17,6 +17,9 @@ QtObject:
type SourcesOfTokensModel* = ref object of QAbstractListModel
delegate: io_interface.SourcesOfTokensModelDataSource
# Forward declaration
proc modelUpdatedCallback(self: SourcesOfTokensModel): proc()
proc setup(self: SourcesOfTokensModel) =
self.QAbstractListModel.setup
@ -27,6 +30,7 @@ QtObject:
new(result, delete)
result.setup
result.delegate = delegate
result.delegate.subscribeToModelUpdates(result.modelUpdatedCallback())
method rowCount(self: SourcesOfTokensModel, index: QModelIndex = nil): int =
return self.delegate.getSourcesOfTokensList().len
@ -69,6 +73,9 @@ QtObject:
of ModelRole.TokensCount:
result = newQVariant(item.tokensCount)
proc modelsUpdated*(self: SourcesOfTokensModel) =
proc modelsUpdated(self: SourcesOfTokensModel) =
self.beginResetModel()
self.endResetModel()
proc modelUpdatedCallback(self: SourcesOfTokensModel): proc() =
return proc() = self.modelsUpdated()

View File

@ -40,6 +40,13 @@ QtObject:
addressPerChainModel: seq[AddressPerChainModel]
tokenMarketDetails: seq[MarketDetailsItem]
# Forward declaration
proc modelUpdatedCallback(self: TokensBySymbolModel): proc()
proc tokenDetailsUpdatedCallback(self: TokensBySymbolModel): proc()
proc tokenMarketValuesUpdatedCallback(self: TokensBySymbolModel): proc()
proc tokenPreferencesUpdatedCallback(self: TokensBySymbolModel): proc()
proc currencyFormatsUpdatedCallback(self: TokensBySymbolModel): proc()
proc setup(self: TokensBySymbolModel) =
self.QAbstractListModel.setup
self.addressPerChainModel = @[]
@ -59,6 +66,11 @@ QtObject:
result.delegate = delegate
result.marketValuesDelegate = marketValuesDelegate
result.tokenMarketDetails = @[]
result.delegate.subscribeToModelUpdates(result.modelUpdatedCallback())
result.delegate.subscribeToTokenDetailsUpdates(result.tokenDetailsUpdatedCallback())
result.delegate.subscribeToTokenMarketValuesUpdates(result.tokenMarketValuesUpdatedCallback())
result.delegate.subscribeToTokenPreferencesUpdates(result.tokenPreferencesUpdatedCallback())
result.delegate.subscribeToCurrencyFormatsUpdates(result.currencyFormatsUpdatedCallback())
method rowCount(self: TokensBySymbolModel, index: QModelIndex = nil): int =
return self.delegate.getTokenBySymbolList().len
@ -139,7 +151,7 @@ QtObject:
of ModelRole.Position:
result = newQVariant(self.delegate.getTokenPreferences(item.symbol).position)
proc modelsUpdated*(self: TokensBySymbolModel) =
proc modelsUpdated(self: TokensBySymbolModel) =
self.beginResetModel()
self.tokenMarketDetails = @[]
self.addressPerChainModel = @[]
@ -151,7 +163,7 @@ QtObject:
self.tokenMarketDetails.add(newMarketDetailsItem(self.marketValuesDelegate, symbol))
self.endResetModel()
proc tokensMarketValuesUpdated*(self: TokensBySymbolModel) =
proc tokensMarketValuesUpdated(self: TokensBySymbolModel) =
if not self.delegate.getTokensMarketValuesLoading():
if self.delegate.getTokenBySymbolList().len > 0:
let index = self.createIndex(0, 0, nil)
@ -160,15 +172,7 @@ QtObject:
defer: lastindex.delete
self.dataChanged(index, lastindex, @[ModelRole.MarketDetails.int, ModelRole.MarketDetailsLoading.int])
proc tokensMarketValuesAboutToUpdate*(self: TokensBySymbolModel) =
if self.delegate.getTokenBySymbolList().len > 0:
let index = self.createIndex(0, 0, nil)
let lastindex = self.createIndex(self.delegate.getTokenBySymbolList().len-1, 0, nil)
defer: index.delete
defer: lastindex.delete
self.dataChanged(index, lastindex, @[ModelRole.MarketDetails.int, ModelRole.MarketDetailsLoading.int])
proc tokensDetailsAboutToUpdate*(self: TokensBySymbolModel) =
proc tokensDetailsUpdated(self: TokensBySymbolModel) =
if self.delegate.getTokenBySymbolList().len > 0:
let index = self.createIndex(0, 0, nil)
let lastindex = self.createIndex(self.delegate.getTokenBySymbolList().len-1, 0, nil)
@ -176,22 +180,29 @@ QtObject:
defer: lastindex.delete
self.dataChanged(index, lastindex, @[ModelRole.Description.int, ModelRole.WebsiteUrl.int, ModelRole.DetailsLoading.int])
proc tokensDetailsUpdated*(self: TokensBySymbolModel) =
if self.delegate.getTokenBySymbolList().len > 0:
let index = self.createIndex(0, 0, nil)
let lastindex = self.createIndex(self.delegate.getTokenBySymbolList().len-1, 0, nil)
defer: index.delete
defer: lastindex.delete
self.dataChanged(index, lastindex, @[ModelRole.Description.int, ModelRole.WebsiteUrl.int, ModelRole.DetailsLoading.int])
proc currencyFormatsUpdated*(self: TokensBySymbolModel) =
proc currencyFormatsUpdated(self: TokensBySymbolModel) =
for marketDetails in self.tokenMarketDetails:
marketDetails.updateCurrencyFormat()
proc tokenPreferencesUpdated*(self: TokensBySymbolModel) =
proc tokenPreferencesUpdated(self: TokensBySymbolModel) =
if self.delegate.getTokenBySymbolList().len > 0:
let index = self.createIndex(0, 0, nil)
let lastindex = self.createIndex(self.delegate.getTokenBySymbolList().len-1, 0, nil)
defer: index.delete
defer: lastindex.delete
self.dataChanged(index, lastindex, @[ModelRole.Visible.int, ModelRole.Position.int])
proc modelUpdatedCallback(self: TokensBySymbolModel): proc() =
return proc() = self.modelsUpdated()
proc tokenDetailsUpdatedCallback(self: TokensBySymbolModel): proc() =
return proc() = self.tokensDetailsUpdated()
proc tokenMarketValuesUpdatedCallback(self: TokensBySymbolModel): proc() =
return proc() = self.tokensMarketValuesUpdated()
proc tokenPreferencesUpdatedCallback(self: TokensBySymbolModel): proc() =
return proc() = self.tokenPreferencesUpdated()
proc currencyFormatsUpdatedCallback(self: TokensBySymbolModel): proc() =
return proc() = self.currencyFormatsUpdated()

View File

@ -30,7 +30,8 @@ QtObject:
result.delegate = delegate
result.marketHistoryIsLoading = false
result.balanceHistoryIsLoading = false
result.sourcesOfTokensModel = newSourcesOfTokensModel(delegate.getSourcesOfTokensModelDataSource())
result.sourcesOfTokensModel = newSourcesOfTokensModel(
delegate.getSourcesOfTokensModelDataSource())
result.flatTokensModel = newFlatTokensModel(
delegate.getFlatTokenModelDataSource(),
delegate.getTokenMarketValuesDataSource())
@ -116,35 +117,6 @@ QtObject:
read = getTokensBySymbolModel
notify = tokensBySymbolModelChanged
proc modelsUpdated*(self: View) =
self.sourcesOfTokensModel.modelsUpdated()
self.flatTokensModel.modelsUpdated()
self.tokensBySymbolModel.modelsUpdated()
proc tokensMarketValuesUpdated*(self: View) =
self.flatTokensModel.tokensMarketValuesUpdated()
self.tokensBySymbolModel.tokensMarketValuesUpdated()
proc tokensMarketValuesAboutToUpdate*(self: View) =
self.flatTokensModel.tokensMarketValuesAboutToUpdate()
self.tokensBySymbolModel.tokensMarketValuesAboutToUpdate()
proc tokensDetailsAboutToUpdate*(self: View) =
self.flatTokensModel.tokensDetailsAboutToUpdate()
self.tokensBySymbolModel.tokensDetailsAboutToUpdate()
proc tokensDetailsUpdated*(self: View) =
self.flatTokensModel.tokensDetailsUpdated()
self.tokensBySymbolModel.tokensDetailsUpdated()
proc currencyFormatsUpdated*(self: View) =
self.flatTokensModel.currencyFormatsUpdated()
self.tokensBySymbolModel.currencyFormatsUpdated()
proc tokenPreferencesUpdated*(self: View) =
self.flatTokensModel.tokenPreferencesUpdated()
self.tokensBySymbolModel.tokenPreferencesUpdated()
proc updateTokenPreferences*(self: View, tokenPreferencesJson: string) {.slot.} =
self.delegate.updateTokenPreferences(tokenPreferencesJson)

View File

@ -31,10 +31,7 @@ const CRYPTO_SUB_UNITS_TO_FACTOR = {
# Signals which may be emitted by this service:
const SIGNAL_TOKEN_HISTORICAL_DATA_LOADED* = "tokenHistoricalDataLoaded"
const SIGNAL_TOKENS_LIST_UPDATED* = "tokensListUpdated"
const SIGNAL_TOKENS_DETAILS_ABOUT_TO_BE_UPDATED* = "tokensDetailsAboutToBeUpdated"
const SIGNAL_TOKENS_DETAILS_UPDATED* = "tokensDetailsUpdated"
const SIGNAL_TOKENS_MARKET_VALUES_ABOUT_TO_BE_UPDATED* = "tokensMarketValuesAboutToBeUpdated"
const SIGNAL_TOKENS_PRICES_ABOUT_TO_BE_UPDATED* = "tokensPricesValuesAboutToBeUpdated"
const SIGNAL_TOKENS_MARKET_VALUES_UPDATED* = "tokensMarketValuesUpdated"
const SIGNAL_TOKENS_PRICES_UPDATED* = "tokensPricesValuesUpdated"
const SIGNAL_TOKEN_PREFERENCES_UPDATED* = "tokenPreferencesUpdated"
@ -104,7 +101,6 @@ QtObject:
proc fetchTokensMarketValues(self: Service, symbols: seq[string]) =
self.tokensMarketDetailsLoading = true
defer: self.events.emit(SIGNAL_TOKENS_MARKET_VALUES_ABOUT_TO_BE_UPDATED, Args())
let arg = FetchTokensMarketValuesTaskArg(
tptr: fetchTokensMarketValuesTask,
vptr: cast[ByteAddress](self.vptr),
@ -187,7 +183,6 @@ QtObject:
proc fetchTokensPrices(self: Service, symbols: seq[string]) =
self.tokensPricesLoading = true
defer: self.events.emit(SIGNAL_TOKENS_PRICES_ABOUT_TO_BE_UPDATED, Args())
let arg = FetchTokensPricesTaskArg(
tptr: fetchTokensPricesTask,
vptr: cast[ByteAddress](self.vptr),