From 1f4fab9d5203f64c449c23c1532e89c365453e1d Mon Sep 17 00:00:00 2001 From: Robert Herber Date: Fri, 10 Jan 2025 13:46:22 +0100 Subject: [PATCH] Add CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication) --- .../DeviceActivityMonitorExtension.swift | 2 + .../ShieldAction/ShieldActionExtension.swift | 5 + .../ShieldConfigurationExtension.swift | 102 ++++++++---------- 3 files changed, 53 insertions(+), 56 deletions(-) diff --git a/targets/ActivityMonitorExtension/DeviceActivityMonitorExtension.swift b/targets/ActivityMonitorExtension/DeviceActivityMonitorExtension.swift index c43965a..684e89d 100644 --- a/targets/ActivityMonitorExtension/DeviceActivityMonitorExtension.swift +++ b/targets/ActivityMonitorExtension/DeviceActivityMonitorExtension.swift @@ -47,6 +47,8 @@ class DeviceActivityMonitorExtension: DeviceActivityMonitor { callbackName: "intervalDidEnd" ) + CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication) + self.notifyAppWithName(name: "intervalDidEnd") self.executeActionsForEvent(activityName: activity.rawValue, callbackName: "intervalDidEnd") diff --git a/targets/ShieldAction/ShieldActionExtension.swift b/targets/ShieldAction/ShieldActionExtension.swift index 2749e0e..970e1c9 100644 --- a/targets/ShieldAction/ShieldActionExtension.swift +++ b/targets/ShieldAction/ShieldActionExtension.swift @@ -57,6 +57,8 @@ func handleAction( applicationToken: ApplicationToken?, webdomainToken: WebDomainToken? ) { + CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication) + if let shieldActionConfig = userDefaults?.dictionary(forKey: "shieldActions") { if let configForSelectedAction = shieldActionConfig[ action == .primaryButtonPressed ? "primary" : "secondary"] as? [String: Any] { @@ -89,6 +91,7 @@ class ShieldActionExtension: ShieldActionDelegate { completionHandler: @escaping (ShieldActionResponse) -> Void ) { logger.log("handle application") + handleAction( action: action, completionHandler: completionHandler, @@ -102,6 +105,7 @@ class ShieldActionExtension: ShieldActionDelegate { completionHandler: @escaping (ShieldActionResponse) -> Void ) { logger.log("handle domain") + handleAction( action: action, completionHandler: completionHandler, @@ -115,6 +119,7 @@ class ShieldActionExtension: ShieldActionDelegate { completionHandler: @escaping (ShieldActionResponse) -> Void ) { logger.log("handle category") + handleAction( action: action, completionHandler: completionHandler, diff --git a/targets/ShieldConfiguration/ShieldConfigurationExtension.swift b/targets/ShieldConfiguration/ShieldConfigurationExtension.swift index 19f481d..3a47abb 100644 --- a/targets/ShieldConfiguration/ShieldConfigurationExtension.swift +++ b/targets/ShieldConfiguration/ShieldConfigurationExtension.swift @@ -52,45 +52,50 @@ func resolveIcon(dict: [String: Any]) -> UIImage? { return image } -func getShieldConfiguration(config: [String: Any], placeholders: [String: String?]) +func getShieldConfiguration(placeholders: [String: String?]) -> ShieldConfiguration { + logger.log("Calling getShieldConfiguration") - let backgroundColor = getColor(color: config["backgroundColor"] as? [String: Double]) - - let title = config["title"] as? String - let titleColor = getColor(color: config["titleColor"] as? [String: Double]) - - let subtitle = config["subtitle"] as? String - let subtitleColor = getColor(color: config["subtitleColor"] as? [String: Double]) - - let primaryButtonLabel = config["primaryButtonLabel"] as? String - let primaryButtonLabelColor = getColor( - color: config["primaryButtonLabelColor"] as? [String: Double]) - let primaryButtonBackgroundColor = getColor( - color: config["primaryButtonBackgroundColor"] as? [String: Double]) - - let secondaryButtonLabel = config["secondaryButtonLabel"] as? String - let secondaryButtonLabelColor = getColor( - color: config["secondaryButtonLabelColor"] as? [String: Double] - ) - - let shield = ShieldConfiguration( - backgroundBlurStyle: config["backgroundBlurStyle"] != nil - ? UIBlurEffect.Style.init(rawValue: config["backgroundBlurStyle"] as! Int) : nil, - backgroundColor: backgroundColor, - icon: resolveIcon(dict: config), - title: buildLabel(text: title, with: titleColor, placeholders: placeholders), - subtitle: buildLabel(text: subtitle, with: subtitleColor, placeholders: placeholders), - primaryButtonLabel: buildLabel( - text: primaryButtonLabel, with: primaryButtonLabelColor, placeholders: placeholders), - primaryButtonBackgroundColor: primaryButtonBackgroundColor, - secondaryButtonLabel: buildLabel( - text: secondaryButtonLabel, with: secondaryButtonLabelColor, placeholders: placeholders) - ) - logger.log("shield initialized") - - return shield + if let config = userDefaults?.dictionary(forKey: "shieldConfiguration") { + let backgroundColor = getColor(color: config["backgroundColor"] as? [String: Double]) + + let title = config["title"] as? String + let titleColor = getColor(color: config["titleColor"] as? [String: Double]) + + let subtitle = config["subtitle"] as? String + let subtitleColor = getColor(color: config["subtitleColor"] as? [String: Double]) + + let primaryButtonLabel = config["primaryButtonLabel"] as? String + let primaryButtonLabelColor = getColor( + color: config["primaryButtonLabelColor"] as? [String: Double]) + let primaryButtonBackgroundColor = getColor( + color: config["primaryButtonBackgroundColor"] as? [String: Double]) + + let secondaryButtonLabel = config["secondaryButtonLabel"] as? String + let secondaryButtonLabelColor = getColor( + color: config["secondaryButtonLabelColor"] as? [String: Double] + ) + + let shield = ShieldConfiguration( + backgroundBlurStyle: config["backgroundBlurStyle"] != nil + ? UIBlurEffect.Style.init(rawValue: config["backgroundBlurStyle"] as! Int) : nil, + backgroundColor: backgroundColor, + icon: resolveIcon(dict: config), + title: buildLabel(text: title, with: titleColor, placeholders: placeholders), + subtitle: buildLabel(text: subtitle, with: subtitleColor, placeholders: placeholders), + primaryButtonLabel: buildLabel( + text: primaryButtonLabel, with: primaryButtonLabelColor, placeholders: placeholders), + primaryButtonBackgroundColor: primaryButtonBackgroundColor, + secondaryButtonLabel: buildLabel( + text: secondaryButtonLabel, with: secondaryButtonLabelColor, placeholders: placeholders) + ) + logger.log("shield initialized") + + return shield + } + + return ShieldConfiguration() } // Override the functions below to customize the shields used in various situations. @@ -111,11 +116,7 @@ class ShieldConfigurationExtension: ShieldConfigurationDataSource { ) ] - if let config = userDefaults?.dictionary(forKey: "shieldConfiguration") { - return getShieldConfiguration(config: config, placeholders: placeholders) - } - - return ShieldConfiguration() + return getShieldConfiguration(placeholders: placeholders) } override func configuration(shielding application: Application, in category: ActivityCategory) @@ -134,11 +135,9 @@ class ShieldConfigurationExtension: ShieldConfigurationDataSource { ) ] - if let dict = userDefaults?.dictionary(forKey: "shieldConfiguration") { - return getShieldConfiguration(config: dict, placeholders: placeholders) - } + CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication) - return ShieldConfiguration() + return getShieldConfiguration(placeholders: placeholders) } override func configuration(shielding webDomain: WebDomain) -> ShieldConfiguration { @@ -155,12 +154,7 @@ class ShieldConfigurationExtension: ShieldConfigurationDataSource { ) ] - // Customize the shield as needed for web domains. - if let config = userDefaults?.dictionary(forKey: "shieldConfiguration") { - return getShieldConfiguration(config: config, placeholders: placeholders) - } - - return ShieldConfiguration() + return getShieldConfiguration(placeholders: placeholders) } override func configuration(shielding webDomain: WebDomain, in category: ActivityCategory) @@ -179,10 +173,6 @@ class ShieldConfigurationExtension: ShieldConfigurationDataSource { ) ] - if let config = userDefaults?.dictionary(forKey: "shieldConfiguration") { - return getShieldConfiguration(config: config, placeholders: placeholders) - } - - return ShieldConfiguration() + return getShieldConfiguration(placeholders: placeholders) } }