summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Notifications/NotificationProvider.swift
blob: 2844d1f3f52b8cef0952d5967adab75768d0d16b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
//
//  NotificationProvider.swift
//  MullvadVPN
//
//  Created by pronebird on 09/12/2022.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import UserNotifications

/// Notification provider delegate primarily used by `NotificationManager`.
protocol NotificationProviderDelegate: AnyObject {
    func notificationProviderDidInvalidate(_ notificationProvider: NotificationProvider)
    func notificationProvider(_ notificationProvider: NotificationProvider, didReceiveAction actionIdentifier: String)
}

/// Base class for all notification providers.
class NotificationProvider: NotificationProviderProtocol, @unchecked Sendable {
    weak var delegate: NotificationProviderDelegate?

    /**
     Provider identifier.

     Override in subclasses and make sure each provider has unique identifier. It's preferred that identifiers use
     reverse domain name, for instance: `com.example.app.ProviderName`.
     */
    var identifier: NotificationProviderIdentifier {
        .default
    }

    /**
     Default implementation for the priority property, setting it to `.low`.
     */
    var priority: NotificationPriority {
        .low
    }

    /**
     Send action to notification manager delegate.

     Usually in response to user interacting with notification banner, i.e by tapping a button. Use different action
     identifiers if notification offers more than one action that user can perform.
     */
    func sendAction(_ actionIdentifier: String = UNNotificationDefaultActionIdentifier) {
        dispatchOnMain {
            self.delegate?.notificationProvider(self, didReceiveAction: actionIdentifier)
        }
    }

    /**
     This method tells notification manager to re-evalute the notification content.
     Call this method when notification provider wants to change the content it presents.
     */
    func invalidate() {
        dispatchOnMain {
            self.delegate?.notificationProviderDidInvalidate(self)
        }
    }

    private func dispatchOnMain(_ block: @escaping @Sendable () -> Void) {
        if Thread.isMainThread {
            block()
        } else {
            DispatchQueue.main.async(execute: block)
        }
    }
}