IceCubesApp/Packages/Env/Sources/Env/CurrentInstance.swift

48 lines
973 B
Swift
Raw Normal View History

import Combine
import Foundation
import Models
import Network
@MainActor
public class CurrentInstance: ObservableObject {
@Published public private(set) var instance: Instance?
2023-01-17 10:36:01 +00:00
private var client: Client?
2023-01-17 10:36:01 +00:00
public static let shared = CurrentInstance()
2023-02-12 15:29:41 +00:00
private var version: Float {
if let stringVersion = instance?.version {
if stringVersion.utf8.count > 2 {
return Float(stringVersion.prefix(3)) ?? 0
} else {
return Float(stringVersion.prefix(1)) ?? 0
}
}
return 0
2023-02-12 08:58:21 +00:00
}
2023-01-27 19:36:40 +00:00
public var isFiltersSupported: Bool {
version >= 4
}
2023-01-27 19:36:40 +00:00
public var isEditSupported: Bool {
version >= 4
}
2023-02-12 15:29:41 +00:00
public var isEditAltTextSupported: Bool {
version >= 4.1
}
2023-01-17 10:36:01 +00:00
private init() {}
public func setClient(client: Client) {
self.client = client
}
2023-01-17 10:36:01 +00:00
public func fetchCurrentInstance() async {
guard let client = client else { return }
2023-02-03 18:44:55 +00:00
instance = try? await client.get(endpoint: Instances.instance)
}
}