Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
diegoreymendez committed Mar 22, 2024
1 parent d837b0c commit bfdd73b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 20 deletions.
2 changes: 1 addition & 1 deletion DuckDuckGo/NavigationBar/View/MoreOptionsMenu.swift
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ final class MoreOptionsMenu: NSMenu {
isMenuItemEnabled = false
}

networkProtectionItem.isHidden = !isMenuItemEnabled
networkProtectionItem.isEnabled = !isMenuItemEnabled
}
}
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,32 +48,47 @@ final class NetworkProtectionSubscriptionEventHandler {
}

private func subscribeToEntitlementChanges() {
NotificationCenter.default
.publisher(for: .entitlementsDidChange)
.receive(on: DispatchQueue.main)
.sink { [weak self] notification in
guard let self else {
return
}
Task {
switch await AccountManager().hasEntitlement(for: .networkProtection) {
case .success(let hasEntitlements):
handleEntitlementsChange(hasEntitlements: hasEntitlements)
case .failure(let error):

Check warning on line 55 in DuckDuckGo/NetworkProtection/AppTargets/DeveloperIDTarget/NetworkProtectionSubscriptionEventHandler.swift

View workflow job for this annotation

GitHub Actions / Test (Non-Sandbox)

immutable value 'error' was never used; consider replacing with '_' or removing it

Check warning on line 55 in DuckDuckGo/NetworkProtection/AppTargets/DeveloperIDTarget/NetworkProtectionSubscriptionEventHandler.swift

View workflow job for this annotation

GitHub Actions / Make Release Build (DuckDuckGo Privacy Browser)

immutable value 'error' was never used; consider replacing with '_' or removing it

Check warning on line 55 in DuckDuckGo/NetworkProtection/AppTargets/DeveloperIDTarget/NetworkProtectionSubscriptionEventHandler.swift

View workflow job for this annotation

GitHub Actions / Make Release Build (DuckDuckGo Privacy Pro)

immutable value 'error' was never used; consider replacing with '_' or removing it
break
}

guard let entitlements = notification.userInfo?[UserDefaultsCacheKey.subscriptionEntitlements] as? [Entitlement] else {
NotificationCenter.default
.publisher(for: .entitlementsDidChange)
.receive(on: DispatchQueue.main)
.sink { [weak self] notification in
guard let self else {
return
}

assertionFailure("Missing entitlements are truly unexpected")
return
}
guard let entitlements = notification.userInfo?[UserDefaultsCacheKey.subscriptionEntitlements] as? [Entitlement] else {

let entitlementsExpired = !entitlements.contains { entitlement in
entitlement.product == .networkProtection
}
assertionFailure("Missing entitlements are truly unexpected")
return
}

if entitlementsExpired {
Task {
await self.networkProtectionFeatureDisabler.disable(keepAuthToken: false, uninstallSystemExtension: false)
UserDefaults.netP.networkProtectionEntitlementsExpired = true
let hasEntitlements = entitlements.contains { entitlement in
entitlement.product == .networkProtection
}

handleEntitlementsChange(hasEntitlements: hasEntitlements)
}
.store(in: &cancellables)
}
}

private func handleEntitlementsChange(hasEntitlements: Bool) {
if hasEntitlements {
UserDefaults.netP.networkProtectionEntitlementsExpired = false
} else {
Task {
await self.networkProtectionFeatureDisabler.disable(keepAuthToken: false, uninstallSystemExtension: false)
UserDefaults.netP.networkProtectionEntitlementsExpired = true
}
.store(in: &cancellables)
}
}

func registerForSubscriptionAccountManagerEvents() {
Expand Down

0 comments on commit bfdd73b

Please sign in to comment.