From c8fcc652d6f05bc397cbcaff1a4e5c2de43fd770 Mon Sep 17 00:00:00 2001 From: Oleg Date: Thu, 4 Apr 2024 14:45:42 +0700 Subject: [PATCH] MOB-1930 - Show profile recommendations if only ens domain owned (#479) --- .../Entities/Wallets/WalletEntity.swift | 16 +++++++++++++++ .../Explore/HomeExploreViewModel.swift | 10 ++++++++-- .../HomeWalletView/HomeWalletViewModel.swift | 20 ++----------------- .../DomainProfilesService.swift | 13 +++++++----- .../DomainProfilesServiceProtocol.swift | 2 +- .../DomainProfilesServiceTests.swift | 16 +++++---------- .../TestableDomainProfilesService.swift | 4 ++-- 7 files changed, 42 insertions(+), 39 deletions(-) diff --git a/unstoppable-ios-app/domains-manager-ios/Entities/Wallets/WalletEntity.swift b/unstoppable-ios-app/domains-manager-ios/Entities/Wallets/WalletEntity.swift index bcbd208b0..4f50dc800 100644 --- a/unstoppable-ios-app/domains-manager-ios/Entities/Wallets/WalletEntity.swift +++ b/unstoppable-ios-app/domains-manager-ios/Entities/Wallets/WalletEntity.swift @@ -121,6 +121,22 @@ extension WalletEntity: Identifiable { var id: String { address } } +extension WalletEntity { + enum WalletProfileState { + case noProfile, udDomain(DomainDisplayInfo), ensDomain(DomainDisplayInfo) + } + + func getCurrentWalletProfileState() -> WalletProfileState { + if let rrDomain = rrDomain { + return .udDomain(rrDomain) + } else if let ensDomain = domains.first(where: { $0.isENSDomain }), + !isReverseResolutionChangeAllowed() { + return .ensDomain(ensDomain) + } + return .noProfile + } +} + extension Array where Element == WalletEntity { func findWithAddress(_ address: HexAddress?) -> Element? { guard let address = address?.normalized else { return nil } diff --git a/unstoppable-ios-app/domains-manager-ios/Modules/Explore/HomeExploreViewModel.swift b/unstoppable-ios-app/domains-manager-ios/Modules/Explore/HomeExploreViewModel.swift index 4cec2e90f..abbd5cd94 100644 --- a/unstoppable-ios-app/domains-manager-ios/Modules/Explore/HomeExploreViewModel.swift +++ b/unstoppable-ios-app/domains-manager-ios/Modules/Explore/HomeExploreViewModel.swift @@ -234,8 +234,9 @@ private extension HomeExploreViewModel { } func loadSuggestedProfilesFor(wallet: WalletEntity) { + guard let rrDomain = getSelectedUserProfileRRDomain() else { return } Task { - let suggestedProfiles = try await domainProfilesService.getSuggestionsFor(wallet: wallet) + let suggestedProfiles = try await domainProfilesService.getSuggestionsFor(domainName: rrDomain.name) setSuggestedProfiles(suggestedProfiles) } } @@ -288,7 +289,12 @@ private extension HomeExploreViewModel { func getSelectedUserProfileRRDomain() -> DomainDisplayInfo? { guard case .wallet(let wallet) = selectedProfile else { return nil } - return wallet.rrDomain + switch wallet.getCurrentWalletProfileState() { + case .udDomain(let domain), .ensDomain(let domain): + return domain + case .noProfile: + return nil + } } func loadNewProfileSuggestionsIfAllFollowing() { diff --git a/unstoppable-ios-app/domains-manager-ios/Modules/Home/HomeWalletView/HomeWalletViewModel.swift b/unstoppable-ios-app/domains-manager-ios/Modules/Home/HomeWalletView/HomeWalletViewModel.swift index 30462faec..e94966845 100644 --- a/unstoppable-ios-app/domains-manager-ios/Modules/Home/HomeWalletView/HomeWalletViewModel.swift +++ b/unstoppable-ios-app/domains-manager-ios/Modules/Home/HomeWalletView/HomeWalletViewModel.swift @@ -75,7 +75,7 @@ extension HomeWalletView { case .receive: router.showingWalletInfo = selectedWallet case .profile: - switch getCurrentWalletProfileState() { + switch selectedWallet.getCurrentWalletProfileState() { case .udDomain(let domain), .ensDomain(let domain): showProfile(of: domain) case .noProfile: @@ -142,7 +142,7 @@ extension HomeWalletView { } var isProfileButtonEnabled: Bool { - switch getCurrentWalletProfileState() { + switch selectedWallet.getCurrentWalletProfileState() { case .udDomain, .ensDomain: return true case .noProfile: @@ -310,19 +310,3 @@ fileprivate extension HomeWalletView.HomeWalletViewModel { } } } - -fileprivate extension HomeWalletView.HomeWalletViewModel { - enum WalletProfileState { - case noProfile, udDomain(DomainDisplayInfo), ensDomain(DomainDisplayInfo) - } - - func getCurrentWalletProfileState() -> WalletProfileState { - if let rrDomain = selectedWallet.rrDomain { - return .udDomain(rrDomain) - } else if let ensDomain = selectedWallet.domains.first(where: { $0.isENSDomain }), - !selectedWallet.isReverseResolutionChangeAllowed() { - return .ensDomain(ensDomain) - } - return .noProfile - } -} diff --git a/unstoppable-ios-app/domains-manager-ios/Services/DomainProfilesService/DomainProfilesService.swift b/unstoppable-ios-app/domains-manager-ios/Services/DomainProfilesService/DomainProfilesService.swift index 2e561c3e9..288353997 100644 --- a/unstoppable-ios-app/domains-manager-ios/Services/DomainProfilesService/DomainProfilesService.swift +++ b/unstoppable-ios-app/domains-manager-ios/Services/DomainProfilesService/DomainProfilesService.swift @@ -118,10 +118,8 @@ extension DomainProfilesService: DomainProfilesServiceProtocol { loadMoreSocialIfAbleFor(relationshipType: relationshipType, walletAddress: wallet.address) } - func getSuggestionsFor(wallet: WalletEntity) async throws -> [DomainProfileSuggestion] { - guard let rrDomain = wallet.rrDomain else { return [] } // No suggestions for user without domain - - let serializedSuggestions = try await networkService.getProfileSuggestions(for: rrDomain.name) + func getSuggestionsFor(domainName: DomainName) async throws -> [DomainProfileSuggestion] { + let serializedSuggestions = try await networkService.getProfileSuggestions(for: domainName) let profileSuggestions = serializedSuggestions.map { DomainProfileSuggestion(serializedProfile: $0) } return profileSuggestions } @@ -206,7 +204,12 @@ private extension DomainProfilesService { func getProfileDomainNameFor(walletAddress: HexAddress) -> DomainName? { let wallet = walletsDataService.wallets.findWithAddress(walletAddress) - return wallet?.profileDomainName + switch wallet?.getCurrentWalletProfileState() { + case .noProfile, nil: + return nil + case .udDomain(let domain), .ensDomain(let domain): + return domain.name + } } func getSerializedPublicDomainProfile(for domainName: DomainName) async throws -> SerializedPublicDomainProfile { diff --git a/unstoppable-ios-app/domains-manager-ios/Services/DomainProfilesService/DomainProfilesServiceProtocol.swift b/unstoppable-ios-app/domains-manager-ios/Services/DomainProfilesService/DomainProfilesServiceProtocol.swift index d461891cc..79f2a9f11 100644 --- a/unstoppable-ios-app/domains-manager-ios/Services/DomainProfilesService/DomainProfilesServiceProtocol.swift +++ b/unstoppable-ios-app/domains-manager-ios/Services/DomainProfilesService/DomainProfilesServiceProtocol.swift @@ -25,6 +25,6 @@ protocol DomainProfilesServiceProtocol { in wallet: WalletEntity) func publisherForWalletDomainProfileDetails(wallet: WalletEntity) async -> CurrentValueSubject - func getSuggestionsFor(wallet: WalletEntity) async throws -> [DomainProfileSuggestion] + func getSuggestionsFor(domainName: DomainName) async throws -> [DomainProfileSuggestion] func getTrendingDomainNames() async throws -> [DomainName] } diff --git a/unstoppable-ios-app/domains-manager-iosTests/DomainProfilesServiceTests.swift b/unstoppable-ios-app/domains-manager-iosTests/DomainProfilesServiceTests.swift index f24f39cbd..88f7a9519 100644 --- a/unstoppable-ios-app/domains-manager-iosTests/DomainProfilesServiceTests.swift +++ b/unstoppable-ios-app/domains-manager-iosTests/DomainProfilesServiceTests.swift @@ -207,19 +207,12 @@ final class DomainProfilesServiceTests: BaseTestClass { } // MARK: - Profile Suggestions tests - func testEmptyProfileSuggestionsIfNoDomainInWallet() async throws { - let walletWithoutDomain = MockEntitiesFabric.Wallet.mockEntities(hasRRDomain: false).first! - let suggestions = try await service.getSuggestionsFor(wallet: walletWithoutDomain) - - XCTAssertTrue(networkService.suggestionsCallDomainNames.isEmpty) - XCTAssertTrue(suggestions.isEmpty) - } - func testProfileSuggestionsReturnSuccess() async throws { let wallet = mockWallet() - let suggestions = try await service.getSuggestionsFor(wallet: wallet) + let domainName = wallet.rrDomain!.name + let suggestions = try await service.getSuggestionsFor(domainName: domainName) - XCTAssertEqual(networkService.suggestionsCallDomainNames, [wallet.rrDomain!.name]) + XCTAssertEqual(networkService.suggestionsCallDomainNames, [domainName]) XCTAssertEqual(suggestions.map { $0.domain }, networkService.suggestionToReturn.map { $0.domain }) } @@ -227,7 +220,8 @@ final class DomainProfilesServiceTests: BaseTestClass { networkService.shouldFail = true do { let wallet = mockWallet() - let _ = try await service.getSuggestionsFor(wallet: wallet) + let domainName = wallet.rrDomain!.name + let _ = try await service.getSuggestionsFor(domainName: domainName) XCTFail("Expected network error") } catch { assertNetworkErrorThrown(error) diff --git a/unstoppable-ios-app/domains-manager-iosTests/Helpers/TestableDomainProfilesService.swift b/unstoppable-ios-app/domains-manager-iosTests/Helpers/TestableDomainProfilesService.swift index 5f3f7baed..2624b01b6 100644 --- a/unstoppable-ios-app/domains-manager-iosTests/Helpers/TestableDomainProfilesService.swift +++ b/unstoppable-ios-app/domains-manager-iosTests/Helpers/TestableDomainProfilesService.swift @@ -51,8 +51,8 @@ final class TestableDomainProfilesService: DomainProfilesServiceProtocol, Failab loadMoreCallsHistory.append(relationshipType) } - func getSuggestionsFor(wallet: WalletEntity) async throws -> [DomainProfileSuggestion] { - loadSuggestionsCallsHistory.append(wallet.address) + func getSuggestionsFor(domainName: DomainName) async throws -> [DomainProfileSuggestion] { + loadSuggestionsCallsHistory.append(domainName) try failIfNeeded() return profilesSuggestions }