Handle dynamic streaming URL

This commit is contained in:
Thomas Ricouard 2023-02-03 19:44:55 +01:00
parent 816eae2d40
commit 4de46610e0
5 changed files with 19 additions and 9 deletions

View file

@ -158,7 +158,11 @@ struct IceCubesApp: App {
currentAccount.setClient(client: client) currentAccount.setClient(client: client)
currentInstance.setClient(client: client) currentInstance.setClient(client: client)
userPreferences.setClient(client: client) userPreferences.setClient(client: client)
watcher.setClient(client: client) Task {
await currentInstance.fetchCurrentInstance()
watcher.setClient(client: client, instanceStreamingURL: currentInstance.instance?.urls?.streamingApi)
watcher.watch(streams: [.user, .direct])
}
} }
private func handleScenePhase(scenePhase: ScenePhase) { private func handleScenePhase(scenePhase: ScenePhase) {

View file

@ -17,6 +17,9 @@ class ShareViewController: UIViewController {
let instance = CurrentInstance.shared let instance = CurrentInstance.shared
account.setClient(client: client) account.setClient(client: client)
instance.setClient(client: client) instance.setClient(client: client)
Task {
await instance.fetchCurrentInstance()
}
let colorScheme = traitCollection.userInterfaceStyle let colorScheme = traitCollection.userInterfaceStyle
let theme = Theme.shared let theme = Theme.shared
theme.setColor(withName: colorScheme == .dark ? .iceCubeDark : .iceCubeLight) theme.setColor(withName: colorScheme == .dark ? .iceCubeDark : .iceCubeLight)

View file

@ -22,15 +22,10 @@ public class CurrentInstance: ObservableObject {
public func setClient(client: Client) { public func setClient(client: Client) {
self.client = client self.client = client
Task {
await fetchCurrentInstance()
}
} }
public func fetchCurrentInstance() async { public func fetchCurrentInstance() async {
guard let client = client else { return } guard let client = client else { return }
Task { instance = try? await client.get(endpoint: Instances.instance)
instance = try? await client.get(endpoint: Instances.instance)
}
} }
} }

View file

@ -7,6 +7,7 @@ public class StreamWatcher: ObservableObject {
private var client: Client? private var client: Client?
private var task: URLSessionWebSocketTask? private var task: URLSessionWebSocketTask?
private var watchedStreams: [Stream] = [] private var watchedStreams: [Stream] = []
private var instanceStreamingURL: URL?
private let decoder = JSONDecoder() private let decoder = JSONDecoder()
private let encoder = JSONEncoder() private let encoder = JSONEncoder()
@ -25,16 +26,18 @@ public class StreamWatcher: ObservableObject {
decoder.keyDecodingStrategy = .convertFromSnakeCase decoder.keyDecodingStrategy = .convertFromSnakeCase
} }
public func setClient(client: Client) { public func setClient(client: Client, instanceStreamingURL: URL?) {
if self.client != nil { if self.client != nil {
stopWatching() stopWatching()
} }
self.client = client self.client = client
self.instanceStreamingURL = instanceStreamingURL
connect() connect()
} }
private func connect() { private func connect() {
task = client?.makeWebSocketTask(endpoint: Streaming.streaming) guard let client else { return }
task = client.makeWebSocketTask(endpoint: Streaming.streaming, instanceStreamingURL: instanceStreamingURL)
task?.resume() task?.resume()
receiveMessage() receiveMessage()
} }

View file

@ -28,6 +28,10 @@ public struct Instance: Codable {
public let id: String public let id: String
public let text: String public let text: String
} }
public struct URLs: Codable {
public let streamingApi: URL?
}
public let title: String public let title: String
public let shortDescription: String public let shortDescription: String
@ -39,4 +43,5 @@ public struct Instance: Codable {
public let thumbnail: URL? public let thumbnail: URL?
public let configuration: Configuration? public let configuration: Configuration?
public let rules: [Rule]? public let rules: [Rule]?
public let urls: URLs?
} }