diff --git a/Tusker/API/MastodonController.swift b/Tusker/API/MastodonController.swift index 0fcf8a85..a43cd3d0 100644 --- a/Tusker/API/MastodonController.swift +++ b/Tusker/API/MastodonController.swift @@ -8,6 +8,7 @@ import Foundation import Pachyderm +import Combine class MastodonController: ObservableObject { @@ -47,7 +48,9 @@ class MastodonController: ObservableObject { @Published private(set) var nodeInfo: NodeInfo! @Published private(set) var instanceFeatures = InstanceFeatures() @Published private(set) var lists: [List] = [] - private(set) var customEmojis: [Emoji]? + @Published private(set) var customEmojis: [Emoji]? + + private var cancellables = Set() private var pendingOwnInstanceRequestCallbacks = [(Result) -> Void]() private var ownInstanceRequest: URLSessionTask? @@ -61,6 +64,20 @@ class MastodonController: ObservableObject { self.accountInfo = nil self.client = Client(baseURL: instanceURL, session: .appDefault) self.transient = transient + + $instance + .combineLatest($nodeInfo) + .compactMap { (instance, nodeInfo) in + if let instance { + return (instance, nodeInfo) + } else { + return nil + } + } + .sink { [unowned self] (instance, nodeInfo) in + self.instanceFeatures.update(instance: instance, nodeInfo: nodeInfo) + } + .store(in: &cancellables) } @discardableResult @@ -230,7 +247,6 @@ class MastodonController: ObservableObject { DispatchQueue.main.async { self.ownInstanceRequest = nil self.instance = instance - self.instanceFeatures.update(instance: instance, nodeInfo: self.nodeInfo) for completion in self.pendingOwnInstanceRequestCallbacks { completion(.success(instance)) @@ -248,9 +264,6 @@ class MastodonController: ObservableObject { case let .success(nodeInfo, _): DispatchQueue.main.async { self.nodeInfo = nodeInfo - if let instance = self.instance { - self.instanceFeatures.update(instance: instance, nodeInfo: nodeInfo) - } } } }