Skip to content

Commit

Permalink
Fix brave/brave-ios#7556: NFT importing improvement (brave/brave-ios#…
Browse files Browse the repository at this point in the history
  • Loading branch information
nuo-xu authored Jun 7, 2023
1 parent 76a2d71 commit 0845c53
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 60 deletions.
90 changes: 57 additions & 33 deletions Sources/BraveWallet/Crypto/NFT/NFTView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ struct NFTView: View {
self.isPresentingNetworkFilter = true
}) {
HStack {
Text(nftStore.networkFilter.title)
Image(braveSystemName: "leo.list")
Text(nftStore.networkFilter.title)
}
.font(.footnote.weight(.medium))
.foregroundColor(Color(.braveBlurpleTint))
Expand All @@ -128,18 +128,29 @@ struct NFTView: View {
Text(Strings.Wallet.assetsTitle)
.font(.footnote)
.foregroundColor(Color(.secondaryBraveLabel))
.padding(.leading, 16)
if nftStore.isLoadingDiscoverAssets && isNFTDiscoveryEnabled {
ProgressView()
.padding(.leading, 5)
}
Spacer()
networkFilterButton
.padding(.trailing, 10)
addCustomAssetButton
}
.textCase(nil)
.padding(.horizontal, 10)
.frame(maxWidth: .infinity, alignment: .leading)
}

private var addCustomAssetButton: some View {
Button(action: {
isShowingAddCustomNFT = true
}) {
Image(systemName: "plus")
}
}

private var nftDiscoveryDescriptionText: NSAttributedString? {
let attributedString = NSMutableAttributedString(
string: Strings.Wallet.nftDiscoveryCalloutDescription,
Expand All @@ -156,44 +167,57 @@ struct NFTView: View {
return attributedString
}

var body: some View {
ScrollView {
VStack {
nftHeaderView
if nftStore.userVisibleNFTs.isEmpty {
emptyView
.listRowBackground(Color(.clear))
} else {
LazyVGrid(columns: nftGrids) {
ForEach(nftStore.userVisibleNFTs) { nft in
Button(action: {
selectedNFTViewModel = nft
}) {
VStack(alignment: .leading, spacing: 4) {
nftImage(nft)
.padding(.bottom, 8)
Text(nft.token.nftTokenTitle)
.font(.callout.weight(.medium))
.foregroundColor(Color(.braveLabel))
.multilineTextAlignment(.leading)
if !nft.token.symbol.isEmpty {
Text(nft.token.symbol)
.font(.caption)
.foregroundColor(Color(.secondaryBraveLabel))
.multilineTextAlignment(.leading)
}
}
@ViewBuilder var nftGridsView: some View {
if nftStore.userVisibleNFTs.isEmpty {
emptyView
.listRowBackground(Color(.clear))
} else {
LazyVGrid(columns: nftGrids) {
ForEach(nftStore.userVisibleNFTs) { nft in
Button(action: {
selectedNFTViewModel = nft
}) {
VStack(alignment: .leading, spacing: 4) {
nftImage(nft)
.padding(.bottom, 8)
Text(nft.token.nftTokenTitle)
.font(.callout.weight(.medium))
.foregroundColor(Color(.braveLabel))
.multilineTextAlignment(.leading)
if !nft.token.symbol.isEmpty {
Text(nft.token.symbol)
.font(.caption)
.foregroundColor(Color(.secondaryBraveLabel))
.multilineTextAlignment(.leading)
}
}
}
VStack(spacing: 16) {
Divider()
editUserAssetsButton
.contextMenu {
Button(action: {
nftStore.updateVisibility(nft.token, visible: false)
}) {
Label(Strings.recentSearchHide, braveSystemImage: "leo.eye.off")
}
}
.padding(.top, 20)
}
}
.padding(24)
VStack(spacing: 16) {
Divider()
editUserAssetsButton
}
.padding(.top, 20)
}
}

var body: some View {
ScrollView {
VStack {
nftHeaderView
.padding(.horizontal, 8)
nftGridsView
.padding(.horizontal, 24)
}
.padding(.vertical, 24)
}
.background(Color(UIColor.braveGroupedBackground))
.background(
Expand Down
54 changes: 27 additions & 27 deletions Sources/BraveWallet/Crypto/Portfolio/AddCustomAssetView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,33 @@ struct AddCustomAssetView: View {
.pickerStyle(.segmented)
}
}
Section(
header: WalletListHeaderView(title: networkSelectionStore.networkSelectionInForm?.coin == .sol ? Text(Strings.Wallet.tokenMintAddress) : Text(Strings.Wallet.tokenAddress))
) {
TextField(Strings.Wallet.enterAddress, text: $addressInput)
.onChange(of: addressInput) { newValue in
guard !newValue.isEmpty else { return }
userAssetStore.tokenInfo(address: newValue) { token in
guard let token else { return }
if nameInput.isEmpty {
nameInput = token.name
}
if symbolInput.isEmpty {
symbolInput = token.symbol
}
if !token.isErc721, !token.isNft, decimalsInput.isEmpty {
decimalsInput = "\(token.decimals)"
}
if let network = networkStore.allChains.first(where: { $0.chainId == token.chainId }) {
networkSelectionStore.networkSelectionInForm = network
}
}
}
.autocapitalization(.none)
.autocorrectionDisabled()
.disabled(userAssetStore.isSearchingToken)
.listRowBackground(Color(.secondaryBraveGroupedBackground))
}
Section(
header: WalletListHeaderView(title: Text(Strings.Wallet.customTokenNetworkHeader))
) {
Expand Down Expand Up @@ -107,33 +134,6 @@ struct AddCustomAssetView: View {
}
.listRowBackground(Color(.secondaryBraveGroupedBackground))
}
Section(
header: WalletListHeaderView(title: networkSelectionStore.networkSelectionInForm?.coin == .sol ? Text(Strings.Wallet.tokenMintAddress) : Text(Strings.Wallet.tokenAddress))
) {
TextField(Strings.Wallet.enterAddress, text: $addressInput)
.onChange(of: addressInput) { newValue in
guard !newValue.isEmpty else { return }
userAssetStore.tokenInfo(address: newValue) { token in
guard let token else { return }
if nameInput.isEmpty {
nameInput = token.name
}
if symbolInput.isEmpty {
symbolInput = token.symbol
}
if !token.isErc721, !token.isNft, decimalsInput.isEmpty {
decimalsInput = "\(token.decimals)"
}
if let network = networkStore.allChains.first(where: { $0.chainId == token.chainId }) {
networkSelectionStore.networkSelectionInForm = network
}
}
}
.autocapitalization(.none)
.autocorrectionDisabled()
.disabled(userAssetStore.isSearchingToken)
.listRowBackground(Color(.secondaryBraveGroupedBackground))
}
Section(
header: WalletListHeaderView(title: Text(Strings.Wallet.tokenSymbol))
) {
Expand Down
8 changes: 8 additions & 0 deletions Sources/BraveWallet/Crypto/Stores/NFTStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,14 @@ public class NFTStore: ObservableObject {
func enableNFTDiscovery() {
walletService.setNftDiscoveryEnabled(true)
}

func updateVisibility(_ token: BraveWallet.BlockchainToken, visible: Bool) {
walletService.setUserAssetVisible(token, visible: visible) { [weak self] success in
if success {
self?.update()
}
}
}
}

extension NFTStore: BraveWalletJsonRpcServiceObserver {
Expand Down

0 comments on commit 0845c53

Please sign in to comment.