diff --git a/Coder-Desktop/Coder-Desktop/State.swift b/Coder-Desktop/Coder-Desktop/State.swift index e9a0248..902d5a1 100644 --- a/Coder-Desktop/Coder-Desktop/State.swift +++ b/Coder-Desktop/Coder-Desktop/State.swift @@ -55,7 +55,8 @@ class AppState: ObservableObject { } } - @Published var stopVPNOnQuit: Bool = UserDefaults.standard.bool(forKey: Keys.stopVPNOnQuit) { + // Defaults to `true` + @Published var stopVPNOnQuit: Bool = UserDefaults.standard.optionalBool(forKey: Keys.stopVPNOnQuit) ?? true { didSet { guard persistent else { return } UserDefaults.standard.set(stopVPNOnQuit, forKey: Keys.stopVPNOnQuit) @@ -239,3 +240,14 @@ extension LiteralHeader { .init(name: name, value: value) } } + +extension UserDefaults { + // Unlike the exisitng `bool(forKey:)` method which returns `false` for both + // missing values this method can return `nil`. + func optionalBool(forKey key: String) -> Bool? { + guard object(forKey: key) != nil else { + return nil + } + return bool(forKey: key) + } +}