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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
|
//
// ProblemReportViewController.swift
// MullvadVPN
//
// Created by pronebird on 15/09/2020.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import MullvadREST
import MullvadTypes
import Operations
import UIKit
final class ProblemReportViewController: UIViewController, UITextFieldDelegate {
private let alertPresenter: AlertPresenter
let interactor: ProblemReportInteractor
var textViewKeyboardResponder: AutomaticKeyboardResponder?
var scrollViewKeyboardResponder: AutomaticKeyboardResponder?
var showsSubmissionOverlay = false
/// Constraints used when description text view is active
var activeMessageTextViewConstraints = [NSLayoutConstraint]()
/// Constraints used when description text view is inactive
var inactiveMessageTextViewConstraints = [NSLayoutConstraint]()
/// Flag indicating when the text view is expanded to fill the entire view
var isMessageTextViewExpanded = false
static var persistentViewModel = ProblemReportViewModel()
/// Scroll view
lazy var scrollView: UIScrollView = { makeScrollView() }()
/// Scroll view content container
lazy var containerView: UIView = { makeContainerView() }()
/// Subheading label displayed below navigation bar
lazy var subheaderLabel: UILabel = { makeSubheaderLabel() }()
lazy var emailTextField: CustomTextField = { makeEmailTextField() }()
lazy var messageTextView: CustomTextView = { makeMessageTextView() }()
/// Container view for text input fields
lazy var textFieldsHolder: UIView = { makeTextFieldsHolder() }()
/// Placeholder view used to fill the space within the scroll view when the text view is
/// expanded to fill the entire view
lazy var messagePlaceholder: UIView = { makeMessagePlaceholderView() }()
/// Footer stack view that contains action buttons
lazy var buttonsStackView: UIStackView = { makeButtonsStackView() }()
lazy var viewLogsButton: AppButton = { makeViewLogsButton() }()
lazy var sendButton: AppButton = { makeSendButton() }()
lazy var emailAccessoryToolbar: UIToolbar = makeKeyboardToolbar(
canGoBackward: false,
canGoForward: true
)
lazy var messageAccessoryToolbar: UIToolbar = makeKeyboardToolbar(
canGoBackward: true,
canGoForward: false
)
lazy var submissionOverlayView: ProblemReportSubmissionOverlayView = { makeSubmissionOverlayView() }()
// MARK: - View lifecycle
override var preferredStatusBarStyle: UIStatusBarStyle { .lightContent }
// Allow dismissing the keyboard in .formSheet presentation style
override var disablesAutomaticKeyboardDismissal: Bool { false }
init(interactor: ProblemReportInteractor, alertPresenter: AlertPresenter) {
self.interactor = interactor
self.alertPresenter = alertPresenter
super.init(nibName: nil, bundle: nil)
}
required init?(coder: NSCoder) { fatalError("init(coder:) has not been implemented") }
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .secondaryColor
view.setAccessibilityIdentifier(.problemReportView)
navigationItem.title = ProblemReportViewModel.navigationTitle
textViewKeyboardResponder = AutomaticKeyboardResponder(targetView: messageTextView)
scrollViewKeyboardResponder = AutomaticKeyboardResponder(targetView: scrollView)
// Make sure that the user can't easily dismiss the controller on iOS 13 and above
isModalInPresentation = true
// Set hugging & compression priorities so that description text view wants to grow
emailTextField.setContentHuggingPriority(.defaultHigh, for: .vertical)
emailTextField.setContentCompressionResistancePriority(.defaultHigh, for: .vertical)
messageTextView.setContentHuggingPriority(.defaultLow, for: .vertical)
messageTextView.setContentCompressionResistancePriority(.defaultLow, for: .vertical)
emailTextField.setAccessibilityIdentifier(.problemReportEmailTextField)
messageTextView.setAccessibilityIdentifier(.problemReportMessageTextView)
addConstraints()
registerForNotifications()
loadPersistentViewModel()
}
// MARK: - Actions
@objc func focusEmailTextField() {
emailTextField.becomeFirstResponder()
}
@objc func focusDescriptionTextView() {
messageTextView.becomeFirstResponder()
}
@objc func dismissKeyboard() {
view.endEditing(false)
}
@objc func handleSendButtonTap() {
let proceedWithSubmission = {
self.sendProblemReport()
}
if Self.persistentViewModel.email.isEmpty {
presentEmptyEmailConfirmationAlert { shouldSend in
if shouldSend {
proceedWithSubmission()
}
}
} else {
proceedWithSubmission()
}
}
@objc func handleViewLogsButtonTap() {
let reviewController = ProblemReportReviewViewController(interactor: interactor)
let navigationController = CustomNavigationController(rootViewController: reviewController)
present(navigationController, animated: true)
}
// MARK: - Private
private func registerForNotifications() {
let notificationCenter = NotificationCenter.default
notificationCenter.addObserver(
self,
selector: #selector(emailTextFieldDidChange),
name: UITextField.textDidChangeNotification,
object: emailTextField
)
notificationCenter.addObserver(
self,
selector: #selector(messageTextViewDidBeginEditing),
name: UITextView.textDidBeginEditingNotification,
object: messageTextView
)
notificationCenter.addObserver(
self,
selector: #selector(messageTextViewDidEndEditing),
name: UITextView.textDidEndEditingNotification,
object: messageTextView
)
notificationCenter.addObserver(
self,
selector: #selector(messageTextViewDidChange),
name: UITextView.textDidChangeNotification,
object: messageTextView
)
}
private func presentEmptyEmailConfirmationAlert(completion: @escaping (Bool) -> Void) {
let presentation = AlertPresentation(
id: "problem-report-alert",
icon: .alert,
message: ProblemReportViewModel.emptyEmailAlertWarning,
buttons: [
AlertAction(
title: ProblemReportViewModel.confirmEmptyEmailTitle,
style: .destructive,
handler: {
completion(true)
}
),
AlertAction(
title: ProblemReportViewModel.cancelEmptyEmailTitle,
style: .default,
handler: {
completion(false)
}
),
]
)
alertPresenter.showAlert(presentation: presentation, animated: true)
}
// MARK: - Data model
private func loadPersistentViewModel() {
emailTextField.text = Self.persistentViewModel.email
messageTextView.text = Self.persistentViewModel.message
validateForm()
}
private func updatePersistentViewModel() {
Self.persistentViewModel = ProblemReportViewModel(
email: emailTextField.text ?? "",
message: messageTextView.text
)
validateForm()
}
private func setPopGestureEnabled(_ isEnabled: Bool) {
navigationController?.interactivePopGestureRecognizer?.isEnabled = isEnabled
}
private func clearPersistentViewModel() {
Self.persistentViewModel = ProblemReportViewModel()
}
// MARK: - Form validation
private func validateForm() {
sendButton.isEnabled = Self.persistentViewModel.isValid
}
// MARK: - Problem submission progress handling
private func willSendProblemReport() {
showSubmissionOverlay()
submissionOverlayView.state = .sending
navigationItem.setHidesBackButton(true, animated: true)
}
private func didSendProblemReport(
viewModel: ProblemReportViewModel,
completion: Result<Void, Error>
) {
switch completion {
case .success:
submissionOverlayView.state = .sent(viewModel.email)
// Clear persistent view model upon successful submission
clearPersistentViewModel()
case let .failure(error):
if let error = error as? OperationError, error == .cancelled {
hideSubmissionOverlay()
} else {
submissionOverlayView.state = .failure(error)
}
}
navigationItem.setHidesBackButton(false, animated: true)
}
// MARK: - Problem report submission helpers
func sendProblemReport() {
let viewModel = Self.persistentViewModel
willSendProblemReport()
interactor.sendReport(
email: viewModel.email,
message: viewModel.message
) { [weak self] completion in
Task { @MainActor in
self?.didSendProblemReport(viewModel: viewModel, completion: completion)
}
}
}
// MARK: - Input fields notifications
@objc private func messageTextViewDidBeginEditing() {
setDescriptionFieldExpanded(true)
setPopGestureEnabled(false)
}
@objc private func messageTextViewDidEndEditing() {
setDescriptionFieldExpanded(false)
setPopGestureEnabled(true)
}
@objc private func messageTextViewDidChange() {
updatePersistentViewModel()
}
@objc private func emailTextFieldDidChange() {
updatePersistentViewModel()
}
// MARK: - UITextFieldDelegate
func textFieldDidBeginEditing(_ textField: UITextField) {
setPopGestureEnabled(false)
}
func textFieldDidEndEditing(_ textField: UITextField) {
setPopGestureEnabled(true)
}
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
messageTextView.becomeFirstResponder()
return false
}
}
|