From 748eb8c40b921836b721f59ad7d614b6b4e09eb0 Mon Sep 17 00:00:00 2001 From: Ethan Dickson Date: Thu, 8 May 2025 14:38:13 +1000 Subject: [PATCH] chore: set 'stop VPN on quit' setting to true by default --- Coder-Desktop/Coder-Desktop/State.swift | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) 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) + } +}