2024-01-04 18:11:07 +01:00
|
|
|
//Made by Lumaa
|
|
|
|
|
|
|
|
import Foundation
|
2024-01-04 22:19:35 +01:00
|
|
|
|
|
|
|
@Observable
|
|
|
|
class UserPreferences: Codable, ObservableObject {
|
|
|
|
private static let saveKey: String = "threaded-preferences.user"
|
|
|
|
public static let defaultPreferences: UserPreferences = .init()
|
|
|
|
|
|
|
|
// Final
|
|
|
|
var displayedName: DisplayedName = .username
|
|
|
|
var profilePictureShape: ProfilePictureShape = .circle
|
|
|
|
|
2024-01-21 13:18:03 +01:00
|
|
|
var browserType: BrowserType = .inApp
|
2024-03-08 14:28:54 +01:00
|
|
|
var defaultVisibility: Visibility = .pub
|
2024-01-21 13:18:03 +01:00
|
|
|
|
2024-01-04 22:19:35 +01:00
|
|
|
// Experimental
|
|
|
|
var showExperimental: Bool = false
|
|
|
|
var experimental: UserPreferences.Experimental
|
|
|
|
|
2024-03-08 14:28:54 +01:00
|
|
|
init(displayedName: DisplayedName = .username, profilePictureShape: ProfilePictureShape = .circle, browserType: BrowserType = .inApp, defaultVisibility: Visibility = .pub, showExperimental: Bool = false, experimental: UserPreferences.Experimental = .init()) {
|
2024-01-04 22:19:35 +01:00
|
|
|
self.displayedName = displayedName
|
|
|
|
self.profilePictureShape = profilePictureShape
|
2024-01-21 13:18:03 +01:00
|
|
|
self.browserType = browserType
|
2024-03-08 14:28:54 +01:00
|
|
|
self.defaultVisibility = defaultVisibility
|
2024-01-04 22:19:35 +01:00
|
|
|
|
|
|
|
self.showExperimental = showExperimental
|
|
|
|
self.experimental = experimental
|
|
|
|
}
|
|
|
|
|
|
|
|
@Observable
|
|
|
|
class Experimental: Codable, ObservableObject {
|
|
|
|
private static let saveKey: String = "threaded-preferences.experimental"
|
|
|
|
|
|
|
|
var replySymbol: Bool = false
|
|
|
|
|
|
|
|
init(replySymbol: Bool = false) {
|
|
|
|
self.replySymbol = replySymbol
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveAsCurrent() throws {
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
let json = try encoder.encode(self)
|
|
|
|
UserDefaults.standard.setValue(json, forKey: UserPreferences.Experimental.saveKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
static func loadAsCurrent() throws -> UserPreferences.Experimental? {
|
|
|
|
let decoder = JSONDecoder()
|
|
|
|
if let data = UserDefaults.standard.data(forKey: UserPreferences.Experimental.saveKey) {
|
|
|
|
let exp = try decoder.decode(UserPreferences.Experimental.self, from: data)
|
|
|
|
return exp
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveAsCurrent() throws {
|
|
|
|
let encoder = JSONEncoder()
|
|
|
|
let json = try encoder.encode(self)
|
|
|
|
UserDefaults.standard.setValue(json, forKey: UserPreferences.saveKey)
|
|
|
|
}
|
|
|
|
|
2024-03-09 22:46:07 +01:00
|
|
|
static func loadAsCurrent() throws -> UserPreferences {
|
2024-01-04 22:19:35 +01:00
|
|
|
let decoder = JSONDecoder()
|
|
|
|
if let data = UserDefaults.standard.data(forKey: UserPreferences.saveKey) {
|
2024-03-09 22:46:07 +01:00
|
|
|
let pref = try? decoder.decode(UserPreferences.self, from: data)
|
|
|
|
return pref ?? UserPreferences.defaultPreferences
|
2024-01-04 22:19:35 +01:00
|
|
|
}
|
2024-03-09 22:46:07 +01:00
|
|
|
return UserPreferences.defaultPreferences
|
|
|
|
}
|
|
|
|
|
|
|
|
// WARNINGS EVERYWHERE WHY...
|
|
|
|
required init(from decoder: Decoder) throws {
|
|
|
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
|
|
|
self.displayedName = try container.decode(DisplayedName.self, forKey: ._displayedName) ?? .username
|
|
|
|
self.profilePictureShape = try container.decode(ProfilePictureShape.self, forKey: ._profilePictureShape) ?? .circle
|
|
|
|
self.browserType = try container.decode(BrowserType.self, forKey: ._browserType) ?? .inApp
|
|
|
|
self.defaultVisibility = try container.decode(Visibility.self, forKey: ._defaultVisibility) ?? .pub
|
|
|
|
self.showExperimental = try container.decode(Bool.self, forKey: ._showExperimental) ?? false
|
|
|
|
self.experimental = try container.decode(UserPreferences.Experimental.self, forKey: ._experimental) ?? .init()
|
2024-01-04 22:19:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Enums and other
|
|
|
|
|
|
|
|
enum DisplayedName: Codable, CaseIterable {
|
|
|
|
case username, displayName, both
|
|
|
|
}
|
|
|
|
|
|
|
|
enum ProfilePictureShape: Codable, CaseIterable {
|
|
|
|
case circle, rounded
|
|
|
|
}
|
2024-01-21 13:18:03 +01:00
|
|
|
|
|
|
|
enum BrowserType: Codable, CaseIterable {
|
|
|
|
case inApp, outApp
|
|
|
|
}
|
2024-01-04 22:19:35 +01:00
|
|
|
}
|