mirror of
https://github.com/Dimillian/IceCubesApp.git
synced 2025-02-18 10:45:15 +00:00
Fix Link Handling (#69)
This commit is contained in:
parent
66efedbbda
commit
d646fef9f4
6 changed files with 34 additions and 3 deletions
|
@ -22,12 +22,21 @@ public class CurrentAccount: ObservableObject {
|
||||||
|
|
||||||
private func fetchUserData() async {
|
private func fetchUserData() async {
|
||||||
await withTaskGroup(of: Void.self) { group in
|
await withTaskGroup(of: Void.self) { group in
|
||||||
|
group.addTask { await self.fetchConnections() }
|
||||||
group.addTask { await self.fetchCurrentAccount() }
|
group.addTask { await self.fetchCurrentAccount() }
|
||||||
group.addTask { await self.fetchLists() }
|
group.addTask { await self.fetchLists() }
|
||||||
group.addTask { await self.fetchFollowedTags() }
|
group.addTask { await self.fetchFollowedTags() }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func fetchConnections() async {
|
||||||
|
guard let client = client else { return }
|
||||||
|
do {
|
||||||
|
let connections: [String] = try await client.get(endpoint: Instances.peers)
|
||||||
|
client.addConnections(connections)
|
||||||
|
} catch { }
|
||||||
|
}
|
||||||
|
|
||||||
public func fetchCurrentAccount() async {
|
public func fetchCurrentAccount() async {
|
||||||
guard let client = client, client.isAuth else {
|
guard let client = client, client.isAuth else {
|
||||||
account = nil
|
account = nil
|
||||||
|
@ -62,7 +71,6 @@ public class CurrentAccount: ObservableObject {
|
||||||
} catch { }
|
} catch { }
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public func deleteList(list: Models.List) async {
|
public func deleteList(list: Models.List) async {
|
||||||
guard let client else { return }
|
guard let client else { return }
|
||||||
lists.removeAll(where: { $0.id == list.id })
|
lists.removeAll(where: { $0.id == list.id })
|
||||||
|
|
|
@ -66,6 +66,8 @@ public class RouterPath: ObservableObject {
|
||||||
navigate(to: .accountDetail(id: mention.id))
|
navigate(to: .accountDetail(id: mention.id))
|
||||||
return .handled
|
return .handled
|
||||||
} else if let client = client,
|
} else if let client = client,
|
||||||
|
client.isAuth,
|
||||||
|
client.hasConnection(with: url),
|
||||||
let id = Int(url.lastPathComponent) {
|
let id = Int(url.lastPathComponent) {
|
||||||
if url.absoluteString.contains(client.server) {
|
if url.absoluteString.contains(client.server) {
|
||||||
navigate(to: .statusDetail(id: String(id)))
|
navigate(to: .statusDetail(id: String(id)))
|
||||||
|
|
|
@ -20,6 +20,7 @@ public class Client: ObservableObject, Equatable {
|
||||||
|
|
||||||
public var server: String
|
public var server: String
|
||||||
public let version: Version
|
public let version: Version
|
||||||
|
public private(set) var connections: Set<String>
|
||||||
private let urlSession: URLSession
|
private let urlSession: URLSession
|
||||||
private let decoder = JSONDecoder()
|
private let decoder = JSONDecoder()
|
||||||
|
|
||||||
|
@ -38,8 +39,20 @@ public class Client: ObservableObject, Equatable {
|
||||||
self.urlSession = URLSession.shared
|
self.urlSession = URLSession.shared
|
||||||
self.decoder.keyDecodingStrategy = .convertFromSnakeCase
|
self.decoder.keyDecodingStrategy = .convertFromSnakeCase
|
||||||
self.oauthToken = oauthToken
|
self.oauthToken = oauthToken
|
||||||
|
self.connections = Set([server])
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public func addConnections(_ connections: [String]) {
|
||||||
|
connections.forEach {
|
||||||
|
self.connections.insert($0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public func hasConnection(with url: URL) -> Bool {
|
||||||
|
guard let host = url.host(percentEncoded: false) else { return false }
|
||||||
|
return connections.contains(host)
|
||||||
|
}
|
||||||
|
|
||||||
private func makeURL(scheme: String = "https", endpoint: Endpoint, forceVersion: Version? = nil) -> URL {
|
private func makeURL(scheme: String = "https", endpoint: Endpoint, forceVersion: Version? = nil) -> URL {
|
||||||
var components = URLComponents()
|
var components = URLComponents()
|
||||||
components.scheme = scheme
|
components.scheme = scheme
|
||||||
|
|
|
@ -2,11 +2,14 @@ import Foundation
|
||||||
|
|
||||||
public enum Instances: Endpoint {
|
public enum Instances: Endpoint {
|
||||||
case instance
|
case instance
|
||||||
|
case peers
|
||||||
|
|
||||||
public func path() -> String {
|
public func path() -> String {
|
||||||
switch self {
|
switch self {
|
||||||
case .instance:
|
case .instance:
|
||||||
return "instance"
|
return "instance"
|
||||||
|
case .peers:
|
||||||
|
return "instance/peers"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,7 @@ public struct StatusDetailView: View {
|
||||||
@EnvironmentObject private var watcher: StreamWatcher
|
@EnvironmentObject private var watcher: StreamWatcher
|
||||||
@EnvironmentObject private var client: Client
|
@EnvironmentObject private var client: Client
|
||||||
@EnvironmentObject private var routeurPath: RouterPath
|
@EnvironmentObject private var routeurPath: RouterPath
|
||||||
|
@Environment(\.openURL) private var openURL
|
||||||
@StateObject private var viewModel: StatusDetailViewModel
|
@StateObject private var viewModel: StatusDetailViewModel
|
||||||
@State private var isLoaded: Bool = false
|
@State private var isLoaded: Bool = false
|
||||||
|
|
||||||
|
@ -81,7 +82,12 @@ public struct StatusDetailView: View {
|
||||||
viewModel.client = client
|
viewModel.client = client
|
||||||
let result = await viewModel.fetch()
|
let result = await viewModel.fetch()
|
||||||
if !result {
|
if !result {
|
||||||
_ = routeurPath.path.popLast()
|
if let url = viewModel.remoteStatusURL {
|
||||||
|
openURL(url)
|
||||||
|
}
|
||||||
|
DispatchQueue.main.async {
|
||||||
|
_ = routeurPath.path.popLast()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
DispatchQueue.main.async {
|
DispatchQueue.main.async {
|
||||||
proxy.scrollTo(viewModel.statusId, anchor: .center)
|
proxy.scrollTo(viewModel.statusId, anchor: .center)
|
||||||
|
|
|
@ -51,7 +51,6 @@ class StatusDetailViewModel: ObservableObject {
|
||||||
await fetchStatusDetail()
|
await fetchStatusDetail()
|
||||||
return true
|
return true
|
||||||
} else {
|
} else {
|
||||||
await UIApplication.shared.open(remoteStatusURL)
|
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue