-
Notifications
You must be signed in to change notification settings - Fork 356
/
SettingsCoordinator.swift
290 lines (238 loc) · 9.73 KB
/
SettingsCoordinator.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
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
//
// SettingsCoordinator.swift
// MullvadVPN
//
// Created by pronebird on 09/01/2023.
// Copyright © 2023 Mullvad VPN AB. All rights reserved.
//
import MullvadLogging
import MullvadSettings
import Operations
import Routing
import UIKit
/// Settings navigation route.
enum SettingsNavigationRoute: Equatable {
/// The route that's always displayed first upon entering settings.
case root
/// VPN settings.
case vpnSettings
/// Problem report.
case problemReport
/// FAQ section displayed as a modal safari browser.
case faq
/// API access route.
case apiAccess
}
/// Top-level settings coordinator.
final class SettingsCoordinator: Coordinator, Presentable, Presenting, SettingsViewControllerDelegate,
UINavigationControllerDelegate {
private let logger = Logger(label: "SettingsNavigationCoordinator")
private let interactorFactory: SettingsInteractorFactory
private var currentRoute: SettingsNavigationRoute?
private var modalRoute: SettingsNavigationRoute?
private let accessMethodRepository: AccessMethodRepositoryProtocol
private let proxyConfigurationTester: ProxyConfigurationTesterProtocol
private let ipOverrideRepository: IPOverrideRepository
let navigationController: UINavigationController
var presentedViewController: UIViewController {
navigationController
}
/// Event handler invoked when navigating bebtween child routes within the horizontal stack.
var willNavigate: ((
_ coordinator: SettingsCoordinator,
_ from: SettingsNavigationRoute?,
_ to: SettingsNavigationRoute
) -> Void)?
/// Event handler invoked when coordinator and its view hierarchy should be dismissed.
var didFinish: ((SettingsCoordinator) -> Void)?
/// Designated initializer.
/// - Parameters:
/// - navigationController: a navigation controller that the coordinator will be managing.
/// - interactorFactory: an instance of a factory that produces interactors for the child routes.
init(
navigationController: UINavigationController,
interactorFactory: SettingsInteractorFactory,
accessMethodRepository: AccessMethodRepositoryProtocol,
proxyConfigurationTester: ProxyConfigurationTesterProtocol,
ipOverrideRepository: IPOverrideRepository
) {
self.navigationController = navigationController
self.interactorFactory = interactorFactory
self.accessMethodRepository = accessMethodRepository
self.proxyConfigurationTester = proxyConfigurationTester
self.ipOverrideRepository = ipOverrideRepository
}
/// Start the coordinator fllow.
/// - Parameter initialRoute: the initial route to display.
func start(initialRoute: SettingsNavigationRoute? = nil) {
navigationController.navigationBar.prefersLargeTitles = true
navigationController.delegate = self
push(from: makeChild(for: .root), animated: false)
if let initialRoute, initialRoute != .root {
push(from: makeChild(for: initialRoute), animated: false)
}
}
// MARK: - Navigation
/// Request navigation to the speciifc route.
///
/// - Parameters:
/// - route: the route to present.
/// - animated: whether transition should be animated.
/// - completion: a completion handler, typically called immediately for horizontal navigation and
func navigate(to route: SettingsNavigationRoute, animated: Bool, completion: (() -> Void)? = nil) {
switch route {
case .root:
popToRoot(animated: animated)
completion?()
case .faq:
guard modalRoute == nil else {
completion?()
return
}
modalRoute = route
logger.debug("Show modal \(route)")
let safariCoordinator = SafariCoordinator(url: ApplicationConfiguration.faqAndGuidesURL)
safariCoordinator.didFinish = { [weak self] in
self?.modalRoute = nil
}
presentChild(safariCoordinator, animated: animated, completion: completion)
default:
// Ignore navigation if the route is already presented.
guard currentRoute != route else {
completion?()
return
}
let child = makeChild(for: route)
// Pop to root first, then push the child.
if navigationController.viewControllers.count > 1 {
popToRoot(animated: animated)
}
push(from: child, animated: animated)
completion?()
}
}
// MARK: - UINavigationControllerDelegate
func navigationController(
_ navigationController: UINavigationController,
willShow viewController: UIViewController,
animated: Bool
) {
/*
Navigation controller calls this delegate method on `viewWillAppear`, for instance during cancellation
of interactive dismissal of a modally presented settings navigation controller, so it's important that we
ignore repeating routes.
*/
guard let route = route(for: viewController), currentRoute != route else { return }
logger.debug(
"Navigate from \(currentRoute.map { "\($0)" } ?? "none") -> \(route)"
)
willNavigate?(self, currentRoute, route)
currentRoute = route
// Release child coordinators when moving to root.
if case .root = route {
releaseChildren()
}
}
// MARK: - SettingsViewControllerDelegate
func settingsViewControllerDidFinish(_ controller: SettingsViewController) {
didFinish?(self)
}
func settingsViewController(
_ controller: SettingsViewController,
didRequestRoutePresentation route: SettingsNavigationRoute
) {
navigate(to: route, animated: true)
}
// MARK: - Route handling
/// Pop to root route.
/// - Parameter animated: whether to animate the transition.
private func popToRoot(animated: Bool) {
navigationController.popToRootViewController(animated: animated)
releaseChildren()
}
/// Push the child into navigation stack.
/// - Parameters:
/// - result: the result of creating a child representing a route.
/// - animated: whether to animate the transition.
private func push(from result: MakeChildResult, animated: Bool) {
switch result {
case let .viewController(vc):
navigationController.pushViewController(vc, animated: animated)
case let .childCoordinator(child):
addChild(child)
child.start(animated: animated)
case .failed:
break
}
}
/// Release all child coordinators conforming to ``SettingsChildCoordinator`` protocol.
private func releaseChildren() {
childCoordinators.forEach { coordinator in
if coordinator is SettingsChildCoordinator {
coordinator.removeFromParent()
}
}
}
// MARK: - Route mapping
/// The result of creating a child representing a route.
private enum MakeChildResult {
/// View controller that should be pushed into navigation stack.
case viewController(UIViewController)
/// Child coordinator that should be added to the children hierarchy.
/// The child is responsile for presenting itself.
case childCoordinator(SettingsChildCoordinator)
/// Failure to produce a child.
case failed
}
/// Produce a view controller or a child coordinator representing the route.
/// - Parameter route: the route for which to request the new view controller or child coordinator.
/// - Returns: a result of creating a child for the route.
private func makeChild(for route: SettingsNavigationRoute) -> MakeChildResult {
switch route {
case .root:
let controller = SettingsViewController(
interactor: interactorFactory.makeSettingsInteractor(),
alertPresenter: AlertPresenter(context: self)
)
controller.delegate = self
return .viewController(controller)
case .vpnSettings:
return .childCoordinator(VPNSettingsCoordinator(
navigationController: navigationController,
interactorFactory: interactorFactory,
ipOverrideRepository: ipOverrideRepository
))
case .problemReport:
return .viewController(ProblemReportViewController(
interactor: interactorFactory.makeProblemReportInteractor(),
alertPresenter: AlertPresenter(context: self)
))
case .apiAccess:
return .childCoordinator(ListAccessMethodCoordinator(
navigationController: navigationController,
accessMethodRepository: accessMethodRepository,
proxyConfigurationTester: proxyConfigurationTester
))
case .faq:
// Handled separately and presented as a modal.
return .failed
}
}
/// Map the view controller to the individual route.
/// - Parameter viewController: an instance of a view controller within the navigation stack.
/// - Returns: a route upon success, otherwise `nil`.
private func route(for viewController: UIViewController) -> SettingsNavigationRoute? {
switch viewController {
case is SettingsViewController:
return .root
case is VPNSettingsViewController:
return .vpnSettings
case is ProblemReportViewController:
return .problemReport
case is ListAccessMethodViewController:
return .apiAccess
default:
return nil
}
}
}