another.im-ios/ConversationsClassic/Helpers/UserDefaultsWrapper.swift

33 lines
788 B
Swift
Raw Normal View History

2024-06-19 15:15:27 +00:00
import Foundation
// Wrapper
@propertyWrapper
struct Storage<T> {
private let key: String
private let defaultValue: T
init(key: String, defaultValue: T) {
self.key = key
self.defaultValue = defaultValue
}
var wrappedValue: T {
get {
// Read value from UserDefaults
UserDefaults.standard.object(forKey: key) as? T ?? defaultValue
}
set {
// Set value to UserDefaults
UserDefaults.standard.set(newValue, forKey: key)
}
}
}
// Storage
private let keyLocalizationSelected = "conversations.classic.user.defaults.localizationSelected"
enum UserSettings {
@Storage(key: keyLocalizationSelected, defaultValue: false)
static var localizationSelectedByUser: Bool
}