blob: 428dff44088bff67d24ad6fe4c7b38a112ba2b19 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
//
// AppStorage.swift
// MullvadSettings
//
// Created by Mojgan on 2024-01-05.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import Foundation
@propertyWrapper
public struct AppStorage<Value> {
let key: String
let defaultValue: Value
let container: UserDefaults
public var wrappedValue: Value {
get {
container.value(forKey: key) as? Value ?? defaultValue
}
set {
if let anyOptional = newValue as? AnyOptional,
anyOptional.isNil
{
container.removeObject(forKey: key)
} else {
container.set(newValue, forKey: key)
}
}
}
public init(wrappedValue: Value, key: String, container: UserDefaults) {
self.defaultValue = wrappedValue
self.container = container
self.key = key
}
}
protocol AnyOptional {
var isNil: Bool { get }
}
extension Optional: AnyOptional {
var isNil: Bool { self == nil }
}
|