Skip to content

Commit

Permalink
MOB-1930 - Show profile recommendations if only ens domain owned (#479)
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg-Pecheneg authored Apr 4, 2024
1 parent 35d7066 commit c8fcc65
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -142,7 +142,7 @@ extension HomeWalletView {
}

var isProfileButtonEnabled: Bool {
switch getCurrentWalletProfileState() {
switch selectedWallet.getCurrentWalletProfileState() {
case .udDomain, .ensDomain:
return true
case .noProfile:
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ protocol DomainProfilesServiceProtocol {
in wallet: WalletEntity)
func publisherForWalletDomainProfileDetails(wallet: WalletEntity) async -> CurrentValueSubject<WalletDomainProfileDetails, Never>

func getSuggestionsFor(wallet: WalletEntity) async throws -> [DomainProfileSuggestion]
func getSuggestionsFor(domainName: DomainName) async throws -> [DomainProfileSuggestion]
func getTrendingDomainNames() async throws -> [DomainName]
}
Original file line number Diff line number Diff line change
Expand Up @@ -207,27 +207,21 @@ 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 })
}

func testProfileSuggestionsFails() async {
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit c8fcc65

Please sign in to comment.