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

45 lines
1.4 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-24 21:42:28 +00:00
public static let sharedDefault = UserDefaults(suiteName: "group.com.thomasricouard.IceCubesApp")
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 20:08:05 +00:00
@AppStorage("font_size_scale") public var fontSizeScale: Double = 1
@AppStorage("show_translate_button_inline") public var showTranslateButton: Bool = true
@AppStorage("is_open_ai_enabled") public var isOpenAIEnabled: Bool = true
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)
}
}