Remove store value when set value is nil

Fixes crash on removing active profile when current, due to setting
nil activeProfileId in UserDefaults.
This commit is contained in:
Davide De Rosa 2022-07-04 19:08:16 +02:00
parent ebe8ae3d29
commit 3e8a49c970
2 changed files with 7 additions and 3 deletions

View File

@ -40,7 +40,7 @@ extension KeyStoreDomainLocation {
} }
public protocol KeyValueStore { public protocol KeyValueStore {
func setValue<L: KeyStoreLocation, V>(_ value: V, forLocation location: L) func setValue<L: KeyStoreLocation, V>(_ value: V?, forLocation location: L)
func value<L: KeyStoreLocation, V>(forLocation location: L) -> V? func value<L: KeyStoreLocation, V>(forLocation location: L) -> V?

View File

@ -32,7 +32,11 @@ public struct UserDefaultsStore: KeyValueStore {
self.defaults = defaults self.defaults = defaults
} }
public func setValue<L: KeyStoreLocation, V>(_ value: V, forLocation location: L) { public func setValue<L: KeyStoreLocation, V>(_ value: V?, forLocation location: L) {
guard let value = value else {
defaults.removeObject(forKey: location.key)
return
}
defaults.setValue(value, forKey: location.key) defaults.setValue(value, forKey: location.key)
} }