Skip to content

chore: set 'stop VPN on quit' setting to true by default #155

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion Coder-Desktop/Coder-Desktop/State.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this API doesn't already just return a Bool?. It is quite silly.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image I'm also not the first to do this...

}
}
Loading