From d1bc47eaaab0082055dac35b670983294cfeed99 Mon Sep 17 00:00:00 2001 From: mojganii Date: Tue, 28 Jan 2025 12:31:23 +0100 Subject: [PATCH] Add priority for notifications --- .../AccountExpiryInAppNotificationProvider.swift | 4 ++++ .../AccountExpirySystemNotificationProvider.swift | 4 ++++ .../LatestChangesNotificationProvider.swift | 4 ++++ .../NewDeviceNotificationProvider.swift | 4 ++++ .../TunnelStatusNotificationProvider.swift | 4 ++++ ios/MullvadVPN/Notifications/NotificationManager.swift | 2 +- .../Notifications/NotificationProvider.swift | 7 +++++++ .../Notifications/NotificationProviderIdentifier.swift | 10 ++++++++++ .../Notifications/NotificationProviderProtocol.swift | 4 ++++ 9 files changed, 42 insertions(+), 1 deletion(-) diff --git a/ios/MullvadVPN/Notifications/Notification Providers/AccountExpiryInAppNotificationProvider.swift b/ios/MullvadVPN/Notifications/Notification Providers/AccountExpiryInAppNotificationProvider.swift index cbe95c1ce629..b0084bee5403 100644 --- a/ios/MullvadVPN/Notifications/Notification Providers/AccountExpiryInAppNotificationProvider.swift +++ b/ios/MullvadVPN/Notifications/Notification Providers/AccountExpiryInAppNotificationProvider.swift @@ -39,6 +39,10 @@ final class AccountExpiryInAppNotificationProvider: NotificationProvider, InAppN .accountExpiryInAppNotification } + override var priority: NotificationPriority { + .high + } + // MARK: - InAppNotificationProvider var notificationDescriptor: InAppNotificationDescriptor? { diff --git a/ios/MullvadVPN/Notifications/Notification Providers/AccountExpirySystemNotificationProvider.swift b/ios/MullvadVPN/Notifications/Notification Providers/AccountExpirySystemNotificationProvider.swift index e0e69f747d1a..a8335a510a7a 100644 --- a/ios/MullvadVPN/Notifications/Notification Providers/AccountExpirySystemNotificationProvider.swift +++ b/ios/MullvadVPN/Notifications/Notification Providers/AccountExpirySystemNotificationProvider.swift @@ -45,6 +45,10 @@ final class AccountExpirySystemNotificationProvider: NotificationProvider, Syste .accountExpirySystemNotification } + override var priority: NotificationPriority { + .high + } + // MARK: - SystemNotificationProvider var notificationRequest: UNNotificationRequest? { diff --git a/ios/MullvadVPN/Notifications/Notification Providers/LatestChangesNotificationProvider.swift b/ios/MullvadVPN/Notifications/Notification Providers/LatestChangesNotificationProvider.swift index 2e72f45545d1..42e4cac792a4 100644 --- a/ios/MullvadVPN/Notifications/Notification Providers/LatestChangesNotificationProvider.swift +++ b/ios/MullvadVPN/Notifications/Notification Providers/LatestChangesNotificationProvider.swift @@ -27,6 +27,10 @@ class LatestChangesNotificationProvider: NotificationProvider, InAppNotification .latestChangesInAppNotificationProvider } + override var priority: NotificationPriority { + .low + } + var notificationDescriptor: InAppNotificationDescriptor? { defer { // Always update the last seen version diff --git a/ios/MullvadVPN/Notifications/Notification Providers/NewDeviceNotificationProvider.swift b/ios/MullvadVPN/Notifications/Notification Providers/NewDeviceNotificationProvider.swift index 66b76f9116fc..4b013c02d138 100644 --- a/ios/MullvadVPN/Notifications/Notification Providers/NewDeviceNotificationProvider.swift +++ b/ios/MullvadVPN/Notifications/Notification Providers/NewDeviceNotificationProvider.swift @@ -81,6 +81,10 @@ final class NewDeviceNotificationProvider: NotificationProvider, .registeredDeviceInAppNotification } + override var priority: NotificationPriority { + .medium + } + private func addObservers() { tunnelObserver = TunnelBlockObserver(didUpdateDeviceState: { [weak self] _, deviceState, previousDeviceState in diff --git a/ios/MullvadVPN/Notifications/Notification Providers/TunnelStatusNotificationProvider.swift b/ios/MullvadVPN/Notifications/Notification Providers/TunnelStatusNotificationProvider.swift index 8a0a3b88cee3..b46dc8a202e2 100644 --- a/ios/MullvadVPN/Notifications/Notification Providers/TunnelStatusNotificationProvider.swift +++ b/ios/MullvadVPN/Notifications/Notification Providers/TunnelStatusNotificationProvider.swift @@ -20,6 +20,10 @@ final class TunnelStatusNotificationProvider: NotificationProvider, InAppNotific .tunnelStatusNotificationProvider } + override var priority: NotificationPriority { + .critical + } + var notificationDescriptor: InAppNotificationDescriptor? { if let packetTunnelError { return notificationDescription(for: packetTunnelError) diff --git a/ios/MullvadVPN/Notifications/NotificationManager.swift b/ios/MullvadVPN/Notifications/NotificationManager.swift index 378a9bdc1bf2..924130c93eeb 100644 --- a/ios/MullvadVPN/Notifications/NotificationManager.swift +++ b/ios/MullvadVPN/Notifications/NotificationManager.swift @@ -30,7 +30,7 @@ final class NotificationManager: NotificationProviderDelegate { newNotificationProvider.delegate = self } - _notificationProviders = newNotificationProviders + _notificationProviders = newNotificationProviders.sorted { $0.priority > $1.priority } } } diff --git a/ios/MullvadVPN/Notifications/NotificationProvider.swift b/ios/MullvadVPN/Notifications/NotificationProvider.swift index 8e82313010b0..57fff1916db4 100644 --- a/ios/MullvadVPN/Notifications/NotificationProvider.swift +++ b/ios/MullvadVPN/Notifications/NotificationProvider.swift @@ -29,6 +29,13 @@ class NotificationProvider: NotificationProviderProtocol, @unchecked Sendable { .default } + /** + Default implementation for the priority property, setting it to `.low`. + */ + var priority: NotificationPriority { + .low + } + /** Send action to notification manager delegate. diff --git a/ios/MullvadVPN/Notifications/NotificationProviderIdentifier.swift b/ios/MullvadVPN/Notifications/NotificationProviderIdentifier.swift index 155d0f7bdb4d..a01c46055a16 100644 --- a/ios/MullvadVPN/Notifications/NotificationProviderIdentifier.swift +++ b/ios/MullvadVPN/Notifications/NotificationProviderIdentifier.swift @@ -7,6 +7,16 @@ // import Foundation +enum NotificationPriority: Int, Comparable { + case low = 1 + case medium = 2 + case high = 3 + case critical = 4 + + static func < (lhs: NotificationPriority, rhs: NotificationPriority) -> Bool { + return lhs.rawValue < rhs.rawValue + } +} enum NotificationProviderIdentifier: String { case accountExpirySystemNotification = "AccountExpiryNotification" diff --git a/ios/MullvadVPN/Notifications/NotificationProviderProtocol.swift b/ios/MullvadVPN/Notifications/NotificationProviderProtocol.swift index e4361578c923..1e21bb0a14e9 100644 --- a/ios/MullvadVPN/Notifications/NotificationProviderProtocol.swift +++ b/ios/MullvadVPN/Notifications/NotificationProviderProtocol.swift @@ -14,6 +14,10 @@ protocol NotificationProviderProtocol { /// produced by them. var identifier: NotificationProviderIdentifier { get } + /// The priority level of the notification, used to determine the order in which notifications + /// should be displayed. Higher priority notifications are displayed first. + var priority: NotificationPriority { get } + /// Tell notification manager to update the associated notification. func invalidate() }