-
Notifications
You must be signed in to change notification settings - Fork 111
/
SurveyCoordinatingController.swift
95 lines (76 loc) · 3.33 KB
/
SurveyCoordinatingController.swift
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
import UIKit
import protocol WooFoundation.Analytics
/// Controls navigation for the in-app feedback flow. Meant to be presented modally
///
final class SurveyCoordinatingController: WooNavigationController {
/// Used to present the Contact Support dialog
private let zendeskManager: ZendeskManagerProtocol
/// Factory that creates view controllers needed for this flow
private let viewControllersFactory: SurveyViewControllersFactoryProtocol
private let analytics: Analytics
/// Is true when `self` is being dismissed because the user has finished the survey.
private var receivedSurveyFinishedEvent: Bool = false
/// What kind of survey to present.
private let survey: SurveyViewController.Source
private let onDismiss: (() -> Void)?
init(survey: SurveyViewController.Source,
zendeskManager: ZendeskManagerProtocol = ZendeskProvider.shared,
viewControllersFactory: SurveyViewControllersFactoryProtocol = SurveyViewControllersFactory(),
analytics: Analytics = ServiceLocator.analytics,
onDismiss: (() -> Void)? = nil) {
self.survey = survey
self.zendeskManager = zendeskManager
self.viewControllersFactory = viewControllersFactory
self.analytics = analytics
self.onDismiss = onDismiss
super.init(nibName: nil, bundle: nil)
startSurveyNavigation()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
if isBeingDismissed && !receivedSurveyFinishedEvent {
analytics.track(event: .surveyScreen(context: survey.feedbackContextForEvents, action: .canceled))
}
onDismiss?()
}
}
// MARK: Navigation
private extension SurveyCoordinatingController {
/// Starts navigation with `SurveyViewController` as root view controller.
///
func startSurveyNavigation() {
analytics.track(event: .surveyScreen(context: survey.feedbackContextForEvents, action: .opened))
let surveyViewController = viewControllersFactory.makeSurveyViewController(survey: survey) { [weak self] in
guard let self = self else {
return
}
self.receivedSurveyFinishedEvent = true
self.analytics.track(event: .surveyScreen(context: self.survey.feedbackContextForEvents,
action: .completed))
self.navigateToSurveySubmitted()
}
setViewControllers([surveyViewController], animated: false)
}
/// Proceeds navigation to `SurveySubmittedViewController`
///
func navigateToSurveySubmitted() {
let completionViewController = viewControllersFactory.makeSurveySubmittedViewController(onContactUsAction: { [weak self] in
guard let self = self else {
return
}
let supportForm = SupportFormHostingController(viewModel: .init())
supportForm.show(from: self)
}, onBackToStoreAction: { [weak self] in
self?.finishSurveyNavigation()
})
setViewControllers([completionViewController], animated: true)
}
/// Dismisses the flow modally
///
func finishSurveyNavigation() {
dismiss(animated: true)
}
}