Skip to content

Commit

Permalink
Add CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication)
Browse files Browse the repository at this point in the history
  • Loading branch information
robertherber committed Jan 10, 2025
1 parent 4f402b3 commit 1f4fab9
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ class DeviceActivityMonitorExtension: DeviceActivityMonitor {
callbackName: "intervalDidEnd"
)

CFPreferencesAppSynchronize(kCFPreferencesCurrentApplication)

self.notifyAppWithName(name: "intervalDidEnd")

self.executeActionsForEvent(activityName: activity.rawValue, callbackName: "intervalDidEnd")
Expand Down
5 changes: 5 additions & 0 deletions targets/ShieldAction/ShieldActionExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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] {
Expand Down Expand Up @@ -89,6 +91,7 @@ class ShieldActionExtension: ShieldActionDelegate {
completionHandler: @escaping (ShieldActionResponse) -> Void
) {
logger.log("handle application")

handleAction(
action: action,
completionHandler: completionHandler,
Expand All @@ -102,6 +105,7 @@ class ShieldActionExtension: ShieldActionDelegate {
completionHandler: @escaping (ShieldActionResponse) -> Void
) {
logger.log("handle domain")

handleAction(
action: action,
completionHandler: completionHandler,
Expand All @@ -115,6 +119,7 @@ class ShieldActionExtension: ShieldActionDelegate {
completionHandler: @escaping (ShieldActionResponse) -> Void
) {
logger.log("handle category")

handleAction(
action: action,
completionHandler: completionHandler,
Expand Down
102 changes: 46 additions & 56 deletions targets/ShieldConfiguration/ShieldConfigurationExtension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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)
Expand All @@ -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 {
Expand All @@ -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)
Expand All @@ -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)
}
}

0 comments on commit 1f4fab9

Please sign in to comment.