summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Notifications/Notification Providers/NewDeviceNotificationProvider.swift
blob: dcdc7dc0870e33967d178ce9118239e6e6665003 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//
//  NewDeviceNotificationProvider.swift
//  MullvadVPN
//
//  Created by Mojgan on 2023-04-21.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import MullvadSettings
import UIKit.UIColor
import UIKit.UIFont

final class NewDeviceNotificationProvider: NotificationProvider,
    InAppNotificationProvider, @unchecked Sendable
{
    // MARK: - private properties

    private let tunnelManager: TunnelManager

    private var storedDeviceData: StoredDeviceData? {
        tunnelManager.deviceState.deviceData
    }

    private var tunnelObserver: TunnelBlockObserver?
    private var isNewDeviceRegistered = false

    private var attributedBody: NSAttributedString {
        let formattedString = NSLocalizedString(
            "This device is now named **%@**. See more under \"Manage devices\" in Account.",
            comment: ""
        )
        let deviceName = storedDeviceData?.capitalizedName ?? ""
        let string = String(format: formattedString, deviceName)

        return NSAttributedString(
            markdownString: string,
            options: MarkdownStylingOptions(
                font: .preferredFont(forTextStyle: .subheadline)
            )
        ) { _, _ in
            [.foregroundColor: UIColor.InAppNotificationBanner.titleColor]
        }
    }

    // MARK: - public properties

    var notificationDescriptor: InAppNotificationDescriptor? {
        guard isNewDeviceRegistered else { return nil }
        return InAppNotificationDescriptor(
            identifier: identifier,
            style: .success,
            title: NSLocalizedString("NEW DEVICE CREATED", comment: ""),
            body: attributedBody,
            button: InAppNotificationAction(
                image: UIImage.Buttons.closeSmall,
                handler: { [weak self] in
                    guard let self else { return }
                    isNewDeviceRegistered = false
                    sendAction()
                    invalidate()
                }
            )
        )
    }

    // MARK: - initialize

    init(tunnelManager: TunnelManager) {
        self.tunnelManager = tunnelManager
        super.init()
        addObservers()
    }

    override var identifier: NotificationProviderIdentifier {
        .registeredDeviceInAppNotification
    }

    override var priority: NotificationPriority {
        .medium
    }

    private func addObservers() {
        tunnelObserver =
            TunnelBlockObserver(didUpdateDeviceState: { [weak self] _, deviceState, previousDeviceState in
                if previousDeviceState == .loggedOut,
                    case .loggedIn = deviceState
                {
                    self?.isNewDeviceRegistered = true
                    DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(1)) { [weak self] in
                        self?.invalidate()
                    }

                } else if case .loggedIn = previousDeviceState,
                    deviceState == .loggedOut || deviceState == .revoked
                {
                    self?.isNewDeviceRegistered = false
                    self?.invalidate()
                }
            })
        tunnelObserver.flatMap { tunnelManager.addObserver($0) }
    }
}