Skip to content
This repository has been archived by the owner on May 10, 2024. It is now read-only.

Fix brave-browser #36266: Buy/Send/Swap button is not opening in Asset Details Screen. #8772

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 25 additions & 6 deletions Sources/BraveWallet/Crypto/Asset Details/AssetDetailView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,12 @@ struct AssetDetailView: View {

@ViewBuilder private var accountsBalanceView: some View {
VStack {
if assetDetailStore.accounts.isEmpty {
if assetDetailStore.nonZeroBalanceAccounts.isEmpty {
emptyAccountState
} else {
accountsBalanceHeader

ForEach(assetDetailStore.accounts) { viewModel in
ForEach(assetDetailStore.nonZeroBalanceAccounts) { viewModel in
accontBalanceRow(viewModel)
}
}
Expand Down Expand Up @@ -192,7 +192,7 @@ struct AssetDetailView: View {
kind: .buy,
initialToken: assetDetailStore.assetDetailToken
)
if assetDetailStore.accounts.isEmpty {
if assetDetailStore.allAccountsForTokenCoin.isEmpty {
onAccountCreationNeeded(destination)
} else {
buySendSwapDestination = destination
Expand All @@ -205,7 +205,7 @@ struct AssetDetailView: View {
kind: .send,
initialToken: assetDetailStore.assetDetailToken
)
if assetDetailStore.accounts.isEmpty {
if assetDetailStore.allAccountsForTokenCoin.isEmpty {
onAccountCreationNeeded(destination)
} else {
buySendSwapDestination = destination
Expand All @@ -218,7 +218,7 @@ struct AssetDetailView: View {
kind: .swap,
initialToken: assetDetailStore.assetDetailToken
)
if assetDetailStore.accounts.isEmpty {
if assetDetailStore.allAccountsForTokenCoin.isEmpty {
onAccountCreationNeeded(destination)
} else {
buySendSwapDestination = destination
Expand Down Expand Up @@ -274,7 +274,7 @@ struct AssetDetailView: View {
tokenContentContainer
.padding(.bottom, 12)

if (selectedContent == .accounts && !assetDetailStore.accounts.isEmpty) || (selectedContent == .transactions && !assetDetailStore.transactionSections.isEmpty) {
if (selectedContent == .accounts && !assetDetailStore.nonZeroBalanceAccounts.isEmpty) || (selectedContent == .transactions && !assetDetailStore.transactionSections.isEmpty) {
Text(Strings.Wallet.coinGeckoDisclaimer)
.multilineTextAlignment(.center)
.font(.footnote)
Expand Down Expand Up @@ -446,6 +446,25 @@ struct AssetDetailView: View {
isShowingAuroraBridgeAlert = false
}
}
.addAccount(
keyringStore: keyringStore,
networkStore: networkStore,
accountNetwork: networkStore.network(for: assetDetailStore.assetDetailToken),
isShowingConfirmation: $isPresentingAddAccountConfirmation,
isShowingAddAccount: $isPresentingAddAccount,
onConfirmAddAccount: { isPresentingAddAccount = true },
onCancelAddAccount: nil,
onAddAccountDismissed: {
Task { @MainActor in
if await assetDetailStore.handleDismissAddAccount() {
if let savedBSSDestination {
buySendSwapDestination = savedBSSDestination
self.savedBSSDestination = nil
}
}
}
}
)
}

private func onAccountCreationNeeded(_ destination: BuySendSwapDestination) {
Expand Down
14 changes: 9 additions & 5 deletions Sources/BraveWallet/Crypto/Stores/AssetDetailStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ class AssetDetailStore: ObservableObject, WalletObserverStore {
}
}
@Published private(set) var isLoadingAccountBalances: Bool = false
@Published private(set) var accounts: [AccountAssetViewModel] = []
@Published private(set) var nonZeroBalanceAccounts: [AccountAssetViewModel] = []
@Published private(set) var transactionSections: [TransactionSection] = []
@Published private(set) var isBuySupported: Bool = false
@Published private(set) var isSendSupported: Bool = false
Expand All @@ -67,7 +67,7 @@ class AssetDetailStore: ObservableObject, WalletObserverStore {
let currencyFormatter: NumberFormatter = .usdCurrencyFormatter

var totalBalance: Double {
accounts
nonZeroBalanceAccounts
.compactMap { Double($0.balance) }
.reduce(0, +)
}
Expand Down Expand Up @@ -120,6 +120,9 @@ class AssetDetailStore: ObservableObject, WalletObserverStore {
keyringServiceObserver != nil && txServiceObserver != nil && walletServiceObserver != nil
}

// All account info that has the same coin type as this asset's
var allAccountsForTokenCoin: [BraveWallet.AccountInfo] = []

init(
assetRatioService: BraveWalletAssetRatioService,
keyringService: BraveWalletKeyringService,
Expand Down Expand Up @@ -207,7 +210,7 @@ class AssetDetailStore: ObservableObject, WalletObserverStore {
self.isSwapSupported = await swapService.isSwapSupported(token.chainId)

// fetch accounts
let allAccountsForTokenCoin = await keyringService.allAccounts().accounts.filter { $0.coin == token.coin }
self.allAccountsForTokenCoin = await keyringService.allAccounts().accounts.filter { $0.coin == token.coin }
var updatedAccounts = allAccountsForTokenCoin.map {
AccountAssetViewModel(account: $0, decimalBalance: 0.0, balance: "", fiatBalance: "")
}
Expand All @@ -233,7 +236,7 @@ class AssetDetailStore: ObservableObject, WalletObserverStore {
}

// fetch accounts balance
self.accounts = await fetchAccountBalances(updatedAccounts, network: network)
self.nonZeroBalanceAccounts = await fetchAccountBalances(updatedAccounts, network: network)

// fetch transactions
let userAssets = assetManager.getAllUserAssetsInNetworkAssets(networks: [network], includingUserDeleted: true).flatMap { $0.tokens }
Expand Down Expand Up @@ -315,7 +318,8 @@ class AssetDetailStore: ObservableObject, WalletObserverStore {
// below is all not supported from Market tab
self.isSendSupported = false
self.isSwapSupported = false
self.accounts = []
self.allAccountsForTokenCoin = []
self.nonZeroBalanceAccounts = []
self.transactionSections = []
}
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/BraveWalletTests/AssetDetailStoreTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class AssetDetailStoreTests: XCTestCase {
XCTAssertEqual($0, "1%")
}
.store(in: &cancellables)
store.$accounts
store.$nonZeroBalanceAccounts
.dropFirst()
.sink { accounts in
defer { assetDetailException.fulfill() }
Expand Down Expand Up @@ -368,7 +368,7 @@ class AssetDetailStoreTests: XCTestCase {
.sink { values in
defer {
XCTAssertNil(store.network)
XCTAssertTrue(store.accounts.isEmpty)
XCTAssertTrue(store.nonZeroBalanceAccounts.isEmpty)
XCTAssertTrue(store.transactionSections.isEmpty)

assetDetailBitcoinException.fulfill()
Expand Down Expand Up @@ -450,7 +450,7 @@ class AssetDetailStoreTests: XCTestCase {
.sink {
defer {
XCTAssertNil(store.network)
XCTAssertTrue(store.accounts.isEmpty)
XCTAssertTrue(store.nonZeroBalanceAccounts.isEmpty)
XCTAssertTrue(store.transactionSections.isEmpty)

assetDetailNonBitcoinException.fulfill()
Expand Down Expand Up @@ -484,7 +484,7 @@ class AssetDetailStoreTests: XCTestCase {
.sink { values in
defer {
XCTAssertNil(store.network)
XCTAssertTrue(store.accounts.isEmpty)
XCTAssertTrue(store.nonZeroBalanceAccounts.isEmpty)
XCTAssertTrue(store.transactionSections.isEmpty)

assetDetailNonBitcoinException.fulfill()
Expand Down
Loading