summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/ProblemReportViewController.swift
blob: 2c8d4959098ef14f6f695ed64c67afec98b7a0e5 (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
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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
//
//  ProblemReportViewController.swift
//  MullvadVPN
//
//  Created by pronebird on 15/09/2020.
//  Copyright © 2020 Mullvad VPN AB. All rights reserved.
//

import UIKit

class ProblemReportViewController: UIViewController, UITextFieldDelegate, ConditionalNavigation {

    private let mullvadRest = MullvadRest(session: URLSession(configuration: .ephemeral))
    private lazy var consolidatedLog: ConsolidatedApplicationLog = {
        let securityGroupIdentifier = ApplicationConfiguration.securityGroupIdentifier

        // TODO: make sure we redact old tokens
        let redactStrings = Account.shared.token.flatMap { [$0] } ?? []

        let report = ConsolidatedApplicationLog(
            redactCustomStrings: redactStrings,
            redactContainerPathsForSecurityGroupIdentifiers: [securityGroupIdentifier]
        )

        report.addLogFiles(fileURLs: ApplicationConfiguration.logFileURLs)

        return report
    }()

    /// Scroll view
    private lazy var scrollView: UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.translatesAutoresizingMaskIntoConstraints = false
        scrollView.backgroundColor = .clear
        return scrollView
    }()

    /// Scroll view content container
    private lazy var containerView: UIView = {
        let containerView = UIView()
        containerView.translatesAutoresizingMaskIntoConstraints = false
        containerView.layoutMargins = UIMetrics.contentLayoutMargins
        containerView.backgroundColor = .clear
        return containerView
    }()

    /// Subheading label displayed below navigation bar
    private lazy var subheaderLabel: UILabel = {
        let textLabel = UILabel()
        textLabel.translatesAutoresizingMaskIntoConstraints = false
        textLabel.numberOfLines = 0
        textLabel.textColor = .white
        textLabel.text = NSLocalizedString("To help you more effectively, your app's log file will be attached to this message. Your data will remain secure and private, as it is anonymised before being sent over an encrypted channel.", comment: "")

        return textLabel
    }()

    private lazy var emailTextField: CustomTextField = {
        let textField = CustomTextField()
        textField.translatesAutoresizingMaskIntoConstraints = false
        textField.delegate = self
        textField.keyboardType = .emailAddress
        textField.textContentType = .emailAddress
        textField.autocorrectionType = .no
        textField.autocapitalizationType = .none
        textField.smartInsertDeleteType = .no
        textField.returnKeyType = .next
        textField.borderStyle = .none
        textField.backgroundColor = .white
        textField.inputAccessoryView = emailAccessoryToolbar
        textField.font = UIFont.systemFont(ofSize: 17)
        textField.placeholder = NSLocalizedString("Your email (optional)", comment: "")

        return textField
    }()

    private lazy var messageTextView: CustomTextView = {
        let textView = CustomTextView()
        textView.translatesAutoresizingMaskIntoConstraints = false
        textView.backgroundColor = .white
        textView.inputAccessoryView = messageAccessoryToolbar
        textView.font = UIFont.systemFont(ofSize: 17)
        textView.placeholder = NSLocalizedString("Please describe your problem in English or Swedish", comment: "")
        textView.contentInsetAdjustmentBehavior = .never

        return textView
    }()

    /// Container view for text input fields
    private lazy var textFieldsHolder: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        return view
    }()

    /// Constraints used when description text view is active
    private var activeMessageTextViewConstraints = [NSLayoutConstraint]()

    /// Constraints used when description text view is inactive
    private var inactiveMessageTextViewConstraints = [NSLayoutConstraint]()

    /// Flag indicating when the text view is expanded to fill the entire view
    private var isMessageTextViewExpanded = false

    /// Keyboard intersection with the controller view
    private var keyboardIntersectionRect = CGRect.zero

    /// Bottom content inset necessary to compensate for the keyboard overlapping
    var scrollViewBottomContentInsetAccountingForKeyboard: CGFloat {
        return max(0, keyboardIntersectionRect.height - view.safeAreaInsets.bottom)
    }

    /// Placeholder view used to fill the space within the scroll view when the text view is
    /// expanded to fill the entire view
    private lazy var messagePlaceholder: UIView = {
        let view = UIView()
        view.translatesAutoresizingMaskIntoConstraints = false
        view.backgroundColor = .clear
        return view
    }()

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

        return stackView
    }()

    private lazy var viewLogsButton: AppButton = {
        let button = AppButton(style: .default)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle(NSLocalizedString("View app logs", comment: ""), for: .normal)
        button.addTarget(self, action: #selector(handleViewLogsButtonTap), for: .touchUpInside)
        return button
    }()

    private lazy var sendButton: AppButton = {
        let button = AppButton(style: .success)
        button.translatesAutoresizingMaskIntoConstraints = false
        button.setTitle(NSLocalizedString("Send", comment: ""), for: .normal)
        button.addTarget(self, action: #selector(handleSendButtonTap), for: .touchUpInside)
        return button
    }()

    private lazy var emailAccessoryToolbar: UIToolbar = {
        return makeKeyboardToolbar(canGoBackward: false, canGoForward: true)
    }()

    private lazy var messageAccessoryToolbar: UIToolbar = {
        return makeKeyboardToolbar(canGoBackward: true, canGoForward: false)
    }()

    private lazy var submissionOverlayView: ProblemReportSubmissionOverlayView = {
        let overlay = ProblemReportSubmissionOverlayView()
        overlay.translatesAutoresizingMaskIntoConstraints = false

        overlay.editButtonAction = { [weak self] in
            self?.hideSubmissionOverlay()
        }

        overlay.retryButtonAction = { [weak self] in
            self?.sendProblemReport()
        }

        return overlay
    }()

    // MARK: - View lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        view.backgroundColor = .secondaryColor

        navigationItem.title = NSLocalizedString("Report a problem", comment: "Navigation title")

        // Make sure that the user can't easily dismiss the controller on iOS 13 and above
        if #available(iOS 13.0, *) {
            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)

        textFieldsHolder.addSubview(emailTextField)
        textFieldsHolder.addSubview(messagePlaceholder)
        textFieldsHolder.addSubview(messageTextView)

        view.addSubview(scrollView)
        scrollView.addSubview(containerView)
        containerView.addSubview(subheaderLabel)
        containerView.addSubview(textFieldsHolder)
        containerView.addSubview(buttonsStackView)

        addConstraints()
        registerForNotifications()

        loadPersistentViewModel()
    }

    override func viewSafeAreaInsetsDidChange() {
        super.viewSafeAreaInsetsDidChange()

        updateScrollViewContentInsets()
        updateMessageTextViewContentInsets()
    }


    // 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(reportString: consolidatedLog.string)
        let navigationController = UINavigationController(rootViewController: reviewController)

        present(navigationController, animated: true)
    }

    // MARK: - Private

    private func registerForNotifications() {
        let notificationCenter = NotificationCenter.default

        notificationCenter.addObserver(self, selector: #selector(keyboardWillChangeFrame(_:)),
                                       name: UIWindow.keyboardWillChangeFrameNotification,
                                       object: nil)
        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 makeKeyboardToolbar(canGoBackward: Bool, canGoForward: Bool) -> UIToolbar {
        var toolbarItems = UIBarButtonItem.makeKeyboardNavigationItems { (prevButton, nextButton) in
            prevButton.target = self
            prevButton.action = #selector(focusEmailTextField)
            prevButton.isEnabled = canGoBackward

            nextButton.target = self
            nextButton.action = #selector(focusDescriptionTextView)
            nextButton.isEnabled = canGoForward
        }

        toolbarItems.append(contentsOf: [
            UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
            UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissKeyboard))
        ])

        let toolbar = UIToolbar(frame: CGRect(x: 0, y: 0, width: 100, height: 44))
        toolbar.items = toolbarItems
        return toolbar
    }

    private func addConstraints() {
        self.activeMessageTextViewConstraints = [
            messageTextView.topAnchor.constraint(equalTo: view.topAnchor),
            messageTextView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            messageTextView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            messageTextView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ]

        self.inactiveMessageTextViewConstraints = [
            messageTextView.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 12),
            messageTextView.leadingAnchor.constraint(equalTo: textFieldsHolder.leadingAnchor),
            messageTextView.trailingAnchor.constraint(equalTo: textFieldsHolder.trailingAnchor),
            messageTextView.bottomAnchor.constraint(equalTo: textFieldsHolder.bottomAnchor),
        ]

        var constraints = [
            subheaderLabel.topAnchor.constraint(equalTo: containerView.layoutMarginsGuide.topAnchor),
            subheaderLabel.leadingAnchor.constraint(equalTo: containerView.layoutMarginsGuide.leadingAnchor),
            subheaderLabel.trailingAnchor.constraint(equalTo: containerView.layoutMarginsGuide.trailingAnchor),

            textFieldsHolder.topAnchor.constraint(equalTo: subheaderLabel.bottomAnchor, constant: 24),
            textFieldsHolder.leadingAnchor.constraint(equalTo: containerView.layoutMarginsGuide.leadingAnchor),
            textFieldsHolder.trailingAnchor.constraint(equalTo: containerView.layoutMarginsGuide.trailingAnchor),

            buttonsStackView.topAnchor.constraint(equalTo: textFieldsHolder.bottomAnchor, constant: 18),
            buttonsStackView.leadingAnchor.constraint(equalTo: containerView.layoutMarginsGuide.leadingAnchor),
            buttonsStackView.trailingAnchor.constraint(equalTo: containerView.layoutMarginsGuide.trailingAnchor),
            buttonsStackView.bottomAnchor.constraint(equalTo: containerView.layoutMarginsGuide.bottomAnchor),

            emailTextField.topAnchor.constraint(equalTo: textFieldsHolder.topAnchor),
            emailTextField.leadingAnchor.constraint(equalTo: textFieldsHolder.leadingAnchor),
            emailTextField.trailingAnchor.constraint(equalTo: textFieldsHolder.trailingAnchor),

            messagePlaceholder.topAnchor.constraint(equalTo: emailTextField.bottomAnchor, constant: 12),
            messagePlaceholder.leadingAnchor.constraint(equalTo: textFieldsHolder.leadingAnchor),
            messagePlaceholder.trailingAnchor.constraint(equalTo: textFieldsHolder.trailingAnchor),
            messagePlaceholder.bottomAnchor.constraint(equalTo: textFieldsHolder.bottomAnchor),
            messagePlaceholder.heightAnchor.constraint(equalTo: messageTextView.heightAnchor),

            scrollView.frameLayoutGuide.topAnchor.constraint(equalTo: view.topAnchor),
            scrollView.frameLayoutGuide.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            scrollView.frameLayoutGuide.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            scrollView.frameLayoutGuide.trailingAnchor.constraint(equalTo: view.trailingAnchor),

            scrollView.contentLayoutGuide.topAnchor.constraint(equalTo: containerView.topAnchor),
            scrollView.contentLayoutGuide.bottomAnchor.constraint(equalTo: containerView.bottomAnchor),
            scrollView.contentLayoutGuide.leadingAnchor.constraint(equalTo: containerView.leadingAnchor),
            scrollView.contentLayoutGuide.trailingAnchor.constraint(equalTo: containerView.trailingAnchor),

            scrollView.contentLayoutGuide.widthAnchor.constraint(equalTo: scrollView.frameLayoutGuide.widthAnchor),
            scrollView.contentLayoutGuide.heightAnchor.constraint(greaterThanOrEqualTo: scrollView.safeAreaLayoutGuide.heightAnchor),

            messageTextView.heightAnchor.constraint(greaterThanOrEqualToConstant: 150),
        ]

        constraints.append(contentsOf: self.inactiveMessageTextViewConstraints)

        NSLayoutConstraint.activate(constraints)
    }

    private func setDescriptionFieldExpanded(_ isExpanded: Bool) {
        if isExpanded {
            // Disable the large title
            self.navigationItem.largeTitleDisplayMode = .never

            // Move the text view above scroll view
            view.addSubview(self.messageTextView)

            // Re-add old constraints
            NSLayoutConstraint.activate(self.inactiveMessageTextViewConstraints)

            // Do a layout pass
            view.layoutIfNeeded()

            // Swap constraints
            NSLayoutConstraint.deactivate(self.inactiveMessageTextViewConstraints)
            NSLayoutConstraint.activate(self.activeMessageTextViewConstraints)

            // Enable content inset adjustment on text view
            self.messageTextView.contentInsetAdjustmentBehavior = .always

            // Animate constraints & rounded corners on the text view
            animateDescriptionTextView(animations: {
                // Turn off rounded corners as the text view fills in the entire view
                self.messageTextView.roundCorners = false

                self.view.layoutIfNeeded()
            }) { (completed) in
                self.isMessageTextViewExpanded = true

                self.updateMessageTextViewContentInsets()
            }

        } else {
            // Re-enable the large title
            self.navigationItem.largeTitleDisplayMode = .automatic

            // Swap constraints
            NSLayoutConstraint.deactivate(self.activeMessageTextViewConstraints)
            NSLayoutConstraint.activate(self.inactiveMessageTextViewConstraints)

            // Animate constraints & rounded corners on the text view
            animateDescriptionTextView(animations: {
                // Turn on rounded corners as the text view returns back to where it was
                self.messageTextView.roundCorners = true

                self.view.layoutIfNeeded()
            }) { (completed) in
                // Revert the content adjustment behavior
                self.messageTextView.contentInsetAdjustmentBehavior = .never

                // Add the text view inside of the scroll view
                self.textFieldsHolder.addSubview(self.messageTextView)

                self.isMessageTextViewExpanded = false
            }
        }
    }

    private func updateScrollViewContentInsets() {
        let scrollViewBottomInset = scrollViewBottomContentInsetAccountingForKeyboard

        scrollView.contentInset.bottom = scrollViewBottomInset
        scrollView.scrollIndicatorInsets.bottom = scrollViewBottomInset
    }

    private func updateMessageTextViewContentInsets() {
        // Ignore updating text view insets until it's fully expanded
        guard isMessageTextViewExpanded else { return }

        let textViewBottomInset: CGFloat

        if messageTextView.isFirstResponder {
            textViewBottomInset = scrollViewBottomContentInsetAccountingForKeyboard
        } else {
            textViewBottomInset = 0
        }

        messageTextView.contentInset.bottom = textViewBottomInset
        messageTextView.scrollIndicatorInsets.bottom = textViewBottomInset
    }

    private func animateDescriptionTextView(animations: @escaping () -> Void, completion: @escaping (Bool) -> Void) {
        UIView.animate(withDuration: 0.25, animations: animations) { (completed) in
            completion(completed)
        }
    }

    private func presentEmptyEmailConfirmationAlert(completion: @escaping (Bool) -> Void) {
        let message = NSLocalizedString("You are about to send the problem report without a way for us to get back to you. If you want an answer to your report you will have to enter an email address.", comment: "")

        let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)

        let cancelAction = UIAlertAction(title: NSLocalizedString("Cancel", comment: ""), style: .cancel) { _ in
            completion(false)
        }
        let sendAction = UIAlertAction(title: NSLocalizedString("Send anyway", comment: ""), style: .destructive) { _ in
            completion(true)
        }

        alertController.addAction(cancelAction)
        alertController.addAction(sendAction)

        present(alertController, animated: true)
    }

    // MARK: - Private: Problem report submission

    private var showsSubmissionOverlay = false

    private func showSubmissionOverlay() {
        guard !showsSubmissionOverlay else { return }

        self.showsSubmissionOverlay = true

        view.addSubview(submissionOverlayView)

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

        UIView.transition(from: scrollView, to: submissionOverlayView, duration: 0.25, options: [.showHideTransitionViews, .transitionCrossDissolve]) { (success) in
            // success
        }
    }

    private func hideSubmissionOverlay() {
        guard showsSubmissionOverlay else { return }

        self.showsSubmissionOverlay = false

        UIView.transition(from: submissionOverlayView, to: scrollView, duration: 0.25, options: [.showHideTransitionViews, .transitionCrossDissolve]) { (success) in
            // success
            self.submissionOverlayView.removeFromSuperview()
        }
    }

    // MARK: - Data model

    private struct ViewModel {
        let email: String
        let message: String

        init() {
            email = ""
            message = ""
        }

        init(email: String, message: String) {
            self.email = email.trimmingCharacters(in: .whitespacesAndNewlines)
            self.message = message.trimmingCharacters(in: .whitespacesAndNewlines)
        }

        var isValid: Bool {
            return !message.isEmpty
        }
    }

    private static var persistentViewModel = ViewModel()

    private func loadPersistentViewModel() {
        emailTextField.text = Self.persistentViewModel.email
        messageTextView.text = Self.persistentViewModel.message

        validateForm()
    }

    private func updatePersistentViewModel() {
        Self.persistentViewModel = ViewModel(
            email: emailTextField.text ?? "",
            message: messageTextView.text
        )

        validateForm()
    }

    private func clearPersistentViewModel() {
        Self.persistentViewModel = ViewModel()
    }

    // 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: ViewModel, result: Result<(), RestError>) {
        switch result {
        case .success:
            submissionOverlayView.state = .sent(viewModel.email)

            // Clear persistent view model upon successful submission
            clearPersistentViewModel()

        case .failure(let error):
            submissionOverlayView.state = .failure(error)
        }

        navigationItem.setHidesBackButton(false, animated: true)
    }

    // MARK: - Problem report submission helpers

    private func sendProblemReport() {
        let viewModel = Self.persistentViewModel

        willSendProblemReport()
        sendProblemReportHelper(with: viewModel) { [weak self] (result) in
            self?.didSendProblemReport(viewModel: viewModel, result: result)
        }
    }

    private func sendProblemReportHelper(with viewModel: ViewModel, completion: @escaping (Result<(), RestError>) -> Void) {
        let log = consolidatedLog.string
        let metadata = consolidatedLog.metadata.reduce(into: [:]) { (output, entry) in
            output[entry.key.rawValue] = entry.value
        }

        let request = ProblemReportRequest(address: viewModel.email, message: viewModel.message, log: log, metadata: metadata)
        let result = mullvadRest.sendProblemReport().dataTask(payload: request) { (result) in
            DispatchQueue.main.async {
                completion(result)
            }
        }

        switch result {
        case .success(let task):
            task.resume()
        case .failure(let error):
            completion(.failure(error))
        }
    }

    // MARK: - Input fields' notifications

    @objc private func messageTextViewDidBeginEditing() {
        setDescriptionFieldExpanded(true)
    }

    @objc private func messageTextViewDidEndEditing() {
        setDescriptionFieldExpanded(false)
    }

    @objc private func messageTextViewDidChange() {
        updatePersistentViewModel()
    }

    @objc private func emailTextFieldDidChange() {
        updatePersistentViewModel()
    }

    // MARK: - Keyboard notifications

    @objc private func keyboardWillChangeFrame(_ notification: Notification) {
        guard let keyboardFrameValue = notification.userInfo?[UIWindow.keyboardFrameEndUserInfoKey] as? NSValue else { return }

        let screenRect = self.view.convert(self.view.bounds, to: nil)

        keyboardIntersectionRect = screenRect.intersection(keyboardFrameValue.cgRectValue)

        updateScrollViewContentInsets()
        updateMessageTextViewContentInsets()
    }

    // MARK: - UITextFieldDelegate

    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        messageTextView.becomeFirstResponder()
        return false
    }

    // MARK: - ConditionalNavigation

    func shouldPopNavigationItem(_ navigationItem: UINavigationItem, trigger: NavigationPopTrigger) -> Bool {
        switch trigger {
        case .interactiveGesture:
            // Disable swipe when editing
            return !emailTextField.isFirstResponder && !messageTextView.isFirstResponder

        case .backButton:
            // Dismiss the keyboard to fix a visual glitch when moving back to the previous controller
            view.endEditing(true)
            return true
        }
    }

}