2023-01-06 11:14:05 +00:00
|
|
|
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-06 11:14:05 +00:00
|
|
|
|
2023-01-09 18:47:54 +00:00
|
|
|
@MainActor
|
2023-01-06 11:14:05 +00:00
|
|
|
public class UserPreferences: ObservableObject {
|
2023-01-24 22:03:25 +00:00
|
|
|
public static let sharedDefault = UserDefaults(suiteName: "group.com.thomasricouard.IceCubesApp")
|
2023-01-17 06:39:13 +00:00
|
|
|
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
|
|
|
|
2023-01-06 11:14:05 +00:00
|
|
|
@AppStorage("remote_local_timeline") public var remoteLocalTimelines: [String] = []
|
2023-01-08 18:56:16 +00:00
|
|
|
@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
|
2023-01-22 08:15:01 +00:00
|
|
|
@AppStorage("show_translate_button_inline") public var showTranslateButton: Bool = true
|
2023-01-23 19:45:18 +00:00
|
|
|
@AppStorage("is_open_ai_enabled") public var isOpenAIEnabled: Bool = true
|
2023-01-24 20:34:16 +00:00
|
|
|
@AppStorage("recently_used_languages") public var recentlyUsedLanguages: [String] = []
|
2023-01-24 20:44:33 +00:00
|
|
|
@AppStorage("social_keyboard_composer") public var isSocialKeyboardEnabled: 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
|
|
|
|
2023-01-09 19:39:42 +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)
|
|
|
|
}
|
2023-01-24 20:34:16 +00:00
|
|
|
|
|
|
|
public func markLanguageAsSelected(isoCode: String) {
|
|
|
|
var copy = recentlyUsedLanguages
|
|
|
|
if let index = copy.firstIndex(of: isoCode) {
|
|
|
|
copy.remove(at: index)
|
|
|
|
}
|
|
|
|
copy.insert(isoCode, at: 0)
|
|
|
|
recentlyUsedLanguages = Array(copy.prefix(3))
|
|
|
|
}
|
2023-01-06 11:14:05 +00:00
|
|
|
}
|