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)
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) {

View File

@ -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)

View File

@ -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)
}
}

View File

@ -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()
}

View File

@ -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?
}