summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/RedeemVoucher/AddCreditSucceededViewController.swift
blob: 7fab48068d4cc691a4fc92396df63528b590d3dc (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
//
//  AddCreditSucceededViewController.swift
//  MullvadVPN
//
//  Created by Sajad Vishkai on 2022-09-23.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import UIKit

protocol AddCreditSucceededViewControllerDelegate: AnyObject {
    func header(in controller: AddCreditSucceededViewController) -> String

    func titleForAction(in controller: AddCreditSucceededViewController) -> String

    func addCreditSucceededViewControllerDidFinish(in controller: AddCreditSucceededViewController)
}

class AddCreditSucceededViewController: UIViewController, RootContainment {
    private let statusImageView: StatusImageView = {
        let statusImageView = StatusImageView(style: .success)
        statusImageView.translatesAutoresizingMaskIntoConstraints = false
        return statusImageView
    }()

    private let titleLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.boldSystemFont(ofSize: 20)
        label.textColor = .white
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    private let messageLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFont(ofSize: 17)
        label.textColor = UIColor.white.withAlphaComponent(0.6)
        label.numberOfLines = 0
        label.translatesAutoresizingMaskIntoConstraints = false
        return label
    }()

    private let dismissButton: AppButton = {
        let button = AppButton(style: .default)
        button.translatesAutoresizingMaskIntoConstraints = false
        return button
    }()

    override var preferredStatusBarStyle: UIStatusBarStyle {
        .lightContent
    }

    var preferredHeaderBarPresentation: HeaderBarPresentation {
        HeaderBarPresentation(style: .default, showsDivider: true)
    }

    var prefersHeaderBarHidden: Bool {
        false
    }

    var prefersDeviceInfoBarHidden: Bool {
        true
    }

    weak var delegate: AddCreditSucceededViewControllerDelegate? {
        didSet {
            dismissButton.setTitle(delegate?.titleForAction(in: self), for: .normal)
            titleLabel.text = delegate?.header(in: self)
        }
    }

    init(timeAddedComponents: DateComponents) {
        super.init(nibName: nil, bundle: nil)

        view.backgroundColor = .secondaryColor
        view.directionalLayoutMargins = UIMetrics.contentLayoutMargins

        messageLabel.text = String(
            format: NSLocalizedString("%@ was added to your account.", comment: ""),
            timeAddedComponents.formattedAddedDay
        )
    }

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

    override func viewDidLoad() {
        super.viewDidLoad()
        configureUI()
        addDismissButtonHandler()
    }

    private func configureUI() {
        let contentHolderView = UIView(frame: .zero)

        view.addConstrainedSubviews([contentHolderView]) {
            contentHolderView.pinEdgesToSuperview(.all(UIMetrics.SettingsRedeemVoucher.successfulRedeemMargins))
        }

        contentHolderView.addConstrainedSubviews([statusImageView, titleLabel, messageLabel, dismissButton]) {
            statusImageView.pinEdgesToSuperviewMargins(PinnableEdges([.top(0)]))
            statusImageView.centerXAnchor.constraint(equalTo: view.centerXAnchor)

            titleLabel.pinEdgesToSuperviewMargins(PinnableEdges([.leading(0), .trailing(0)]))
            titleLabel.topAnchor.constraint(
                equalTo: statusImageView.bottomAnchor,
                constant: UIMetrics.TableView.sectionSpacing
            )

            messageLabel.topAnchor.constraint(
                equalTo: titleLabel.layoutMarginsGuide.bottomAnchor,
                constant: UIMetrics.interButtonSpacing
            )
            messageLabel.pinEdgesToSuperviewMargins(PinnableEdges([.leading(0), .trailing(0)]))

            dismissButton.pinEdgesToSuperviewMargins(.all().excluding(.top))
        }
    }

    private func addDismissButtonHandler() {
        dismissButton.addTarget(
            self,
            action: #selector(handleDismissTap),
            for: .touchUpInside
        )
    }

    @objc private func handleDismissTap() {
        delegate?.addCreditSucceededViewControllerDidFinish(in: self)
    }
}

private extension DateComponents {
    var formattedAddedDay: String {
        let formatter = DateComponentsFormatter()
        formatter.allowedUnits = [.day]
        formatter.unitsStyle = .full
        return formatter.string(from: self) ?? ""
    }
}