Skip to content

Commit

Permalink
Fix crash tapping external links with an old browser setting (#1189)
Browse files Browse the repository at this point in the history
  • Loading branch information
nolanw committed Jun 19, 2024
1 parent b256de1 commit 4d7a20f
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions AwfulSettings/Sources/AwfulSettings/Settings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public enum Settings {
public static let darkMode = Setting(key: "dark_theme", default: false)

/// Which app to use for opening URLs.
public static let defaultBrowser = Setting(key: "default_browser", default: DefaultBrowser.awful)
public static let defaultBrowser = Setting(key: "default_browser", default: DefaultBrowser.default)

/// The theme to use by default when dark mode is on.
public static let defaultDarkThemeName = Setting(
Expand Down Expand Up @@ -147,7 +147,7 @@ public enum BuiltInTheme: String, UserDefaultsSerializable {
}

/// The default browser set by the user via `UserDefaults` and `Settings.defaultBrowser`.
public enum DefaultBrowser: String, CaseIterable, UserDefaultsSerializable {
public enum DefaultBrowser: String, CaseIterable {
// These raw values are persisted in user defaults, so don't change them willy nilly.
case awful = "Awful"
case defaultiOSBrowser = "Default iOS Browser"
Expand All @@ -156,6 +156,8 @@ public enum DefaultBrowser: String, CaseIterable, UserDefaultsSerializable {
case edge = "Edge"
case firefox = "Firefox"

static var `default`: Self { .awful }

/// Passing the returned URL to `UIApplication.canOpenURL(_:)` indicates whether the browser is available. When the returned URL is `nil`, it's always available.
public var checkCanOpenURL: URL? {
switch self {
Expand All @@ -167,3 +169,10 @@ public enum DefaultBrowser: String, CaseIterable, UserDefaultsSerializable {
}
}
}

extension DefaultBrowser: UserDefaultsSerializable {
public init(storedValue: String) {
// Handle browsers that have disappeared from the list by falling back to the default default browser.
self = Self.init(rawValue: storedValue) ?? .default
}
}

0 comments on commit 4d7a20f

Please sign in to comment.