Skip to content

Commit

Permalink
implement WalletUserAssetDataObserver to the stores that need to upda…
Browse files Browse the repository at this point in the history
…te after cached balances are refreshed.
  • Loading branch information
nuo-xu committed Jun 11, 2024
1 parent a2f15ba commit 9113e22
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ class AccountActivityStore: ObservableObject, WalletObserverStore {

extension AccountActivityStore: WalletUserAssetDataObserver {
public func cachedBalanceRefreshed() {
update()
}

public func userAssetUpdated() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class AccountsStore: ObservableObject, WalletObserverStore {

func setupObservers() {
guard !isObserving else { return }
self.userAssetManager.addUserAssetDataObserver(self)
self.keyringServiceObserver = KeyringServiceObserver(
keyringService: keyringService,
_accountsChanged: { [weak self] in
Expand Down Expand Up @@ -326,3 +327,12 @@ class AccountsStore: ObservableObject, WalletObserverStore {
tokenBalancesCache[account.id]?[tokenId]
}
}

extension AccountsStore: WalletUserAssetDataObserver {
func cachedBalanceRefreshed() {
update()
}

func userAssetUpdated() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ class AssetDetailStore: ObservableObject, WalletObserverStore {

func setupObservers() {
guard !isObserving else { return }
self.assetManager.addUserAssetDataObserver(self)
self.keyringServiceObserver = KeyringServiceObserver(
keyringService: keyringService,
_accountsChanged: { [weak self] in
Expand Down Expand Up @@ -697,3 +698,12 @@ extension AssetDetailStore: BraveWalletBraveWalletServiceObserver {
func onResetWallet() {
}
}

extension AssetDetailStore: WalletUserAssetDataObserver {
func cachedBalanceRefreshed() {
update()
}

func userAssetUpdated() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class NFTDetailStore: ObservableObject, WalletObserverStore {
}

func setupObservers() {
guard !isObserving else { return }
self.assetManager.addUserAssetDataObserver(self)
self.txServiceObserver = TxServiceObserver(
txService: txService,
_onTransactionStatusChanged: { [weak self] txInfo in
Expand All @@ -142,9 +144,7 @@ class NFTDetailStore: ObservableObject, WalletObserverStore {
networkInfo = network
}

if owner == nil {
updateOwner()
}
updateOwner()

if nftMetadata == nil {
isLoading = true
Expand Down Expand Up @@ -224,3 +224,12 @@ class NFTDetailStore: ObservableObject, WalletObserverStore {
}
}
}

extension NFTDetailStore: WalletUserAssetDataObserver {
func cachedBalanceRefreshed() {
update()
}

func userAssetUpdated() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ public class SendTokenStore: ObservableObject, WalletObserverStore {

func setupObservers() {
guard !isObserving else { return }
self.assetManager.addUserAssetDataObserver(self)
self.keyringServiceObserver = KeyringServiceObserver(
keyringService: keyringService,
_selectedWalletAccountChanged: { [weak self] _ in
Expand Down Expand Up @@ -986,3 +987,12 @@ public class SendTokenStore: ObservableObject, WalletObserverStore {
return await rpcService.fetchNFTMetadata(tokens: tokens, ipfsApi: ipfsApi)
}
}

extension SendTokenStore: WalletUserAssetDataObserver {
public func cachedBalanceRefreshed() {
update()
}

public func userAssetUpdated() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore {

func setupObservers() {
guard !isObserving else { return }
self.assetManager.addUserAssetDataObserver(self)
self.keyringServiceObserver = KeyringServiceObserver(
keyringService: keyringService,
_selectedWalletAccountChanged: { [weak self] account in
Expand Down Expand Up @@ -1323,3 +1324,16 @@ public class SwapTokenStore: ObservableObject, WalletObserverStore {
}
#endif
}

extension SwapTokenStore: WalletUserAssetDataObserver {
public func cachedBalanceRefreshed() {
if let token = selectedFromToken {
fetchTokenBalance(for: token) { [weak self] balance in
self?.selectedFromTokenBalance = balance
}
}
}

public func userAssetUpdated() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public class TransactionConfirmationStore: ObservableObject, WalletObserverStore

func setupObservers() {
guard !isObserving else { return }
self.assetManager.addUserAssetDataObserver(self)
self.txServiceObserver = TxServiceObserver(
txService: txService,
_onNewUnapprovedTx: { _ in
Expand Down Expand Up @@ -998,3 +999,16 @@ struct TransactionProviderError {
let code: Int
let message: String
}

extension TransactionConfirmationStore: WalletUserAssetDataObserver {
public func cachedBalanceRefreshed() {
updateTransaction(
with: activeTransaction,
shouldFetchCurrentAllowance: false,
shouldFetchGasTokenBalance: true
)
}

public func userAssetUpdated() {
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ public class WalletUserAssetManager: WalletUserAssetManagerType, WalletObserverS
keyringServiceObserver = nil
txServiceObserver = nil
walletServiceObserver = nil
dataObservers.removeAllObjects()
}

public func setupObservers() {
Expand Down

0 comments on commit 9113e22

Please sign in to comment.