Adapt store of the openFeedsInDefaultBrowser preference to be stored/retrieved from app group shared defaults, so that the Safari App Extension can access it.

This commit is contained in:
Daniel Jalkut 2021-06-23 14:39:02 -04:00
parent d2157a7c08
commit 8b447f6446
5 changed files with 31 additions and 14 deletions

View File

@ -107,12 +107,24 @@ final class AppDefaults {
}
}
// Special case for this default: store/retrieve it from the shared app group
// defaults, so that it can be resolved by the Safari App Extension.
var subscribeToFeedDefaults: UserDefaults {
if let appGroupID = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as? String,
let appGroupDefaults = UserDefaults(suiteName: appGroupID) {
return appGroupDefaults
}
else {
return UserDefaults.standard
}
}
var subscribeToFeedsInDefaultBrowser: Bool {
get {
return AppDefaults.bool(for: Key.subscribeToFeedsInDefaultBrowser)
return subscribeToFeedDefaults.bool(forKey: Key.subscribeToFeedsInDefaultBrowser)
}
set {
AppDefaults.setBool(for: Key.subscribeToFeedsInDefaultBrowser, newValue)
subscribeToFeedDefaults.set(newValue, forKey: Key.subscribeToFeedsInDefaultBrowser)
}
}
@ -297,7 +309,6 @@ final class AppDefaults {
Key.detailFontSize: FontSize.medium.rawValue,
Key.timelineSortDirection: ComparisonResult.orderedDescending.rawValue,
Key.timelineGroupByFeed: false,
Key.subscribeToFeedsInDefaultBrowser: false,
"NSScrollViewShouldScrollUnderTitlebar": false,
Key.refreshInterval: RefreshInterval.everyHour.rawValue,
Key.showDebugMenu: showDebugMenu]

View File

@ -190,7 +190,7 @@
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<binding destination="mAF-gO-1PI" name="value" keyPath="values.subscribeToFeedsInDefaultBrowser" id="rgT-1E-AoD">
<binding destination="iuH-lz-18x" name="value" keyPath="openFeedsInDefaultNewsReader" id="l1B-rk-qfE">
<dictionary key="options">
<string key="NSValueTransformerName">NSNegateBoolean</string>
</dictionary>
@ -204,7 +204,7 @@
<font key="font" metaFont="system"/>
</buttonCell>
<connections>
<binding destination="mAF-gO-1PI" name="value" keyPath="values.subscribeToFeedsInDefaultBrowser" id="Dnc-co-ppt"/>
<binding destination="iuH-lz-18x" name="value" keyPath="openFeedsInDefaultNewsReader" id="QZ4-W8-rPi"/>
</connections>
</button>
</subviews>

View File

@ -126,4 +126,12 @@ private extension GeneralPreferencesViewController {
}
}
@objc var openFeedsInDefaultNewsReader: Bool {
get {
return AppDefaults.shared.subscribeToFeedsInDefaultBrowser
}
set {
AppDefaults.shared.subscribeToFeedsInDefaultBrowser = newValue
}
}
}

View File

@ -8,6 +8,8 @@
<string>Subscribe to Feed</string>
<key>CFBundleExecutable</key>
<string>$(EXECUTABLE_NAME)</string>
<key>AppGroup</key>
<string>group.$(ORGANIZATION_IDENTIFIER).NetNewsWire-Evergreen</string>
<key>CFBundleIdentifier</key>
<string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
<key>CFBundleInfoDictionaryVersion</key>

View File

@ -45,15 +45,11 @@ class SafariExtensionHandler: SFSafariExtensionHandler {
if var feedURLString = userInfo?["url"] as? String {
var openInDefaultBrowser = false
// Ask for the user default from NetNewsWire's defaults to determine whether to open the feed URL
// using whatever the system configured default is, or to always hard-code it to NetNewsWire itself.
if let pluginBundleID = Bundle.main.bundleIdentifier {
// By convention we assume that our bundle ID will always be the same as the host app's, with
// the addition of ".Subscribe-to-Feed".
let hostAppBundleID = pluginBundleID.replacingOccurrences(of: ".Subscribe-to-Feed", with: "")
if let sharedDefaults = UserDefaults(suiteName: hostAppBundleID) {
openInDefaultBrowser = sharedDefaults.bool(forKey: "subscribeToFeedsInDefaultBrowser")
// Ask for the user's choice for whether to open the feed URL using whatever the system
// configured default is, or to always hard-code it to have NetNewsWire handle it itself.
if let appGroupID = Bundle.main.object(forInfoDictionaryKey: "AppGroup") as? String {
if let groupDefaults = UserDefaults(suiteName: appGroupID) {
openInDefaultBrowser = groupDefaults.bool(forKey: "subscribeToFeedsInDefaultBrowser")
}
}