Tweak how InstanceFeatures is updated

This commit is contained in:
Shadowfacts 2022-11-29 20:52:39 -05:00
parent 811aac35d7
commit 0485400c1f
1 changed files with 18 additions and 5 deletions

View File

@ -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<AnyCancellable>()
private var pendingOwnInstanceRequestCallbacks = [(Result<Instance, Client.Error>) -> 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)
}
}
}
}