Skip to content

Commit 22caef3

Browse files
authored
Open NetP status view from notifications (duckduckgo#2053)
Task/Issue URL: https://app.asana.com/0/0/1205614408194312/f Description: As part of iOS: Network Protection Notifications, we should make sure the Network Protection Status view is opened when tapping a notification triggered by the extension.
1 parent 9028834 commit 22caef3

File tree

6 files changed

+65
-13
lines changed

6 files changed

+65
-13
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// NetworkProtectionNotificationIdentifier.swift
3+
// DuckDuckGo
4+
//
5+
// Copyright © 2023 DuckDuckGo. All rights reserved.
6+
//
7+
// Licensed under the Apache License, Version 2.0 (the "License");
8+
// you may not use this file except in compliance with the License.
9+
// You may obtain a copy of the License at
10+
//
11+
// http://www.apache.org/licenses/LICENSE-2.0
12+
//
13+
// Unless required by applicable law or agreed to in writing, software
14+
// distributed under the License is distributed on an "AS IS" BASIS,
15+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
// See the License for the specific language governing permissions and
17+
// limitations under the License.
18+
//
19+
20+
import Foundation
21+
22+
public enum NetworkProtectionNotificationIdentifier: String {
23+
case reconnecting = "network-protection.notification.reconnecting"
24+
case reconnected = "network-protection.notification.reconnected"
25+
case connectionFailure = "network-protection.notification.connection-failure"
26+
case superseded = "network-protection.notification.superseded"
27+
case test = "network-protection.notification.test"
28+
}

DuckDuckGo.xcodeproj/project.pbxproj

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -759,6 +759,7 @@
759759
EE50053029C3BA0800AE0773 /* InternalUserStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE50052F29C3BA0800AE0773 /* InternalUserStore.swift */; };
760760
EE72CA852A862D000043B5B3 /* NetworkProtectionDebugViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE72CA842A862D000043B5B3 /* NetworkProtectionDebugViewController.swift */; };
761761
EE7917912A83DE93008DFF28 /* CombineTestUtilities.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE7917902A83DE93008DFF28 /* CombineTestUtilities.swift */; };
762+
EE7A92872AC6DE4700832A36 /* NetworkProtectionNotificationIdentifier.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE7A92862AC6DE4700832A36 /* NetworkProtectionNotificationIdentifier.swift */; };
762763
EE8594992A44791C008A6D06 /* NetworkProtectionTunnelController.swift in Sources */ = {isa = PBXBuildFile; fileRef = EE8594982A44791C008A6D06 /* NetworkProtectionTunnelController.swift */; };
763764
EE8E568A2A56BCE400F11DCA /* NetworkProtection in Frameworks */ = {isa = PBXBuildFile; productRef = EE8E56892A56BCE400F11DCA /* NetworkProtection */; };
764765
EEEB80A32A421CE600386378 /* NetworkProtectionPacketTunnelProvider.swift in Sources */ = {isa = PBXBuildFile; fileRef = EEEB80A22A421CE600386378 /* NetworkProtectionPacketTunnelProvider.swift */; };
@@ -2349,6 +2350,7 @@
23492350
EE50052F29C3BA0800AE0773 /* InternalUserStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InternalUserStore.swift; sourceTree = "<group>"; };
23502351
EE72CA842A862D000043B5B3 /* NetworkProtectionDebugViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkProtectionDebugViewController.swift; sourceTree = "<group>"; };
23512352
EE7917902A83DE93008DFF28 /* CombineTestUtilities.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CombineTestUtilities.swift; sourceTree = "<group>"; };
2353+
EE7A92862AC6DE4700832A36 /* NetworkProtectionNotificationIdentifier.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkProtectionNotificationIdentifier.swift; sourceTree = "<group>"; };
23522354
EE8594982A44791C008A6D06 /* NetworkProtectionTunnelController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkProtectionTunnelController.swift; sourceTree = "<group>"; };
23532355
EEB8FDB92A990AEE00EBEDCF /* Configuration-Alpha.xcconfig */ = {isa = PBXFileReference; lastKnownFileType = text.xcconfig; name = "Configuration-Alpha.xcconfig"; path = "Configuration/Configuration-Alpha.xcconfig"; sourceTree = "<group>"; };
23542356
EEEB80A22A421CE600386378 /* NetworkProtectionPacketTunnelProvider.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NetworkProtectionPacketTunnelProvider.swift; sourceTree = "<group>"; };
@@ -4394,6 +4396,14 @@
43944396
name = NetworkProtection;
43954397
sourceTree = "<group>";
43964398
};
4399+
EE7A92852AC6DE2500832A36 /* NetworkProtection */ = {
4400+
isa = PBXGroup;
4401+
children = (
4402+
EE7A92862AC6DE4700832A36 /* NetworkProtectionNotificationIdentifier.swift */,
4403+
);
4404+
name = NetworkProtection;
4405+
sourceTree = "<group>";
4406+
};
43974407
EECD94B22A28B8580085C66E /* NetworkProtection */ = {
43984408
isa = PBXGroup;
43994409
children = (
@@ -4669,6 +4679,7 @@
46694679
F143C2E51E4A4CD400CFDE3A /* Core */ = {
46704680
isa = PBXGroup;
46714681
children = (
4682+
EE7A92852AC6DE2500832A36 /* NetworkProtection */,
46724683
4B470ED4299C484B0086EBDC /* AppTrackingProtection */,
46734684
F1CE42A71ECA0A520074A8DF /* Bookmarks */,
46744685
837774491F8E1ECE00E17A29 /* ContentBlocker */,
@@ -6693,6 +6704,7 @@
66936704
85F21DC621145DD5002631A6 /* global.swift in Sources */,
66946705
F41C2DA326C1925700F9A760 /* BookmarksAndFolders.xcdatamodeld in Sources */,
66956706
F4F6DFBA26EFF28A00ED7E12 /* BookmarkObjects.swift in Sources */,
6707+
EE7A92872AC6DE4700832A36 /* NetworkProtectionNotificationIdentifier.swift in Sources */,
66966708
836A941D247F23C600BF8EF5 /* UserAgentManager.swift in Sources */,
66976709
4B83397329AFB8D2003F7EA9 /* AppTrackingProtectionFeedbackModel.swift in Sources */,
66986710
85CA53A824BB343700A6288C /* Favicons.swift in Sources */,

DuckDuckGo/AppDelegate.swift

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -677,9 +677,16 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
677677
didReceive response: UNNotificationResponse,
678678
withCompletionHandler completionHandler: @escaping () -> Void) {
679679
if response.actionIdentifier == UNNotificationDefaultActionIdentifier {
680-
if response.notification.request.identifier == WindowsBrowserWaitlist.notificationIdentitier {
680+
let identifier = response.notification.request.identifier
681+
if identifier == WindowsBrowserWaitlist.notificationIdentitier {
681682
presentWindowsBrowserWaitlistSettingsModal()
682683
}
684+
685+
#if NETWORK_PROTECTION
686+
if NetworkProtectionNotificationIdentifier(rawValue: identifier) != nil {
687+
presentNetworkProtectionStatusSettingsModal()
688+
}
689+
#endif
683690
}
684691

685692
completionHandler()
@@ -689,7 +696,14 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
689696
let waitlistViewController = WindowsWaitlistViewController(nibName: nil, bundle: nil)
690697
presentSettings(with: waitlistViewController)
691698
}
692-
699+
700+
#if NETWORK_PROTECTION
701+
private func presentNetworkProtectionStatusSettingsModal() {
702+
let networkProtectionRoot = NetworkProtectionRootViewController()
703+
presentSettings(with: networkProtectionRoot)
704+
}
705+
#endif
706+
693707
private func presentSettings(with viewController: UIViewController) {
694708
guard let window = window, let rootViewController = window.rootViewController as? MainViewController else { return }
695709

@@ -703,5 +717,4 @@ extension AppDelegate: UNUserNotificationCenterDelegate {
703717
navigationController?.pushViewController(viewController, animated: true)
704718
}
705719
}
706-
707720
}

DuckDuckGo/NetworkProtectionRootViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import SwiftUI
2323

2424
final class NetworkProtectionRootViewController: UIHostingController<NetworkProtectionRootView> {
2525

26-
init(inviteCompletion: @escaping () -> Void) {
26+
init(inviteCompletion: @escaping () -> Void = { }) {
2727
let rootView = NetworkProtectionRootView(inviteCompletion: inviteCompletion)
2828
super.init(rootView: rootView)
2929
}

DuckDuckGo/SettingsViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -420,7 +420,7 @@ class SettingsViewController: UITableViewController {
420420
// This will be tidied up as part of https://app.asana.com/0/0/1205084446087078/f
421421
let rootViewController = NetworkProtectionRootViewController { [weak self] in
422422
self?.navigationController?.popViewController(animated: true)
423-
let newRootViewController = NetworkProtectionRootViewController { }
423+
let newRootViewController = NetworkProtectionRootViewController()
424424
self?.pushNetP(newRootViewController)
425425
}
426426
pushNetP(rootViewController)

PacketTunnelProvider/NetworkProtection/NetworkProtectionUNNotificationPresenter.swift

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import UIKit
2121
import NetworkProtection
22+
import Core
2223

2324
/// This class takes care of requesting the presentation of notifications using UNNotificationCenter
2425
///
@@ -72,29 +73,29 @@ final class NetworkProtectionUNNotificationPresenter: NSObject, NetworkProtectio
7273
func showTestNotification() {
7374
// Debug only string. Doesn't need localized
7475
let content = notificationContent(body: "Test notification")
75-
showNotification(content)
76+
showNotification(.test, content)
7677
}
7778

7879
func showReconnectedNotification() {
7980
let content = notificationContent(body: UserText.networkProtectionConnectionSuccessNotificationBody)
80-
showNotification(content)
81+
showNotification(.reconnected, content)
8182
}
8283

8384
func showReconnectingNotification() {
8485
let content = notificationContent(body: UserText.networkProtectionConnectionInterruptedNotificationBody)
85-
showNotification(content)
86+
showNotification(.reconnecting, content)
8687
}
8788

8889
func showConnectionFailureNotification() {
8990
let content = notificationContent(body: UserText.networkProtectionConnectionFailureNotificationBody)
90-
showNotification(content)
91+
showNotification(.connectionFailure, content)
9192
}
9293

9394
func showSupersededNotification() {
9495
}
9596

96-
private func showNotification(_ content: UNNotificationContent) {
97-
let request = UNNotificationRequest(identifier: UUID().uuidString, content: content, trigger: .none)
97+
private func showNotification(_ identifier: NetworkProtectionNotificationIdentifier, _ content: UNNotificationContent) {
98+
let request = UNNotificationRequest(identifier: identifier.rawValue, content: content, trigger: .none)
9899

99100
requestAlertAuthorization { authorized in
100101
guard authorized else {
@@ -107,9 +108,7 @@ final class NetworkProtectionUNNotificationPresenter: NSObject, NetworkProtectio
107108
}
108109

109110
extension NetworkProtectionUNNotificationPresenter: UNUserNotificationCenterDelegate {
110-
111111
func userNotificationCenter(_ center: UNUserNotificationCenter, willPresent notification: UNNotification) async -> UNNotificationPresentationOptions {
112112
return .banner
113113
}
114-
115114
}

0 commit comments

Comments
 (0)