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
|
//
// SettingsNavigationController.swift
// MullvadVPN
//
// Created by pronebird on 02/07/2020.
// Copyright © 2020 Mullvad VPN AB. All rights reserved.
//
import Foundation
import UIKit
enum SettingsNavigationRoute {
case account
case preferences
case wireguardKeys
case problemReport
}
enum SettingsDismissReason {
case none
case userLoggedOut
}
protocol SettingsNavigationControllerDelegate: AnyObject {
func settingsNavigationController(_ controller: SettingsNavigationController, didFinishWithReason reason: SettingsDismissReason)
}
class SettingsNavigationController: CustomNavigationController, SettingsViewControllerDelegate, AccountViewControllerDelegate, UIAdaptivePresentationControllerDelegate {
weak var settingsDelegate: SettingsNavigationControllerDelegate?
override var childForStatusBarStyle: UIViewController? {
return topViewController
}
override var childForStatusBarHidden: UIViewController? {
return topViewController
}
init() {
super.init(navigationBarClass: CustomNavigationBar.self, toolbarClass: nil)
let settingsController = SettingsViewController()
settingsController.delegate = self
pushViewController(settingsController, animated: false)
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?) {
// This initializer exists to prevent crash on iOS 12.
// See: https://stackoverflow.com/a/38335090/351305
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override func viewDidLoad() {
super.viewDidLoad()
navigationBar.prefersLargeTitles = true
// Update account expiry
Account.shared.updateAccountExpiry()
}
// MARK: - SettingsViewControllerDelegate
func settingsViewControllerDidFinish(_ controller: SettingsViewController) {
self.settingsDelegate?.settingsNavigationController(self, didFinishWithReason: .none)
}
// MARK: - AccountViewControllerDelegate
func accountViewControllerDidLogout(_ controller: AccountViewController) {
self.settingsDelegate?.settingsNavigationController(self, didFinishWithReason: .userLoggedOut)
}
// MARK: - Navigation
func navigate(to route: SettingsNavigationRoute, animated: Bool) {
let nextViewController = makeViewController(for: route)
if let rootController = self.viewControllers.first, viewControllers.count > 1 {
setViewControllers([rootController, nextViewController], animated: animated)
} else {
pushViewController(nextViewController, animated: animated)
}
}
private func makeViewController(for route: SettingsNavigationRoute) -> UIViewController {
switch route {
case .account:
let controller = AccountViewController()
controller.delegate = self
return controller
case .preferences:
return PreferencesViewController()
case .wireguardKeys:
return WireguardKeysViewController()
case .problemReport:
return ProblemReportViewController()
}
}
// MARK: - UIAdaptivePresentationControllerDelegate
func presentationControllerDidDismiss(_ presentationController: UIPresentationController) {
settingsDelegate?.settingsNavigationController(self, didFinishWithReason: .none)
}
}
|