Skip to content

Commit

Permalink
Use existing GetSiteRequest(instanceURL: URL) system
Browse files Browse the repository at this point in the history
  • Loading branch information
Sjmarf committed Jan 17, 2024
1 parent e7cb5bf commit 4a81fc3
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
5 changes: 5 additions & 0 deletions Mlem/API/APIClient/APIClient.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions Mlem/Models/Content/Post Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -119,5 +119,6 @@ extension PostModel: Hashable {
hasher.combine(saved)
hasher.combine(read)
hasher.combine(post.updated)
hasher.combine(unreadCommentCount)
}
}
25 changes: 14 additions & 11 deletions Mlem/Views/Shared/Instance/InstanceView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)) {
Expand Down Expand Up @@ -185,6 +187,7 @@ struct InstanceView: View {
}
}
}
.navigationBarColor()
.navigationTitle(instance?.name ?? domainName)
.navigationBarTitleDisplayMode(.inline)
}
Expand Down
1 change: 1 addition & 0 deletions Mlem/Views/Tabs/Profile/UserView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ struct UserView: View {
}
}
.fancyTabScrollCompatible()
.navigationBarColor()
.navigationTitle(user.displayName)
.navigationBarTitleDisplayMode(.inline)
.sheet(isPresented: $isPresentingAccountSwitcher) {
Expand Down

0 comments on commit 4a81fc3

Please sign in to comment.