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

Commit

Permalink
Fix #4677: Prefill default buy/send/swap token if user comes from tok…
Browse files Browse the repository at this point in the history
…en details screen (#4756)
  • Loading branch information
nuo-xu authored Jan 12, 2022
1 parent c45e0bc commit 8ae3f1c
Show file tree
Hide file tree
Showing 14 changed files with 258 additions and 73 deletions.
31 changes: 27 additions & 4 deletions BraveWallet/Crypto/Asset Details/AssetDetailHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,36 @@ struct AssetDetailHeaderView: View {
Divider()
.padding(.bottom)
HStack {
Button(action: { buySendSwapDestination = .buy }) {
Text(Strings.Wallet.buy)
if assetDetailStore.isBuySupported {
Button(
action: {
buySendSwapDestination = BuySendSwapDestination(
kind: .buy,
initialToken: assetDetailStore.token
)
}
) {
Text(Strings.Wallet.buy)
}
}
Button(action: { buySendSwapDestination = .send }) {
Button(
action: {
buySendSwapDestination = BuySendSwapDestination(
kind: .send,
initialToken: assetDetailStore.token
)
}
) {
Text(Strings.Wallet.send)
}
Button(action: { buySendSwapDestination = .swap }) {
Button(
action: {
buySendSwapDestination = BuySendSwapDestination(
kind: .swap,
initialToken: assetDetailStore.token
)
}
) {
Text(Strings.Wallet.swap)
}
}
Expand Down
59 changes: 32 additions & 27 deletions BraveWallet/Crypto/BuySendSwap/BuySendSwapDestination.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,43 @@
import Foundation
import struct Shared.Strings
import SwiftUI
import BraveCore

/// Used to determine where a user is navigated to when they tap on a buy, send or swap button
enum BuySendSwapDestination: String, Identifiable, CaseIterable {
case buy
case send
case swap

var id: String {
rawValue
}

var localizedTitle: String {
switch self {
case .buy:
return Strings.Wallet.buy
case .send:
return Strings.Wallet.send
case .swap:
return Strings.Wallet.swap
struct BuySendSwapDestination: Identifiable, Equatable, Hashable {
enum Kind: String, Identifiable, CaseIterable {
case buy, send, swap

var id: String {
rawValue
}
}

var localizedDescription: String {
switch self {
case .buy:
return Strings.Wallet.buyDescription
case .send:
return Strings.Wallet.sendDescription
case .swap:
return Strings.Wallet.swapDescription

var localizedTitle: String {
switch self {
case .buy:
return Strings.Wallet.buy
case .send:
return Strings.Wallet.send
case .swap:
return Strings.Wallet.swap
}
}

var localizedDescription: String {
switch self {
case .buy:
return Strings.Wallet.buyDescription
case .send:
return Strings.Wallet.sendDescription
case .swap:
return Strings.Wallet.swapDescription
}
}
}

var kind: Kind
var initialToken: BraveWallet.ERCToken?
var id: String { kind.id }
}

private struct BuySendSwapDestinationKey: EnvironmentKey {
Expand Down
14 changes: 8 additions & 6 deletions BraveWallet/Crypto/BuySendSwap/BuySendSwapView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,27 @@ import struct Shared.Strings

struct BuySendSwapView: View {
var action: (BuySendSwapDestination) -> Void

var destinations = [BuySendSwapDestination(kind: .buy),
BuySendSwapDestination(kind: .send),
BuySendSwapDestination(kind: .swap)]
var body: some View {
VStack(alignment: .leading, spacing: 16) {
ForEach(BuySendSwapDestination.allCases, id: \.self) { action in
Button(action: { self.action(action) }) {
ForEach(destinations, id: \.self) { destination in
Button(action: { self.action(destination) }) {
VStack(alignment: .leading, spacing: 3) {
Text(action.localizedTitle)
Text(destination.kind.localizedTitle)
.foregroundColor(Color(.bravePrimary))
.font(.headline)
.multilineTextAlignment(.leading)
Text(action.localizedDescription)
Text(destination.kind.localizedDescription)
.foregroundColor(Color(.braveLabel))
.font(.footnote)
.multilineTextAlignment(.leading)
}
.frame(maxWidth: .infinity, alignment: .leading)
.padding([.leading, .trailing], 20)
}
if action != BuySendSwapDestination.allCases.last {
if destination != destinations.last {
Divider()
.padding(.leading, 20)
}
Expand Down
8 changes: 4 additions & 4 deletions BraveWallet/Crypto/CryptoView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -111,24 +111,24 @@ private struct CryptoContainerView<DismissContent: ToolbarContent>: View {
.background(
Color.clear
.sheet(item: $cryptoStore.buySendSwapDestination) { action in
switch action {
switch action.kind {
case .buy:
BuyTokenView(
keyringStore: keyringStore,
networkStore: cryptoStore.networkStore,
buyTokenStore: cryptoStore.openBuyTokenStore()
buyTokenStore: cryptoStore.openBuyTokenStore(action.initialToken)
)
case .send:
SendTokenView(
keyringStore: keyringStore,
networkStore: cryptoStore.networkStore,
sendTokenStore: cryptoStore.openSendTokenStore()
sendTokenStore: cryptoStore.openSendTokenStore(action.initialToken)
)
case .swap:
SwapCryptoView(
keyringStore: keyringStore,
ethNetworkStore: cryptoStore.networkStore,
swapTokensStore: cryptoStore.openSwapTokenStore()
swapTokensStore: cryptoStore.openSwapTokenStore(action.initialToken)
)
}
}
Expand Down
8 changes: 8 additions & 0 deletions BraveWallet/Crypto/Stores/AssetDetailStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ class AssetDetailStore: ObservableObject {
@Published private(set) var isLoadingAccountBalances: Bool = false
@Published private(set) var accounts: [AccountAssetViewModel] = []
@Published private(set) var transactions: [BraveWallet.TransactionInfo] = []
@Published private(set) var isBuySupported: Bool = true

private(set) var assetPriceValue: Double = 0.0

private let assetRatioController: BraveWalletAssetRatioController
private let keyringController: BraveWalletKeyringController
private let rpcController: BraveWalletEthJsonRpcController
private let txController: BraveWalletEthTxController
private let tokenRegistry: BraveWalletERCTokenRegistry

let token: BraveWallet.ERCToken

Expand All @@ -52,12 +54,14 @@ class AssetDetailStore: ObservableObject {
keyringController: BraveWalletKeyringController,
rpcController: BraveWalletEthJsonRpcController,
txController: BraveWalletEthTxController,
tokenRegistry: BraveWalletERCTokenRegistry,
token: BraveWallet.ERCToken
) {
self.assetRatioController = assetRatioController
self.keyringController = keyringController
self.rpcController = rpcController
self.txController = txController
self.tokenRegistry = tokenRegistry
self.token = token

self.keyringController.add(self)
Expand All @@ -80,6 +84,10 @@ class AssetDetailStore: ObservableObject {
isLoadingPrice = true
isLoadingChart = true

tokenRegistry.buyTokens { [self] tokens in
isBuySupported = tokens.first(where: { $0.symbol.lowercased() == token.symbol.lowercased() }) != nil
}

keyringController.defaultKeyringInfo { [self] keyring in
accounts = keyring.accountInfos.map {
.init(account: $0, decimalBalance: 0.0, balance: "", fiatBalance: "")
Expand Down
4 changes: 3 additions & 1 deletion BraveWallet/Crypto/Stores/BuyTokenStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ public class BuyTokenStore: ObservableObject {

public init(
tokenRegistry: BraveWalletERCTokenRegistry,
rpcController: BraveWalletEthJsonRpcController
rpcController: BraveWalletEthJsonRpcController,
prefilledToken: BraveWallet.ERCToken?
) {
self.tokenRegistry = tokenRegistry
self.rpcController = rpcController
self.selectedBuyToken = prefilledToken
}

func fetchBuyUrl(account: BraveWallet.AccountInfo, amount: String, completion: @escaping (_ url: String?) -> Void) {
Expand Down
16 changes: 10 additions & 6 deletions BraveWallet/Crypto/Stores/CryptoStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,20 +66,21 @@ public class CryptoStore: ObservableObject {
}

private var buyTokenStore: BuyTokenStore?
func openBuyTokenStore() -> BuyTokenStore {
func openBuyTokenStore(_ prefilledToken: BraveWallet.ERCToken?) -> BuyTokenStore {
if let store = buyTokenStore {
return store
}
let store = BuyTokenStore(
tokenRegistry: tokenRegistry,
rpcController: rpcController
rpcController: rpcController,
prefilledToken: prefilledToken
)
buyTokenStore = store
return store
}

private var sendTokenStore: SendTokenStore?
func openSendTokenStore() -> SendTokenStore {
func openSendTokenStore(_ prefilledToken: BraveWallet.ERCToken?) -> SendTokenStore {
if let store = sendTokenStore {
return store
}
Expand All @@ -88,14 +89,15 @@ public class CryptoStore: ObservableObject {
rpcController: rpcController,
walletService: walletService,
transactionController: transactionController,
tokenRegistery: tokenRegistry
tokenRegistery: tokenRegistry,
prefilledToken: prefilledToken
)
sendTokenStore = store
return store
}

private var swapTokenStore: SwapTokenStore?
func openSwapTokenStore() -> SwapTokenStore {
func openSwapTokenStore(_ prefilledToken: BraveWallet.ERCToken?) -> SwapTokenStore {
if let store = swapTokenStore {
return store
}
Expand All @@ -105,7 +107,8 @@ public class CryptoStore: ObservableObject {
rpcController: rpcController,
assetRatioController: assetRatioController,
swapController: swapController,
transactionController: transactionController
transactionController: transactionController,
prefilledToken: prefilledToken
)
swapTokenStore = store
return store
Expand All @@ -121,6 +124,7 @@ public class CryptoStore: ObservableObject {
keyringController: keyringController,
rpcController: rpcController,
txController: transactionController,
tokenRegistry: tokenRegistry,
token: token
)
assetDetailStore = store
Expand Down
4 changes: 3 additions & 1 deletion BraveWallet/Crypto/Stores/SendTokenStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,15 @@ public class SendTokenStore: ObservableObject {
rpcController: BraveWalletEthJsonRpcController,
walletService: BraveWalletBraveWalletService,
transactionController: BraveWalletEthTxController,
tokenRegistery: BraveWalletERCTokenRegistry
tokenRegistery: BraveWalletERCTokenRegistry,
prefilledToken: BraveWallet.ERCToken?
) {
self.keyringController = keyringController
self.rpcController = rpcController
self.walletService = walletService
self.transactionController = transactionController
self.tokenRegistery = tokenRegistery
self.selectedSendToken = prefilledToken

self.keyringController.add(self)
self.rpcController.add(self)
Expand Down
18 changes: 15 additions & 3 deletions BraveWallet/Crypto/Stores/SwapTokenStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ public class SwapTokenStore: ObservableObject {
}
private var updatingPriceQuote = false
private var timer: Timer?
private let batSymbol = "BAT"
private let daiSymbol = "DAI"

enum SwapParamsBase {
// calculating based on sell asset amount
Expand Down Expand Up @@ -140,14 +142,16 @@ public class SwapTokenStore: ObservableObject {
rpcController: BraveWalletEthJsonRpcController,
assetRatioController: BraveWalletAssetRatioController,
swapController: BraveWalletSwapController,
transactionController: BraveWalletEthTxController
transactionController: BraveWalletEthTxController,
prefilledToken: BraveWallet.ERCToken?
) {
self.keyringController = keyringController
self.tokenRegistry = tokenRegistry
self.rpcController = rpcController
self.assetRatioController = assetRatioController
self.swapController = swapController
self.transactionController = transactionController
self.selectedFromToken = prefilledToken

self.keyringController.add(self)
self.rpcController.add(self)
Expand Down Expand Up @@ -561,9 +565,17 @@ public class SwapTokenStore: ObservableObject {
}
} else {
if chainId == BraveWallet.MainnetChainId {
selectedToToken = allTokens.first(where: { $0.symbol.uppercased() == "BAT" })
if let fromToken = selectedFromToken, fromToken.symbol.uppercased() == batSymbol.uppercased() {
selectedToToken = allTokens.first(where: { $0.symbol.uppercased() != batSymbol.uppercased() })
} else {
selectedToToken = allTokens.first(where: { $0.symbol.uppercased() == batSymbol.uppercased() })
}
} else if chainId == BraveWallet.RopstenChainId {
selectedToToken = allTokens.first(where: { $0.symbol.uppercased() == "DAI" })
if let fromToken = selectedFromToken, fromToken.symbol.uppercased() == daiSymbol.uppercased() {
selectedToToken = allTokens.first(where: { $0.symbol.uppercased() != daiSymbol.uppercased() })
} else {
selectedToToken = allTokens.first(where: { $0.symbol.uppercased() == daiSymbol.uppercased() })
}
}
completion?()
}
Expand Down
10 changes: 7 additions & 3 deletions BraveWallet/Preview Content/TestStores.swift
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ extension BuyTokenStore {
static var previewStore: BuyTokenStore {
.init(
tokenRegistry: TestTokenRegistry(),
rpcController: TestEthJsonRpcController()
rpcController: TestEthJsonRpcController(),
prefilledToken: .eth
)
}
}
Expand All @@ -71,7 +72,8 @@ extension SendTokenStore {
rpcController: TestEthJsonRpcController(),
walletService: TestBraveWalletService(),
transactionController: TestEthTxController(),
tokenRegistery: TestTokenRegistry()
tokenRegistery: TestTokenRegistry(),
prefilledToken: .eth
)
}
}
Expand All @@ -83,6 +85,7 @@ extension AssetDetailStore {
keyringController: TestKeyringController(),
rpcController: TestEthJsonRpcController(),
txController: TestEthTxController(),
tokenRegistry: TestTokenRegistry(),
token: .eth
)
}
Expand All @@ -96,7 +99,8 @@ extension SwapTokenStore {
rpcController: TestEthJsonRpcController(),
assetRatioController: TestAssetRatioController(),
swapController: TestSwapController(),
transactionController: TestEthTxController()
transactionController: TestEthTxController(),
prefilledToken: nil
)
}
}
Expand Down
Loading

0 comments on commit 8ae3f1c

Please sign in to comment.