From 4a81fc3f9e486cc12a20950b827c82fa5869711a Mon Sep 17 00:00:00 2001 From: Sjmarf <78750526+Sjmarf@users.noreply.github.com> Date: Wed, 17 Jan 2024 20:50:39 +0000 Subject: [PATCH] Use existing `GetSiteRequest(instanceURL: URL)` system --- Mlem/API/APIClient/APIClient.swift | 5 ++++ Mlem/Models/Content/Post Model.swift | 1 + Mlem/Views/Shared/Instance/InstanceView.swift | 25 +++++++++++-------- Mlem/Views/Tabs/Profile/UserView.swift | 1 + 4 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Mlem/API/APIClient/APIClient.swift b/Mlem/API/APIClient/APIClient.swift index e3a8c9239..5e68f9d22 100644 --- a/Mlem/API/APIClient/APIClient.swift +++ b/Mlem/API/APIClient/APIClient.swift @@ -384,6 +384,11 @@ extension APIClient { return try await perform(request: request) } + func loadSiteInformation(instanceURL: URL) async throws -> SiteResponse { + let request = GetSiteRequest(instanceURL: instanceURL.appendingPathComponent("api/v3")) + return try await perform(request: request) + } + // swiftlint:disable function_parameter_count func performSearch( query: String, diff --git a/Mlem/Models/Content/Post Model.swift b/Mlem/Models/Content/Post Model.swift index 49081467b..0ee3e71b3 100644 --- a/Mlem/Models/Content/Post Model.swift +++ b/Mlem/Models/Content/Post Model.swift @@ -119,5 +119,6 @@ extension PostModel: Hashable { hasher.combine(saved) hasher.combine(read) hasher.combine(post.updated) + hasher.combine(unreadCommentCount) } } diff --git a/Mlem/Views/Shared/Instance/InstanceView.swift b/Mlem/Views/Shared/Instance/InstanceView.swift index 8183cf31e..608fb0b26 100644 --- a/Mlem/Views/Shared/Instance/InstanceView.swift +++ b/Mlem/Views/Shared/Instance/InstanceView.swift @@ -25,6 +25,7 @@ enum InstanceViewTab: String, Identifiable, CaseIterable { } struct InstanceView: View { + @Dependency(\.apiClient) var apiClient: APIClient @Dependency(\.errorHandler) var errorHandler @Environment(\.navigationPathWithRoutes) private var navigationPath @@ -138,19 +139,20 @@ struct InstanceView: View { .task { if instance?.administrators == nil { do { - let client = APIClient(transport: { urlSession, urlRequest in try await urlSession.data(for: urlRequest) }) - let url = try await getCorrectURLtoEndpoint(baseInstanceAddress: domainName) - client.session = .unauthenticated(url) - let info = try await client.loadSiteInformation() - DispatchQueue.main.async { - withAnimation(.easeOut(duration: 0.2)) { - if var instance { - instance.update(with: info) - self.instance = instance - } else { - self.instance = InstanceModel(from: info) + if let url = URL(string: "https://\(domainName)") { + let info = try await apiClient.loadSiteInformation(instanceURL: url) + DispatchQueue.main.async { + withAnimation(.easeOut(duration: 0.2)) { + if var instance { + instance.update(with: info) + self.instance = instance + } else { + self.instance = InstanceModel(from: info) + } } } + } else { + errorDetails = ErrorDetails(title: "\"\(domainName)\" is an invalid URL.") } } catch EndpointDiscoveryError.couldNotFindAnyCorrectEndpoints { withAnimation(.easeOut(duration: 0.2)) { @@ -185,6 +187,7 @@ struct InstanceView: View { } } } + .navigationBarColor() .navigationTitle(instance?.name ?? domainName) .navigationBarTitleDisplayMode(.inline) } diff --git a/Mlem/Views/Tabs/Profile/UserView.swift b/Mlem/Views/Tabs/Profile/UserView.swift index cc00b4a50..f1a1ee8f2 100644 --- a/Mlem/Views/Tabs/Profile/UserView.swift +++ b/Mlem/Views/Tabs/Profile/UserView.swift @@ -206,6 +206,7 @@ struct UserView: View { } } .fancyTabScrollCompatible() + .navigationBarColor() .navigationTitle(user.displayName) .navigationBarTitleDisplayMode(.inline) .sheet(isPresented: $isPresentingAccountSwitcher) {