summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/Extensions/UIAlertController+InAppPurchase.swift
blob: c7096d91f0c7d7a247054095dff3653ae10c887b (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
//
//  UIAlertController+InAppPurchase.swift
//  MullvadVPN
//
//  Created by Steffen Ernst on 2025-01-20.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import StoreKit
import UIKit

extension UIAlertController {
    public static func showInAppPurchaseAlert(
        products: [SKProduct],
        didRequestPurchase: @escaping (SKProduct) -> Void
    ) -> UIAlertController {
        let localizedString = NSLocalizedString(
            "ADD_TIME_BUTTON",
            tableName: "Welcome",
            value: "Add Time",
            comment: ""
        )
        let actionSheet = UIAlertController(
            title: localizedString,
            message: nil,
            preferredStyle: UIDevice.current.userInterfaceIdiom == .pad ? .alert : .actionSheet
        )
        actionSheet.overrideUserInterfaceStyle = .dark
        actionSheet.view.tintColor = .AlertController.tintColor
        products.sortedByPrice().forEach { product in
            guard let localizedTitle = product.customLocalizedTitle else {
                return
            }
            let action = UIAlertAction(title: localizedTitle, style: .default, handler: { _ in
                actionSheet.dismiss(animated: true, completion: {
                    didRequestPurchase(product)
                })
            })
            action
                .accessibilityIdentifier =
                "\(AccessibilityIdentifier.purchaseButton.asString)_\(product.productIdentifier)"
            actionSheet.addAction(action)
        }
        let cancelAction = UIAlertAction(title: NSLocalizedString(
            "PRODUCT_LIST_CANCEL_BUTTON",
            tableName: "Welcome",
            value: "Cancel",
            comment: ""
        ), style: .cancel)
        cancelAction.accessibilityIdentifier = AccessibilityIdentifier.cancelPurchaseListButton.asString
        actionSheet.addAction(cancelAction)
        return actionSheet
    }
}