Skip to content

Commit

Permalink
Add priority for notifications
Browse files Browse the repository at this point in the history
  • Loading branch information
mojganii committed Jan 24, 2025
1 parent 654de1c commit 1cb83da
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ final class AccountExpiryInAppNotificationProvider: NotificationProvider, InAppN
.accountExpiryInAppNotification
}

override var priority: NotificationPriority {
.high
}

// MARK: - InAppNotificationProvider

var notificationDescriptor: InAppNotificationDescriptor? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ final class AccountExpirySystemNotificationProvider: NotificationProvider, Syste
.accountExpirySystemNotification
}

override var priority: NotificationPriority {
.high
}

// MARK: - SystemNotificationProvider

var notificationRequest: UNNotificationRequest? {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class LatestChangesNotificationProvider: NotificationProvider, InAppNotification
.latestChangesInAppNotificationProvider
}

override var priority: NotificationPriority {
.low
}

var notificationDescriptor: InAppNotificationDescriptor? {
defer {
// Always update the last seen version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion ios/MullvadVPN/Notifications/NotificationManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ final class NotificationManager: NotificationProviderDelegate {

var notificationProviders: [NotificationProvider] {
get {
_notificationProviders
_notificationProviders.sorted {
if $0.priority == $1.priority {
// Use timestamp to break ties (older notifications are shown first)
return $0.timestamp < $1.timestamp
}
return $0.priority > $1.priority
}
}
set(newNotificationProviders) {
dispatchPrecondition(condition: .onQueue(.main))
Expand Down
8 changes: 8 additions & 0 deletions ios/MullvadVPN/Notifications/NotificationProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ protocol NotificationProviderDelegate: AnyObject {
/// Base class for all notification providers.
class NotificationProvider: NotificationProviderProtocol, @unchecked Sendable {
weak var delegate: NotificationProviderDelegate?
var timestamp = Date()

/**
Provider identifier.
Expand All @@ -29,6 +30,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.

Expand Down
10 changes: 10 additions & 0 deletions ios/MullvadVPN/Notifications/NotificationProviderIdentifier.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ 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 }

/// Timestamp for tie-breaking (when the notification became valid)
var timestamp: Date { get }

/// Tell notification manager to update the associated notification.
func invalidate()
}

0 comments on commit 1cb83da

Please sign in to comment.