summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/ProblemReportSubmissionOverlayView.swift
blob: fadff38f704637e4dc59686059866b8264e206b5 (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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//
//  ProblemReportSubmissionOverlayView.swift
//  MullvadVPN
//
//  Created by pronebird on 12/02/2021.
//  Copyright © 2021 Mullvad VPN AB. All rights reserved.
//

import UIKit
import Foundation

class ProblemReportSubmissionOverlayView: UIView {

    var editButtonAction: (() -> Void)?
    var retryButtonAction: (() -> Void)?

    enum State {
        case sending
        case sent(_ email: String)
        case failure(REST.Error)

        var title: String? {
            switch self {
            case .sending:
                return NSLocalizedString(
                    "SUBMISSION_STATUS_SENDING",
                    tableName: "ProblemReport",
                    value: "Sending...",
                    comment: ""
                )
            case .sent:
                return NSLocalizedString(
                    "SUBMISSION_STATUS_SENT",
                    tableName: "ProblemReport",
                    value: "Sent",
                    comment: ""
                )
            case .failure:
                return NSLocalizedString(
                    "SUBMISSION_STATUS_FAILURE",
                    tableName: "ProblemReport",
                    value: "Failed to send",
                    comment: ""
                )
            }
        }

        var body: NSAttributedString? {
            switch self {
            case .sending:
                return nil
            case .sent(let email):
                let combinedAttributedString = NSMutableAttributedString(
                    string: NSLocalizedString(
                        "THANKS_MESSAGE",
                        tableName: "ProblemReport",
                        value: "Thanks!",
                        comment: ""
                    ),
                    attributes: [.foregroundColor: UIColor.successColor]
                )

                if email.isEmpty {
                    combinedAttributedString.append(NSAttributedString(string: " "))
                    combinedAttributedString.append(
                        NSAttributedString(
                            string: NSLocalizedString(
                                "WE_WILL_LOOK_INTO_THIS_MESSAGE",
                                tableName: "ProblemReport",
                                value: "We will look into this.",
                                comment: ""
                            )
                        )
                    )
                } else {
                    let emailText = String(
                        format: NSLocalizedString(
                            "CONTACT_BACK_EMAIL_MESSAGE_FORMAT",
                            tableName: "ProblemReport",
                            value: "If needed we will contact you at %@",
                            comment: ""
                        ), email)
                    let emailAttributedString = NSMutableAttributedString(string: emailText)
                    if let emailRange = emailText.range(of: email) {
                        let font = UIFont.systemFont(ofSize: 17, weight: .bold)
                        let nsRange = NSRange(emailRange, in: emailText)

                        emailAttributedString.addAttribute(.font, value: font, range: nsRange)
                    }

                    combinedAttributedString.append(NSAttributedString(string: " "))
                    combinedAttributedString.append(emailAttributedString)
                }

                return combinedAttributedString

            case .failure(let error):
                return error.errorChainDescription.flatMap { NSAttributedString(string: $0) }
            }
        }
    }

    var state: State = .sending {
        didSet {
            transitionToState(self.state)
        }
    }

    let activityIndicator: SpinnerActivityIndicatorView = {
        let indicator = SpinnerActivityIndicatorView(style: .large)
        indicator.tintColor = .white
        return indicator
    }()
    let statusImageView = StatusImageView(style: .success)

    let titleLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.font = UIFont.systemFont(ofSize: 32)
        textLabel.textColor = .white
        textLabel.numberOfLines = 0
        return textLabel
    }()

    let bodyLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.font = UIFont.systemFont(ofSize: 17)
        textLabel.textColor = .white
        textLabel.numberOfLines = 0
        return textLabel
    }()

    /// Footer stack view that contains action buttons
    private lazy var buttonsStackView: UIStackView = {
        let stackView = UIStackView(arrangedSubviews: [self.editMessageButton, self.tryAgainButton])
        stackView.translatesAutoresizingMaskIntoConstraints = false
        stackView.axis = .vertical
        stackView.spacing = 18

        return stackView
    }()

    private lazy var editMessageButton: AppButton = {
        let button = AppButton(style: .default)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle(NSLocalizedString(
            "EDIT_MESSAGE_BUTTON",
            tableName: "ProblemReport",
            value: "Edit message",
            comment: ""
        ), for: .normal)
        button.addTarget(self, action: #selector(handleEditButton), for: .touchUpInside)
        return button
    }()

    private lazy var tryAgainButton: AppButton = {
        let button = AppButton(style: .success)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle(NSLocalizedString(
            "TRY_AGAIN_BUTTON",
            tableName: "ProblemReport",
            value: "Try again",
            comment: ""
        ), for: .normal)
        button.addTarget(self, action: #selector(handleRetryButton), for: .touchUpInside)
        return button
    }()

    override init(frame: CGRect) {
        super.init(frame: frame)

        addSubviews()
        transitionToState(state)

        layoutMargins = UIMetrics.contentLayoutMargins
    }

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

    private func addSubviews() {
        for subview in [titleLabel, bodyLabel, activityIndicator, statusImageView, buttonsStackView] {
            subview.translatesAutoresizingMaskIntoConstraints = false
            addSubview(subview)
        }

        NSLayoutConstraint.activate([
            statusImageView.topAnchor.constraint(equalTo: layoutMarginsGuide.topAnchor, constant: 32),
            statusImageView.centerXAnchor.constraint(equalTo: centerXAnchor),

            activityIndicator.centerXAnchor.constraint(equalTo: statusImageView.centerXAnchor),
            activityIndicator.centerYAnchor.constraint(equalTo: statusImageView.centerYAnchor),

            titleLabel.topAnchor.constraint(equalTo: statusImageView.bottomAnchor, constant: 60),
            titleLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
            titleLabel.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),

            bodyLabel.topAnchor.constraint(equalToSystemSpacingBelow: titleLabel.bottomAnchor, multiplier: 1),
            bodyLabel.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
            bodyLabel.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
            buttonsStackView.topAnchor.constraint(greaterThanOrEqualTo: bodyLabel.bottomAnchor, constant: 18),

            buttonsStackView.leadingAnchor.constraint(equalTo: layoutMarginsGuide.leadingAnchor),
            buttonsStackView.trailingAnchor.constraint(equalTo: layoutMarginsGuide.trailingAnchor),
            buttonsStackView.bottomAnchor.constraint(equalTo: layoutMarginsGuide.bottomAnchor)
        ])
    }

    private func transitionToState(_ state: State) {
        titleLabel.text = state.title
        bodyLabel.attributedText = state.body

        switch state {
        case .sending:
            activityIndicator.startAnimating()
            statusImageView.isHidden = true
            buttonsStackView.isHidden = true

        case .sent:
            activityIndicator.stopAnimating()
            statusImageView.style = .success
            statusImageView.isHidden = false
            buttonsStackView.isHidden = true

        case .failure:
            activityIndicator.stopAnimating()
            statusImageView.style = .failure
            statusImageView.isHidden = false
            buttonsStackView.isHidden = false
        }
    }

    // MARK: - Actions

    @objc private func handleEditButton() {
        editButtonAction?()
    }

    @objc private func handleRetryButton() {
        retryButtonAction?()
    }
}