IceCubesApp/Packages/Env/Sources/Env/UserPreferences.swift

42 lines
1.1 KiB
Swift
Raw Normal View History

import Foundation
2023-01-09 18:47:54 +00:00
import Models
import Network
2023-01-17 10:36:01 +00:00
import SwiftUI
2023-01-09 18:47:54 +00:00
@MainActor
public class UserPreferences: ObservableObject {
2023-01-17 10:36:01 +00:00
public static let sharedDefault = UserDefaults(suiteName: "group.icecubesapps")
public static let shared = UserPreferences()
2023-01-17 10:36:01 +00:00
2023-01-09 18:47:54 +00:00
private var client: Client?
2023-01-17 10:36:01 +00:00
@AppStorage("remote_local_timeline") public var remoteLocalTimelines: [String] = []
@AppStorage("preferred_browser") public var preferredBrowser: PreferredBrowser = .inAppSafari
2023-01-11 11:44:34 +00:00
@AppStorage("draft_posts") public var draftsPosts: [String] = []
2023-01-17 10:36:01 +00:00
2023-01-09 17:52:53 +00:00
public var pushNotificationsCount: Int {
get {
Self.sharedDefault?.integer(forKey: "push_notifications_count") ?? 0
}
set {
Self.sharedDefault?.set(newValue, forKey: "push_notifications_count")
}
}
2023-01-17 10:36:01 +00:00
@Published public var serverPreferences: ServerPreferences?
2023-01-17 10:36:01 +00:00
private init() {}
2023-01-09 18:47:54 +00:00
public func setClient(client: Client) {
self.client = client
Task {
await refreshServerPreferences()
}
}
2023-01-17 10:36:01 +00:00
2023-01-09 18:47:54 +00:00
public func refreshServerPreferences() async {
guard let client, client.isAuth else { return }
serverPreferences = try? await client.get(endpoint: Accounts.preferences)
}
}