summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/OutOfTime/OutOfTimeViewController.swift
blob: 2f9020df56e0fe273ea2f30733e4b8895a20017e (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//
//  OutOfTimeViewController.swift
//  MullvadVPN
//
//  Created by Andreas Lif on 2022-07-25.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import MullvadREST
import Operations
@preconcurrency import StoreKit
import UIKit

protocol OutOfTimeViewControllerDelegate: AnyObject, Sendable {
    func didRequestShowInAppPurchase(
        accountNumber: String,
        paymentAction: PaymentAction
    )
}

@MainActor
class OutOfTimeViewController: UIViewController, RootContainment {
    weak var delegate: OutOfTimeViewControllerDelegate?

    private let interactor: OutOfTimeInteractor

    private lazy var contentView = OutOfTimeContentView()

    override var preferredStatusBarStyle: UIStatusBarStyle {
        .lightContent
    }

    nonisolated var preferredHeaderBarPresentation: HeaderBarPresentation {
        let tunnelState = interactor.tunnelStatus.state

        return HeaderBarPresentation(
            style: tunnelState.isSecured ? .secured : .unsecured,
            showsDivider: false
        )
    }

    var prefersHeaderBarHidden: Bool {
        false
    }

    init(interactor: OutOfTimeInteractor, errorPresenter: PaymentAlertPresenter) {
        self.interactor = interactor

        super.init(nibName: nil, bundle: nil)
    }

    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        view.addSubview(contentView)

        NSLayoutConstraint.activate([
            contentView.topAnchor.constraint(equalTo: view.topAnchor),
            contentView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            contentView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            contentView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])

        contentView.disconnectButton.addTarget(
            self,
            action: #selector(handleDisconnect(_:)),
            for: .touchUpInside
        )
        contentView.purchaseButton.addTarget(
            self,
            action: #selector(requestStoreProducts),
            for: .touchUpInside
        )
        contentView.restoreButton.addTarget(
            self,
            action: #selector(restorePurchases),
            for: .touchUpInside
        )

        interactor.didReceiveTunnelStatus = { [weak self] _ in
            Task { @MainActor in
                self?.setNeedsHeaderBarStyleAppearanceUpdate()
                self?.applyViewState()
            }
        }
        applyViewState()
    }

    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        interactor.startAccountUpdateTimer()
    }

    override func viewDidDisappear(_ animated: Bool) {
        super.viewDidDisappear(animated)
        interactor.stopAccountUpdateTimer()
    }

    // MARK: - Private

    private func applyViewState() {
        let tunnelState = interactor.tunnelStatus.state
        let baseMessage = NSLocalizedString("You have no more VPN time left on this account. ", comment: "")
        contentView.enableDisconnectButton(tunnelState.isSecured, animated: true)
        contentView.enablePurchaseButton(!tunnelState.isSecured)

        if tunnelState.isSecured {
            contentView.setBodyLabelText(
                [
                    baseMessage,
                    NSLocalizedString(
                        "To add more, you will need to "
                            + "disconnect and access the Internet with an unsecure connection.",
                        comment: ""
                    ),
                ].joined()
            )
        } else {
            contentView.setBodyLabelText(
                [
                    baseMessage,
                    NSLocalizedString(
                        "Either buy credit on our website "
                            + "or make an in-app purchase via the **Add time** button below.",
                        comment: ""
                    ),
                ].joined()
            )
        }
    }

    // MARK: - Actions

    @objc private func requestStoreProducts() {
        guard let accountNumber = interactor.deviceState.accountData?.number else {
            return
        }
        delegate?.didRequestShowInAppPurchase(
            accountNumber: accountNumber,
            paymentAction: .purchase
        )
    }

    @objc func restorePurchases() {
        guard let accountNumber = interactor.deviceState.accountData?.number else {
            return
        }
        delegate?.didRequestShowInAppPurchase(
            accountNumber: accountNumber,
            paymentAction: .restorePurchase
        )
    }

    @objc private func handleDisconnect(_ sender: Any) {
        contentView.disconnectButton.isEnabled = false
        interactor.stopTunnel()
    }
}