Skip to content

Commit

Permalink
[Wallet/iOS] Fix #36266: Buy/Send/Swap buttons not opening from Asset…
Browse files Browse the repository at this point in the history
… Details (uplift to 1.64.x) (#22247)

Uplift of #22229 (squashed) to beta
  • Loading branch information
brave-builds committed Feb 26, 2024
1 parent 865fcaa commit 5b071ee
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
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
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 @@ -119,6 +119,9 @@ class AssetDetailStore: ObservableObject, WalletObserverStore {
var isObserving: Bool {
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,
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
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

0 comments on commit 5b071ee

Please sign in to comment.