diff options
| author | Jon Petersson <jon.petersson@mullvad.net> | 2025-01-22 16:39:21 +0100 |
|---|---|---|
| committer | Jon Petersson <jon.petersson@mullvad.net> | 2025-01-22 16:39:21 +0100 |
| commit | a1b47c23a9532abc0f51fc94de481b0528afb9fb (patch) | |
| tree | 8c93aed3d2fc5540e963bc6b02dbdd8549268429 /ios/MullvadVPN/Notifications/Notification Providers/NewDeviceNotificationProvider.swift | |
| parent | 060839d420a9cf222b49fe4932730a98fd5b1434 (diff) | |
| parent | 5f9315b46dc7a364bc20d40420c2e0feb34a2d6c (diff) | |
| download | mullvadvpn-a1b47c23a9532abc0f51fc94de481b0528afb9fb.tar.xz mullvadvpn-a1b47c23a9532abc0f51fc94de481b0528afb9fb.zip | |
Merge branch 'add-in-app-notification-banner-for-changelog-ios-989'
Diffstat (limited to 'ios/MullvadVPN/Notifications/Notification Providers/NewDeviceNotificationProvider.swift')
| -rw-r--r-- | ios/MullvadVPN/Notifications/Notification Providers/NewDeviceNotificationProvider.swift | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/ios/MullvadVPN/Notifications/Notification Providers/NewDeviceNotificationProvider.swift b/ios/MullvadVPN/Notifications/Notification Providers/NewDeviceNotificationProvider.swift new file mode 100644 index 0000000000..66b76f9116 --- /dev/null +++ b/ios/MullvadVPN/Notifications/Notification Providers/NewDeviceNotificationProvider.swift @@ -0,0 +1,102 @@ +// +// NewDeviceNotificationProvider.swift +// MullvadVPN +// +// Created by Mojgan on 2023-04-21. +// Copyright © 2023 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( + "ACCOUNT_CREATION_INAPP_NOTIFICATION_BODY", + value: "Welcome, this device is now called **%@**. For more details see the info button in Account.", + comment: "" + ) + let deviceName = storedDeviceData?.capitalizedName ?? "" + let string = String(format: formattedString, deviceName) + + let stylingOptions = MarkdownStylingOptions(font: .systemFont(ofSize: 14.0)) + + return NSAttributedString(markdownString: string, options: stylingOptions) { markdownType, _ in + if case .bold = markdownType { + return [.foregroundColor: UIColor.InAppNotificationBanner.titleColor] + } else { + return [:] + } + } + } + + // MARK: - public properties + + var notificationDescriptor: InAppNotificationDescriptor? { + guard isNewDeviceRegistered else { return nil } + return InAppNotificationDescriptor( + identifier: identifier, + style: .success, + title: NSLocalizedString( + "ACCOUNT_CREATION_INAPP_NOTIFICATION_TITLE", + value: "NEW DEVICE CREATED", + comment: "" + ), + body: attributedBody, + button: InAppNotificationAction( + image: UIImage(named: "IconCloseSml"), + 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 + } + + 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) } + } +} |
