diff --git a/src/logic/Preferences.swift b/src/logic/Preferences.swift index 9b770e00..90e80814 100644 --- a/src/logic/Preferences.swift +++ b/src/logic/Preferences.swift @@ -129,7 +129,7 @@ class Preferences { static var hideWindowlessApps: Bool { defaults.bool("hideWindowlessApps") } // periphery:ignore static var startAtLogin: Bool { defaults.bool("startAtLogin") } - static var blacklist: [BlacklistEntry] { jsonDecode([BlacklistEntry].self, defaults.string("blacklist")) } + static var blacklist: [BlacklistEntry] { defaults.json("blacklist", [BlacklistEntry].self) } static var previewFocusedWindow: Bool { defaults.bool("previewFocusedWindow") } static var screenRecordingPermissionSkipped: Bool { defaults.bool("screenRecordingPermissionSkipped") } @@ -424,10 +424,6 @@ class Preferences { }) } - static func jsonDecode(_ type: T.Type, _ value: String) -> T where T: Decodable { - return try! JSONDecoder().decode(type, from: value.data(using: .utf8)!) - } - static func jsonEncode(_ value: T) -> String where T: Encodable { return String(data: try! JSONEncoder().encode(value), encoding: .utf8)! } @@ -1075,10 +1071,10 @@ enum CrashPolicyPreference: CaseIterable, MacroPreference { } } -enum BlacklistHidePreference: CaseIterable, MacroPreference, Codable { - case none - case always - case whenNoOpenWindow +enum BlacklistHidePreference: String/* required for jsonEncode */, CaseIterable, MacroPreference, Codable { + case none = "0" + case always = "1" + case whenNoOpenWindow = "2" var localizedString: LocalizedString { switch self { @@ -1089,10 +1085,10 @@ enum BlacklistHidePreference: CaseIterable, MacroPreference, Codable { } } -enum BlacklistIgnorePreference: CaseIterable, MacroPreference, Codable { - case none - case always - case whenFullscreen +enum BlacklistIgnorePreference: String/* required for jsonEncode */, CaseIterable, MacroPreference, Codable { + case none = "0" + case always = "1" + case whenFullscreen = "2" var localizedString: LocalizedString { switch self { @@ -1152,4 +1148,12 @@ extension UserDefaults { func macroPref(_ key: String, _ macroPreferences: [A]) -> A { return getThenConvertOrReset(key, { s in Int(s).flatMap { macroPreferences[safe: $0] } }) } + + func json(_ key: String, _ type: T.Type) -> T where T: Decodable { + return getThenConvertOrReset(key, { s in jsonDecode(s, type) }) + } + + private func jsonDecode(_ value: String, _ type: T.Type) -> T? where T: Decodable { + return value.data(using: .utf8).flatMap { try? JSONDecoder().decode(type, from: $0) } + } }