From d15181dcb6c935af1b0118e4aa8a958a2a4557d3 Mon Sep 17 00:00:00 2001 From: Nathan Mattes Date: Mon, 23 Oct 2023 13:57:50 +0200 Subject: [PATCH] Show profile on tap Bridge account to user as long as Profile-screen doesn't work with Mastodon.Entity.Account, but MastodonUser --- .../Provider/DataSourceFacade+Profile.swift | 27 ++++++++++++++++++- .../DataSourceFacade+SearchHistory.swift | 3 ++- ...taSourceProvider+UITableViewDelegate.swift | 2 ++ .../Provider/DataSourceProvider.swift | 1 + ...istViewController+DataSourceProvider.swift | 2 ++ ...ultViewController+DataSourceProvider.swift | 3 +++ .../Entity/Mastodon+Entity+Account.swift | 8 ++++-- 7 files changed, 42 insertions(+), 4 deletions(-) diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+Profile.swift b/Mastodon/Protocol/Provider/DataSourceFacade+Profile.swift index 8f77a1888..30c024f54 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+Profile.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+Profile.swift @@ -8,6 +8,7 @@ import UIKit import CoreDataStack import MastodonCore +import MastodonSDK extension DataSourceFacade { @@ -53,7 +54,31 @@ extension DataSourceFacade { transition: .show ) } - + + @MainActor + static func coordinateToProfileScene( + provider: ViewControllerWithDependencies & AuthContextProvider, + account: Mastodon.Entity.Account + ) async { + provider.coordinator.showLoading() + + guard let domain = account.domain else { return provider.coordinator.hideLoading() } + + Task { + do { + let user = try await provider.context.apiService.fetchUser(username: account.username, + domain: domain, + authenticationBox: provider.authContext.mastodonAuthenticationBox) + provider.coordinator.hideLoading() + + if let user { + await coordinateToProfileScene(provider: provider, user: user.asRecord) + } + } catch { + provider.coordinator.hideLoading() + } + } + } } extension DataSourceFacade { diff --git a/Mastodon/Protocol/Provider/DataSourceFacade+SearchHistory.swift b/Mastodon/Protocol/Provider/DataSourceFacade+SearchHistory.swift index edc4fbe2f..31206c262 100644 --- a/Mastodon/Protocol/Provider/DataSourceFacade+SearchHistory.swift +++ b/Mastodon/Protocol/Provider/DataSourceFacade+SearchHistory.swift @@ -17,7 +17,8 @@ extension DataSourceFacade { item: DataSourceItem ) async { switch item { - case .status: + + case .status, .account(_, _): break // not create search history for status case .user(let record): let authenticationBox = provider.authContext.mastodonAuthenticationBox diff --git a/Mastodon/Protocol/Provider/DataSourceProvider+UITableViewDelegate.swift b/Mastodon/Protocol/Provider/DataSourceProvider+UITableViewDelegate.swift index c45ef4ca1..5143b3c10 100644 --- a/Mastodon/Protocol/Provider/DataSourceProvider+UITableViewDelegate.swift +++ b/Mastodon/Protocol/Provider/DataSourceProvider+UITableViewDelegate.swift @@ -21,6 +21,8 @@ extension UITableViewDelegate where Self: DataSourceProvider & AuthContextProvid return } switch item { + case .account(let account, relationship: _): + await DataSourceFacade.coordinateToProfileScene(provider: self, account: account) case .status(let status): await DataSourceFacade.coordinateToStatusThreadScene( provider: self, diff --git a/Mastodon/Protocol/Provider/DataSourceProvider.swift b/Mastodon/Protocol/Provider/DataSourceProvider.swift index 6df47ccae..b92aadcef 100644 --- a/Mastodon/Protocol/Provider/DataSourceProvider.swift +++ b/Mastodon/Protocol/Provider/DataSourceProvider.swift @@ -16,6 +16,7 @@ enum DataSourceItem: Hashable { case user(record: ManagedObjectRecord) case hashtag(tag: TagKind) case notification(record: ManagedObjectRecord) + case account(account: Mastodon.Entity.Account, relationship: Mastodon.Entity.Relationship?) } extension DataSourceItem { diff --git a/Mastodon/Scene/Profile/Following/FollowingListViewController+DataSourceProvider.swift b/Mastodon/Scene/Profile/Following/FollowingListViewController+DataSourceProvider.swift index 3ea2a74c1..790c8e2ac 100644 --- a/Mastodon/Scene/Profile/Following/FollowingListViewController+DataSourceProvider.swift +++ b/Mastodon/Scene/Profile/Following/FollowingListViewController+DataSourceProvider.swift @@ -20,6 +20,8 @@ extension FollowingListViewController: DataSourceProvider { } switch item { + case .account(let account, let relationship): + return .account(account: account, relationship: relationship) case .user(let record): return .user(record: record) default: diff --git a/Mastodon/Scene/Search/SearchDetail/SearchResult/SearchResultViewController+DataSourceProvider.swift b/Mastodon/Scene/Search/SearchDetail/SearchResult/SearchResultViewController+DataSourceProvider.swift index f2e8c7c6e..5e9d9e2db 100644 --- a/Mastodon/Scene/Search/SearchDetail/SearchResult/SearchResultViewController+DataSourceProvider.swift +++ b/Mastodon/Scene/Search/SearchDetail/SearchResult/SearchResultViewController+DataSourceProvider.swift @@ -52,6 +52,9 @@ extension SearchResultViewController { ) switch item { + case .account(account: _, relationship: _): + // do nothing + break case .status(let status): await DataSourceFacade.coordinateToStatusThreadScene( provider: self, diff --git a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift index 34fdad5ba..eb2910bb9 100644 --- a/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift +++ b/MastodonSDK/Sources/MastodonSDK/Entity/Mastodon+Entity+Account.swift @@ -91,11 +91,15 @@ extension Mastodon.Entity.Account { } return acct } -} -extension Mastodon.Entity.Account { public var verifiedLink: Mastodon.Entity.Field? { let firstVerified = fields?.first(where: { $0.verifiedAt != nil }) return firstVerified } + + public var domain: String? { + guard let components = URLComponents(string: url) else { return nil } + + return components.host + } }