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

Commit

Permalink
still need a way to update isSpam attribute in CD
Browse files Browse the repository at this point in the history
  • Loading branch information
nuo-xu committed Oct 11, 2023
1 parent c2b3912 commit 95a3237
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
10 changes: 5 additions & 5 deletions Sources/BraveWallet/Crypto/NFT/NFTView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -228,10 +228,10 @@ struct NFTView: View {
}
.contextMenu {
Button(action: {
if nft.token.visible { // a collected visible NFT
nftStore.updateNFTStatus(nft.token, visible: false, isDeletedByUser: false)
} else { // including hidden NFTs and junk NFTs
nftStore.updateNFTStatus(nft.token, visible: true, isDeletedByUser: false)
if nft.token.visible { // a collected visible NFT, mark as hidden
nftStore.updateNFTStatus(nft.token, visible: false, isSpam: false, isDeletedByUser: false)
} else { // either a hidden NFT or a junk NFT, mark as visible
nftStore.updateNFTStatus(nft.token, visible: true, isSpam: false, isDeletedByUser: false)
}
}) {
if nft.token.visible { // a collected visible NFT
Expand All @@ -243,7 +243,7 @@ struct NFTView: View {
}
}
Button(action: {
nftStore.updateNFTStatus(nft.token, visible: false, isDeletedByUser: true)
nftStore.updateNFTStatus(nft.token, visible: false, isSpam: nft.token.isSpam, isDeletedByUser: true)
}) {
Label(Strings.Wallet.nftRemoveFromWallet, braveSystemImage: "leo.trash")
}
Expand Down
14 changes: 12 additions & 2 deletions Sources/BraveWallet/Crypto/Stores/NFTStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,18 @@ public class NFTStore: ObservableObject, WalletObserverStore {
walletService.setNftDiscoveryEnabled(true)
}

func updateNFTStatus(_ token: BraveWallet.BlockchainToken, visible: Bool, isDeletedByUser: Bool) {
assetManager.updateUserAsset(for: token, visible: visible, isDeletedByUser: isDeletedByUser) { [weak self] in
func updateNFTStatus(
_ token: BraveWallet.BlockchainToken,
visible: Bool,
isSpam: Bool,
isDeletedByUser: Bool
) {
assetManager.updateUserAsset(
for: token,
visible: visible,
isSpam: isSpam,
isDeletedByUser: isDeletedByUser
) { [weak self] in
guard let self else { return }
let selectedAccounts = self.filters.accounts.filter(\.isSelected).map(\.model)
let selectedNetworks = self.filters.networks.filter(\.isSelected).map(\.model)
Expand Down
2 changes: 1 addition & 1 deletion Sources/BraveWallet/Crypto/Stores/UserAssetsStore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class AssetStore: ObservableObject, Equatable, WalletObserverStore {
@Published var token: BraveWallet.BlockchainToken
@Published var isVisible: Bool {
didSet {
assetManager.updateUserAsset(for: token, visible: isVisible, isDeletedByUser: false, completion: nil)
assetManager.updateUserAsset(for: token, visible: isVisible, isSpam: false, isDeletedByUser: false, completion: nil)
}
}
var network: BraveWallet.NetworkInfo
Expand Down
9 changes: 6 additions & 3 deletions Sources/BraveWallet/WalletUserAssetManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public protocol WalletUserAssetManagerType: AnyObject {
func removeUserAsset(_ asset: BraveWallet.BlockchainToken, completion: (() -> Void)?)
/// Remove an entire `WalletUserAssetGroup` with a given `groupId`
func removeGroup(for groupId: String, completion: (() -> Void)?)
/// Update a `WalletUserAsset`'s `visible` and `isDeletedByUser` status
func updateUserAsset(for asset: BraveWallet.BlockchainToken, visible: Bool, isDeletedByUser: Bool, completion: (() -> Void)?)
/// Update a `WalletUserAsset`'s `visible`, `isSpam`, and `isDeletedByUser` status
func updateUserAsset(for asset: BraveWallet.BlockchainToken, visible: Bool, isSpam: Bool, isDeletedByUser: Bool, completion: (() -> Void)?)
}

public class WalletUserAssetManager: WalletUserAssetManagerType {
Expand Down Expand Up @@ -113,7 +113,7 @@ public class WalletUserAssetManager: WalletUserAssetManagerType {
public func addUserAsset(_ asset: BraveWallet.BlockchainToken, completion: (() -> Void)?) {
if let existedAsset = WalletUserAsset.getUserAsset(asset: asset) {
if existedAsset.isDeletedByUser { // this asset was added before but user marked as deleted after
WalletUserAsset.updateUserAsset(for: asset, visible: true, isDeletedByUser: false, completion: completion)
WalletUserAsset.updateUserAsset(for: asset, visible: true, isSpam: false, isDeletedByUser: false, completion: completion)
} else { // this asset exists, either in `Collected` or `Hidden` based on its `visible` value
completion?()
return
Expand All @@ -130,12 +130,14 @@ public class WalletUserAssetManager: WalletUserAssetManagerType {
public func updateUserAsset(
for asset: BraveWallet.BlockchainToken,
visible: Bool,
isSpam: Bool,
isDeletedByUser: Bool,
completion: (() -> Void)?
) {
WalletUserAsset.updateUserAsset(
for: asset,
visible: visible,
isSpam: isSpam,
isDeletedByUser: isDeletedByUser,
completion: completion
)
Expand Down Expand Up @@ -238,6 +240,7 @@ public class TestableWalletUserAssetManager: WalletUserAssetManagerType {
public func updateUserAsset(
for asset: BraveWallet.BlockchainToken,
visible: Bool,
isSpam: Bool,
isDeletedByUser: Bool,
completion: (() -> Void)?
) {
Expand Down
3 changes: 3 additions & 0 deletions Sources/Data/models/WalletUserAsset.swift
Original file line number Diff line number Diff line change
Expand Up @@ -117,18 +117,21 @@ public final class WalletUserAsset: NSManagedObject, CRUD {
public static func updateUserAsset(
for asset: BraveWallet.BlockchainToken,
visible: Bool,
isSpam: Bool,
isDeletedByUser: Bool,
completion: (() -> Void)? = nil
) {
DataController.perform(context: .new(inMemory: false), save: false) { context in
if let asset = WalletUserAsset.first(where: NSPredicate(format: "contractAddress == %@ AND chainId == %@ AND symbol == %@ AND tokenId == %@", asset.contractAddress, asset.chainId, asset.symbol, asset.tokenId), context: context) {
asset.visible = visible
asset.isSpam = isSpam
asset.isDeletedByUser = isDeletedByUser
} else {
let groupId = asset.walletUserAssetGroupId
let group = WalletUserAssetGroup.getGroup(groupId: groupId, context: context) ?? WalletUserAssetGroup(context: context, groupId: groupId)
let visibleAsset = WalletUserAsset(context: context, asset: asset)
visibleAsset.visible = visible
visibleAsset.isSpam = isSpam
visibleAsset.isDeletedByUser = isDeletedByUser
visibleAsset.walletUserAssetGroup = group
}
Expand Down
8 changes: 5 additions & 3 deletions Tests/DataTests/WalletUserAssetTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,18 @@ class WalletUserAssetTests: CoreDataTestCase {

XCTAssertTrue(userAsset.visible)
XCTAssertFalse(userAsset.isSpam)
XCTAssertFalse(userAsset.isDeletedByUser)

backgroundSaveAndWaitForExpectation {
WalletUserAsset.updateUserAsset(for: asset, visible: false, isDeletedByUser: true)
WalletUserAsset.updateUserAsset(for: asset, visible: false, isSpam: true, isDeletedByUser: true)
}

DataController.viewContext.refreshAllObjects()
// Make sure only one record was added to DB
XCTAssertEqual(try! DataController.viewContext.count(for: fetchRequest), 1)

XCTAssertFalse(userAsset.visible)
XCTAssertTrue(userAsset.isSpam)
XCTAssertTrue(userAsset.isDeletedByUser)
}

Expand All @@ -57,7 +59,7 @@ class WalletUserAssetTests: CoreDataTestCase {
createAndWait(asset: asset3)

backgroundSaveAndWaitForExpectation {
WalletUserAsset.updateUserAsset(for: asset2, visible: false, isDeletedByUser: false)
WalletUserAsset.updateUserAsset(for: asset2, visible: false, isSpam: false, isDeletedByUser: false)
}

DataController.viewContext.refreshAllObjects()
Expand All @@ -71,7 +73,7 @@ class WalletUserAssetTests: CoreDataTestCase {
createAndWait(asset: asset2)

backgroundSaveAndWaitForExpectation {
WalletUserAsset.updateUserAsset(for: asset2, visible: false, isDeletedByUser: true)
WalletUserAsset.updateUserAsset(for: asset2, visible: false, isSpam: false, isDeletedByUser: true)
}

DataController.viewContext.refreshAllObjects()
Expand Down

0 comments on commit 95a3237

Please sign in to comment.