-
Notifications
You must be signed in to change notification settings - Fork 425
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add toggling of NetP Notifications to iOS #2112
Merged
Merged
Changes from 19 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
21a255b
Add NotificationsView with just the settings link
graeme 44212a0
Add notifications authorization flow
graeme 1ed5878
Fix init typo
graeme b8565bf
Point to dev BSK commit
graeme 1454f88
Fix header
graeme e6653dd
Add NetP User defaults
graeme 0967bd2
NetP US project changes
graeme 57f896e
Inject notificationsPresenter w/ NetP defaults
graeme 778204e
Add authorized view with notifications toggling
graeme 062ddc6
Add animation
graeme 6280ca1
Fix issue with delegation
graeme 026091d
Fix threading issues
graeme e91dc80
Add an explanatory comment
graeme 22372eb
Fix issue where reload reverts to empty
graeme 5e6f49f
Add the netP group ID to main entitlements
graeme b25ec34
Fix animation bug on status view
graeme f6a7386
Add footer text for turning on notifications
graeme e26bb98
Point to latest BSK version
graeme d6f20cf
Merge branch 'develop' into graeme/ios-vpn-notification-settings
graeme e2d08fc
Move view model init into convenience init
graeme 691a990
Wrap the whole PTP in a NetP check to fix release
graeme f8e0161
Update DuckDuckGo/NetworkProtectionVPNNotificationsView.swift
graeme 303ccdd
Point to 82.1.0
graeme b146678
Package.resolved
graeme b3c8fb6
Merge remote-tracking branch 'origin/develop' into graeme/ios-vpn-not…
graeme File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// UserDefaults+NetworkProtection.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#if NETWORK_PROTECTION | ||
|
||
import Foundation | ||
|
||
public extension UserDefaults { | ||
static var networkProtectionGroupDefaults: UserDefaults { | ||
let suiteName = "\(Global.groupIdPrefix).netp" | ||
guard let defaults = UserDefaults(suiteName: suiteName) else { | ||
fatalError("Failed to create netP UserDefaults") | ||
} | ||
return defaults | ||
} | ||
} | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,9 +40,12 @@ struct NetworkProtectionStatusView: View { | |
} | ||
settings() | ||
} | ||
.animation(.default, value: statusModel.shouldShowError) | ||
.padding(.top, statusModel.error == nil ? 0 : -20) | ||
.animation(.default, value: statusModel.shouldShowConnectionDetails) | ||
.if(statusModel.animationsOn, transform: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The animation bug this fixes became a lot more noticeable with the addition of the new section. So I fixed it here. |
||
$0 | ||
.animation(.default, value: statusModel.shouldShowConnectionDetails) | ||
.animation(.default, value: statusModel.shouldShowError) | ||
}) | ||
.applyInsetGroupedListStyle() | ||
.navigationTitle(UserText.netPNavTitle) | ||
} | ||
|
@@ -134,7 +137,7 @@ struct NetworkProtectionStatusView: View { | |
NavigationLink(UserText.netPVPNSettingsTitle, destination: NetworkProtectionVPNSettingsView()) | ||
.font(.system(size: 16)) | ||
.foregroundColor(.textPrimary) | ||
NavigationLink(UserText.netPVPNNotificationsTitle, destination: Text("Coming soon!")) | ||
NavigationLink(UserText.netPVPNNotificationsTitle, destination: NetworkProtectionVPNNotificationsView()) | ||
.font(.system(size: 16)) | ||
.foregroundColor(.textPrimary) | ||
} header: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
// | ||
// NetworkProtectionVPNNotificationsView.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#if NETWORK_PROTECTION | ||
|
||
import SwiftUI | ||
import UIKit | ||
import NetworkProtection | ||
import Core | ||
|
||
@available(iOS 15, *) | ||
struct NetworkProtectionVPNNotificationsView: View { | ||
@StateObject var model = NetworkProtectionVPNNotificationsViewModel( | ||
notificationsAuthorization: NotificationsAuthorizationController(), | ||
notificationsSettingsStore: NetworkProtectionNotificationsSettingsUserDefaultsStore(userDefaults: .networkProtectionGroupDefaults) | ||
) | ||
|
||
var body: some View { | ||
List { | ||
switch model.viewKind { | ||
case .loading: | ||
EmptyView() | ||
case .unauthorized: | ||
unauthorizedView | ||
case .authorized: | ||
authorizedView | ||
} | ||
} | ||
.animation(.default, value: model.viewKind) | ||
.applyInsetGroupedListStyle() | ||
.navigationTitle(UserText.netPVPNNotificationsTitle).onAppear { | ||
Task { | ||
await model.onViewAppeared() | ||
} | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
private var unauthorizedView: some View { | ||
Section { | ||
Button(UserText.netPTurnOnNotificationsButtonTitle) { | ||
model.turnOnNotifications() | ||
} | ||
.foregroundColor(.controlColor) | ||
} footer: { | ||
Text(UserText.netPTurnOnNotificationsSectionFooter) | ||
.foregroundColor(.textSecondary) | ||
.font(.system(size: 13)) | ||
.padding(.top, 6) | ||
} | ||
} | ||
|
||
@ViewBuilder | ||
private var authorizedView: some View { | ||
Section { | ||
Toggle(UserText.netPVPNAlertsToggleTitle, isOn: Binding( | ||
get: { model.alertsEnabled }, | ||
set: model.didToggleAlerts(to:) | ||
)) | ||
.toggleStyle(SwitchToggleStyle(tint: Color(designSystemColor: .accent))) | ||
graeme marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} footer: { | ||
Text(UserText.netPVPNAlertsToggleSectionFooter) | ||
.foregroundColor(.textSecondary) | ||
.font(.system(size: 13)) | ||
.padding(.top, 6) | ||
} | ||
} | ||
} | ||
|
||
private extension Color { | ||
static let textPrimary = Color(designSystemColor: .textPrimary) | ||
static let textSecondary = Color(designSystemColor: .textSecondary) | ||
static let cellBackground = Color(designSystemColor: .surface) | ||
static let controlColor = Color(designSystemColor: .accent) | ||
} | ||
|
||
#endif |
79 changes: 79 additions & 0 deletions
79
DuckDuckGo/NetworkProtectionVPNNotificationsViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// NetworkProtectionVPNNotificationsViewModel.swift | ||
// DuckDuckGo | ||
// | ||
// Copyright © 2023 DuckDuckGo. All rights reserved. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
// | ||
|
||
#if NETWORK_PROTECTION | ||
|
||
import Combine | ||
import UserNotifications | ||
import NetworkProtection | ||
|
||
enum NetworkProtectionNotificationsViewKind: Equatable { | ||
case loading | ||
case unauthorized | ||
case authorized | ||
} | ||
|
||
final class NetworkProtectionVPNNotificationsViewModel: ObservableObject { | ||
private var notificationsAuthorization: NotificationsAuthorizationControlling | ||
private var notificationsSettingsStore: NetworkProtectionNotificationsSettingsStore | ||
@Published var viewKind: NetworkProtectionNotificationsViewKind = .loading | ||
var alertsEnabled: Bool { | ||
self.notificationsSettingsStore.alertsEnabled | ||
} | ||
|
||
init(notificationsAuthorization: NotificationsAuthorizationControlling, | ||
notificationsSettingsStore: NetworkProtectionNotificationsSettingsStore) { | ||
self.notificationsAuthorization = notificationsAuthorization | ||
self.notificationsSettingsStore = notificationsSettingsStore | ||
self.notificationsAuthorization.delegate = self | ||
} | ||
|
||
@MainActor | ||
func onViewAppeared() async { | ||
let status = await notificationsAuthorization.authorizationStatus | ||
updateViewKind(for: status) | ||
} | ||
|
||
func turnOnNotifications() { | ||
notificationsAuthorization.requestAlertAuthorization() | ||
} | ||
|
||
func didToggleAlerts(to enabled: Bool) { | ||
notificationsSettingsStore.alertsEnabled = enabled | ||
} | ||
|
||
private func updateViewKind(for authorizationStatus: UNAuthorizationStatus) { | ||
switch authorizationStatus { | ||
case .notDetermined, .denied: | ||
viewKind = .unauthorized | ||
case .authorized, .ephemeral, .provisional: | ||
viewKind = .authorized | ||
@unknown default: | ||
assertionFailure("Unhandled enum case") | ||
} | ||
} | ||
} | ||
|
||
extension NetworkProtectionVPNNotificationsViewModel: NotificationsPermissionsControllerDelegate { | ||
func authorizationStateDidChange(toStatus status: UNAuthorizationStatus) { | ||
updateViewKind(for: status) | ||
} | ||
} | ||
|
||
#endif |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice, I suspect I'll be using this for the widget - thanks!