From 4de46610e007cae40a5717bf1a2ed11fe3d92035 Mon Sep 17 00:00:00 2001 From: Thomas Ricouard Date: Fri, 3 Feb 2023 19:44:55 +0100 Subject: [PATCH] Handle dynamic streaming URL --- IceCubesApp/App/IceCubesApp.swift | 6 +++++- IceCubesShareExtension/ShareViewController.swift | 3 +++ Packages/Env/Sources/Env/CurrentInstance.swift | 7 +------ Packages/Env/Sources/Env/StreamWatcher.swift | 7 +++++-- Packages/Models/Sources/Models/Instance.swift | 5 +++++ 5 files changed, 19 insertions(+), 9 deletions(-) diff --git a/IceCubesApp/App/IceCubesApp.swift b/IceCubesApp/App/IceCubesApp.swift index 5b2bd1a2..1db14571 100644 --- a/IceCubesApp/App/IceCubesApp.swift +++ b/IceCubesApp/App/IceCubesApp.swift @@ -158,7 +158,11 @@ struct IceCubesApp: App { currentAccount.setClient(client: client) currentInstance.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) { diff --git a/IceCubesShareExtension/ShareViewController.swift b/IceCubesShareExtension/ShareViewController.swift index a7757421..a9707fd3 100644 --- a/IceCubesShareExtension/ShareViewController.swift +++ b/IceCubesShareExtension/ShareViewController.swift @@ -17,6 +17,9 @@ class ShareViewController: UIViewController { let instance = CurrentInstance.shared account.setClient(client: client) instance.setClient(client: client) + Task { + await instance.fetchCurrentInstance() + } let colorScheme = traitCollection.userInterfaceStyle let theme = Theme.shared theme.setColor(withName: colorScheme == .dark ? .iceCubeDark : .iceCubeLight) diff --git a/Packages/Env/Sources/Env/CurrentInstance.swift b/Packages/Env/Sources/Env/CurrentInstance.swift index d4343a1b..77fef15e 100644 --- a/Packages/Env/Sources/Env/CurrentInstance.swift +++ b/Packages/Env/Sources/Env/CurrentInstance.swift @@ -22,15 +22,10 @@ public class CurrentInstance: ObservableObject { public func setClient(client: Client) { self.client = client - Task { - await fetchCurrentInstance() - } } public func fetchCurrentInstance() async { guard let client = client else { return } - Task { - instance = try? await client.get(endpoint: Instances.instance) - } + instance = try? await client.get(endpoint: Instances.instance) } } diff --git a/Packages/Env/Sources/Env/StreamWatcher.swift b/Packages/Env/Sources/Env/StreamWatcher.swift index aabd5b16..ff7714d6 100644 --- a/Packages/Env/Sources/Env/StreamWatcher.swift +++ b/Packages/Env/Sources/Env/StreamWatcher.swift @@ -7,6 +7,7 @@ public class StreamWatcher: ObservableObject { private var client: Client? private var task: URLSessionWebSocketTask? private var watchedStreams: [Stream] = [] + private var instanceStreamingURL: URL? private let decoder = JSONDecoder() private let encoder = JSONEncoder() @@ -25,16 +26,18 @@ public class StreamWatcher: ObservableObject { decoder.keyDecodingStrategy = .convertFromSnakeCase } - public func setClient(client: Client) { + public func setClient(client: Client, instanceStreamingURL: URL?) { if self.client != nil { stopWatching() } self.client = client + self.instanceStreamingURL = instanceStreamingURL 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() receiveMessage() } diff --git a/Packages/Models/Sources/Models/Instance.swift b/Packages/Models/Sources/Models/Instance.swift index c8e9f7f7..58559b8d 100644 --- a/Packages/Models/Sources/Models/Instance.swift +++ b/Packages/Models/Sources/Models/Instance.swift @@ -28,6 +28,10 @@ public struct Instance: Codable { public let id: String public let text: String } + + public struct URLs: Codable { + public let streamingApi: URL? + } public let title: String public let shortDescription: String @@ -39,4 +43,5 @@ public struct Instance: Codable { public let thumbnail: URL? public let configuration: Configuration? public let rules: [Rule]? + public let urls: URLs? }