another.im-ios/ConversationsClassic/Helpers/UserDefaultsWrapper.swift
2024-10-07 18:58:02 +02:00

42 lines
1 KiB
Swift

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 kOmemoDeviceId = "conversations.classic.user.defaults.omemoDeviceId"
private let kSecureChatsByDefault = "conversations.classic.user.defaults.secureChatsByDefault"
enum UserSettings {
@Storage(key: kOmemoDeviceId, defaultValue: 0)
static var omemoDeviceId: UInt32
@Storage(key: kSecureChatsByDefault, defaultValue: false)
static var secureChatsByDefault: Bool
static func reset() {
omemoDeviceId = 0
secureChatsByDefault = false
}
}