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

Commit

Permalink
addressed more comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
nuo-xu committed Oct 23, 2023
1 parent e91f8af commit 50a0166
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ struct FiltersDisplaySettingsView: View {
self._isHidingSmallBalances = State(initialValue: filters.isHidingSmallBalances)
self._isHidingUnownedNFTs = State(initialValue: filters.groupBy == .accounts ? true : filters.isHidingUnownedNFTs)
self._isShowingNFTNetworkLogo = State(initialValue: filters.isShowingNFTNetworkLogo)
self._isHidingUnownedNFTsDisabled = State(initialValue: filters.groupBy == .accounts ? true : false)
self._isHidingUnownedNFTsDisabled = State(initialValue: filters.groupBy == .accounts)
self._accounts = State(initialValue: filters.accounts)
self._networks = State(initialValue: filters.networks)
self.isNFTFilters = isNFTFilters
Expand Down
4 changes: 2 additions & 2 deletions Sources/BraveWallet/Crypto/NFT/NFTView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ struct NFTView: View {
HStack(spacing: 12) {
Text(nftStore.displayType.dropdownTitle)
.font(.subheadline.weight(.semibold))
Text("\(nftStore.totalDisplayNFTCounter)")
Text("\(nftStore.totalDisplayedNFTCount)")
.padding(.horizontal, 8)
.padding(.vertical, 4)
.font(.caption2.weight(.semibold))
.background(
Color(uiColor: WalletV2Design.displayNFTCounterColor)
Color(braveSystemName: .primary20)
.cornerRadius(4)
)
Image(braveSystemName: "leo.carat.down")
Expand Down
130 changes: 75 additions & 55 deletions Sources/BraveWallet/Crypto/Stores/NFTStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public class NFTStore: ObservableObject, WalletObserverStore {
return displayNFTGroups.isEmpty
}

var totalDisplayNFTCounter: Int {
var totalDisplayedNFTCount: Int {
displayNFTGroups.reduce(0) { $0 + $1.assets.count }
}

Expand Down Expand Up @@ -258,10 +258,24 @@ public class NFTStore: ObservableObject, WalletObserverStore {
let selectedAccounts = filters.accounts.filter(\.isSelected).map(\.model)
let selectedNetworks = filters.networks.filter(\.isSelected).map(\.model)

// user visible assets
let userVisibleAssets = assetManager.getAllUserAssetsInNetworkAssetsByVisibility(networks: selectedNetworks, visible: true)
// user hidden assets
let userHiddenAssets = assetManager.getAllUserAssetsInNetworkAssetsByVisibility(networks: selectedNetworks, visible: false)
// user visible NFTs
let userVisibleNFTs = assetManager.getAllUserAssetsInNetworkAssetsByVisibility(networks: selectedNetworks, visible: true)
.map { networkAssets in
NetworkAssets(
network: networkAssets.network,
tokens: networkAssets.tokens.filter { $0.isNft || $0.isErc721 }
, sortOrder: networkAssets.sortOrder
)
}
// user hidden NFTs
let userHiddenNFTs = assetManager.getAllUserAssetsInNetworkAssetsByVisibility(networks: selectedNetworks, visible: false)
.map { networkAssets in
NetworkAssets(
network: networkAssets.network,
tokens: networkAssets.tokens.filter { $0.isNft || $0.isErc721 }
, sortOrder: networkAssets.sortOrder
)
}
// all spam NFTs marked by SimpleHash
simpleHashSpamNFTs = await walletService.simpleHashSpamNFTs(for: selectedAccounts, on: selectedNetworks)
let unionedSpamNFTs = computeSpamNFTs(
Expand All @@ -270,28 +284,22 @@ public class NFTStore: ObservableObject, WalletObserverStore {
simpleHashSpamNFTs: simpleHashSpamNFTs
)

var allNetworkAssets: [NetworkAssets] = []
for networkAssets in userVisibleAssets {
let hiddenAssets = userHiddenAssets.first(where: {
$0.network.chainId == networkAssets.network.chainId && $0.network.coin == networkAssets.network.coin
})?.tokens ?? []
let spamNFTs = unionedSpamNFTs.first(where: {
$0.network.chainId == networkAssets.network.chainId && $0.network.coin == networkAssets.network.coin
})?.tokens ?? []
allNetworkAssets.append(.init(network: networkAssets.network, tokens: networkAssets.tokens + hiddenAssets + spamNFTs, sortOrder: networkAssets.sortOrder))
}
let allNetworkNFTs = generateAllNFTsInNetworks(
userVisibleNFTs: userVisibleNFTs,
userHiddenNFTs: userHiddenNFTs,
computedSpamNFTs: unionedSpamNFTs
)
userNFTGroups = buildNFTGroupModels(
groupBy: filters.groupBy,
allUserAssets: allNetworkAssets,
allUserNFTs: allNetworkNFTs,
selectedAccounts: selectedAccounts,
selectedNetworks: selectedNetworks
)

var allTokens: [BraveWallet.BlockchainToken] = []
for networkAssets in [userVisibleAssets, userHiddenAssets, unionedSpamNFTs] {
allTokens.append(contentsOf: networkAssets.flatMap(\.tokens))
var allNFTs: [BraveWallet.BlockchainToken] = []
for networkAssets in [userVisibleNFTs, userHiddenNFTs, unionedSpamNFTs] {
allNFTs.append(contentsOf: networkAssets.flatMap(\.tokens))
}
let allNFTs = allTokens.filter { $0.isNft || $0.isErc721 }
// fetch balance for all NFTs
let allAccounts = filters.accounts.map(\.model)
nftBalancesCache = await withTaskGroup(
Expand Down Expand Up @@ -332,7 +340,7 @@ public class NFTStore: ObservableObject, WalletObserverStore {
guard !Task.isCancelled else { return }
userNFTGroups = buildNFTGroupModels(
groupBy: filters.groupBy,
allUserAssets: allNetworkAssets,
allUserNFTs: allNetworkNFTs,
selectedAccounts: selectedAccounts,
selectedNetworks: selectedNetworks
)
Expand All @@ -345,7 +353,7 @@ public class NFTStore: ObservableObject, WalletObserverStore {
guard !Task.isCancelled else { return }
userNFTGroups = buildNFTGroupModels(
groupBy: filters.groupBy,
allUserAssets: allNetworkAssets,
allUserNFTs: allNetworkNFTs,
selectedAccounts: selectedAccounts,
selectedNetworks: selectedNetworks
)
Expand All @@ -367,17 +375,16 @@ public class NFTStore: ObservableObject, WalletObserverStore {
userNFTGroups = updatedGroups
}

private func buildAssetViewModels(
private func buildNFTAssetViewModels(
for groupType: AssetGroupType,
allUserAssets: [NetworkAssets]
allUserNFTs: [NetworkAssets]
) -> [NFTAssetViewModel] {
let selectedAccounts = self.filters.accounts.filter(\.isSelected).map(\.model)
switch groupType {
case .none:
return allUserAssets.flatMap { networkAssets in
networkAssets.tokens.compactMap { token in
guard token.isErc721 || token.isNft else { return nil }
return NFTAssetViewModel(
return allUserNFTs.flatMap { networkAssets in
networkAssets.tokens.map { token in
NFTAssetViewModel(
groupType: groupType,
token: token,
network: networkAssets.network,
Expand All @@ -394,17 +401,16 @@ public class NFTStore: ObservableObject, WalletObserverStore {
first.token.symbol < second.token.symbol
}
case let .network(network):
guard let networkAssets = allUserAssets
guard let networkNFTs = allUserNFTs
.first(where: { $0.network.chainId == network.chainId && $0.network.coin == network.coin }) else {
return []
}
return networkAssets.tokens
.filter { $0.isErc721 || $0.isNft }
return networkNFTs.tokens
.map { token in
NFTAssetViewModel(
groupType: groupType,
token: token,
network: networkAssets.network,
network: networkNFTs.network,
balanceForAccounts: nftBalancesCache[token.id] ?? [:],
nftMetadata: metadataCache[token.id]
)
Expand All @@ -417,17 +423,17 @@ public class NFTStore: ObservableObject, WalletObserverStore {
first.token.symbol < second.token.symbol
}
case let .account(account):
return allUserAssets
return allUserNFTs
.filter { $0.network.coin == account.coin && $0.network.supportedKeyrings.contains(account.accountId.keyringId.rawValue as NSNumber)
}
.flatMap { networkAssets in
networkAssets.tokens.compactMap { token in
.flatMap { networkNFTs in
networkNFTs.tokens.compactMap { token in
// we need to exclude any NFT that THIS account does not own (balance is not 1)
guard token.isErc721 || token.isNft, let balance = nftBalancesCache[token.id]?[account.address], balance > 0 else { return nil }
guard let balance = nftBalancesCache[token.id]?[account.address], balance > 0 else { return nil }
return NFTAssetViewModel(
groupType: groupType,
token: token,
network: networkAssets.network,
network: networkNFTs.network,
balanceForAccounts: nftBalancesCache[token.id] ?? [:],
nftMetadata: metadataCache[token.id]
)
Expand All @@ -443,6 +449,24 @@ public class NFTStore: ObservableObject, WalletObserverStore {
}
}

private func generateAllNFTsInNetworks(
userVisibleNFTs: [NetworkAssets],
userHiddenNFTs: [NetworkAssets],
computedSpamNFTs: [NetworkAssets]
) -> [NetworkAssets] {
var allNetworkNFTs: [NetworkAssets] = []
for networkNFTs in userVisibleNFTs {
let hiddenNFTs = userHiddenNFTs.first(where: {
$0.network.chainId == networkNFTs.network.chainId && $0.network.coin == networkNFTs.network.coin
})?.tokens ?? []
let spamNFTs = computedSpamNFTs.first(where: {
$0.network.chainId == networkNFTs.network.chainId && $0.network.coin == networkNFTs.network.coin
})?.tokens ?? []
allNetworkNFTs.append(.init(network: networkNFTs.network, tokens: networkNFTs.tokens + hiddenNFTs + spamNFTs, sortOrder: networkNFTs.sortOrder))
}
return allNetworkNFTs
}

private func computeSpamNFTs(
selectedNetworks: [BraveWallet.NetworkInfo],
selectedAccounts: [BraveWallet.AccountInfo],
Expand Down Expand Up @@ -489,16 +513,16 @@ public class NFTStore: ObservableObject, WalletObserverStore {

private func buildNFTGroupModels(
groupBy: GroupBy,
allUserAssets: [NetworkAssets],
allUserNFTs: [NetworkAssets],
selectedAccounts: [BraveWallet.AccountInfo],
selectedNetworks: [BraveWallet.NetworkInfo]
) -> [NFTGroupViewModel] {
let groups: [NFTGroupViewModel]
switch filters.groupBy {
case .none:
let assets = buildAssetViewModels(
let assets = buildNFTAssetViewModels(
for: .none,
allUserAssets: allUserAssets
allUserNFTs: allUserNFTs
)
return [
.init(
Expand All @@ -509,9 +533,9 @@ public class NFTStore: ObservableObject, WalletObserverStore {
case .accounts:
groups = selectedAccounts.map { account in
let groupType: AssetGroupType = .account(account)
let assets = buildAssetViewModels(
let assets = buildNFTAssetViewModels(
for: .account(account),
allUserAssets: allUserAssets
allUserNFTs: allUserNFTs
)
return NFTGroupViewModel(
groupType: groupType,
Expand All @@ -521,9 +545,9 @@ public class NFTStore: ObservableObject, WalletObserverStore {
case .networks:
groups = selectedNetworks.map { network in
let groupType: AssetGroupType = .network(network)
let assets = buildAssetViewModels(
let assets = buildNFTAssetViewModels(
for: .network(network),
allUserAssets: allUserAssets
allUserNFTs: allUserNFTs
)
return NFTGroupViewModel(
groupType: groupType,
Expand Down Expand Up @@ -564,19 +588,15 @@ public class NFTStore: ObservableObject, WalletObserverStore {
selectedAccounts: selectedAccounts,
simpleHashSpamNFTs: simpleHashSpamNFTs
)
var allNetworkAssets: [NetworkAssets] = []
for networkAssets in userVisibleAssets {
let hiddenAssets = userHiddenAssets.first(where: {
$0.network.chainId == networkAssets.network.chainId && $0.network.coin == networkAssets.network.coin
})?.tokens ?? []
let spamNFTs = unionedSpamNFTs.first(where: {
$0.network.chainId == networkAssets.network.chainId && $0.network.coin == networkAssets.network.coin
})?.tokens ?? []
allNetworkAssets.append(.init(network: networkAssets.network, tokens: networkAssets.tokens + hiddenAssets + spamNFTs, sortOrder: networkAssets.sortOrder))
}

let allNetworkNFTs = generateAllNFTsInNetworks(
userVisibleNFTs: userVisibleAssets,
userHiddenNFTs: userHiddenAssets,
computedSpamNFTs: unionedSpamNFTs
)
userNFTGroups = buildNFTGroupModels(
groupBy: filters.groupBy,
allUserAssets: allNetworkAssets,
allUserNFTs: allNetworkNFTs,
selectedAccounts: selectedAccounts,
selectedNetworks: selectedNetworks
)
Expand Down
7 changes: 0 additions & 7 deletions Sources/BraveWallet/Extensions/WalletColors.swift
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,4 @@ enum WalletV2Design {
blue: 207 / 255,
alpha: 1
)

static let displayNFTCounterColor = UIColor(
red: 213 / 255,
green: 220 / 255,
blue: 1,
alpha: 1
)
}

0 comments on commit 50a0166

Please sign in to comment.