diff options
| author | Andrej Mihajlov <and@mullvad.net> | 2021-04-08 13:18:19 +0200 |
|---|---|---|
| committer | Andrej Mihajlov <and@mullvad.net> | 2021-04-09 18:48:25 +0200 |
| commit | e49b8da84c2d6206b7eb36020692ce1ef64b7898 (patch) | |
| tree | a35f401d3960b973467eeece964876ccdeec79f8 | |
| parent | 454c60a1f1e5b8089af04fbc8becb5d855cf982a (diff) | |
| download | mullvadvpn-e49b8da84c2d6206b7eb36020692ce1ef64b7898.tar.xz mullvadvpn-e49b8da84c2d6206b7eb36020692ce1ef64b7898.zip | |
Settings: rework navigation
| -rw-r--r-- | ios/MullvadVPN/SettingsNavigationController.swift | 72 | ||||
| -rw-r--r-- | ios/MullvadVPN/SettingsViewController.swift | 61 |
2 files changed, 90 insertions, 43 deletions
diff --git a/ios/MullvadVPN/SettingsNavigationController.swift b/ios/MullvadVPN/SettingsNavigationController.swift index b582b78bca..e3b4ac4355 100644 --- a/ios/MullvadVPN/SettingsNavigationController.swift +++ b/ios/MullvadVPN/SettingsNavigationController.swift @@ -9,7 +9,43 @@ import Foundation import UIKit -class SettingsNavigationController: CustomNavigationController { +enum SettingsNavigationRoute { + case account + case wireguardKeys + case problemReport +} + +enum SettingsDismissReason { + case none + case userLoggedOut +} + +protocol SettingsNavigationControllerDelegate: class { + func settingsNavigationController(_ controller: SettingsNavigationController, didFinishWithReason reason: SettingsDismissReason) +} + +class SettingsNavigationController: CustomNavigationController, SettingsViewControllerDelegate, AccountViewControllerDelegate, UIAdaptivePresentationControllerDelegate { + + weak var settingsDelegate: SettingsNavigationControllerDelegate? + + 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() @@ -22,4 +58,38 @@ class SettingsNavigationController: CustomNavigationController { 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) { + switch route { + case .account: + let controller = AccountViewController() + controller.delegate = self + pushViewController(controller, animated: animated) + + case .wireguardKeys: + pushViewController(WireguardKeysViewController(), animated: animated) + + case .problemReport: + pushViewController(ProblemReportViewController(), animated: animated) + } + } + + // MARK: - UIAdaptivePresentationControllerDelegate + + func presentationControllerDidDismiss(_ presentationController: UIPresentationController) { + settingsDelegate?.settingsNavigationController(self, didFinishWithReason: .none) + } } diff --git a/ios/MullvadVPN/SettingsViewController.swift b/ios/MullvadVPN/SettingsViewController.swift index af843a1fd0..58a382e60e 100644 --- a/ios/MullvadVPN/SettingsViewController.swift +++ b/ios/MullvadVPN/SettingsViewController.swift @@ -9,21 +9,13 @@ import Foundation import UIKit -enum SettingsNavigationRoute { - case account - case wireguardKeys -} - -enum SettingsDismissReason { - case none - case userLoggedOut -} - protocol SettingsViewControllerDelegate: class { - func settingsViewController(_ controller: SettingsViewController, didFinishWithReason reason: SettingsDismissReason) + func settingsViewControllerDidFinish(_ controller: SettingsViewController) } -class SettingsViewController: UITableViewController, AccountViewControllerDelegate { +class SettingsViewController: UITableViewController { + + weak var delegate: SettingsViewControllerDelegate? private enum CellIdentifier: String { case accountCell = "AccountCell" @@ -35,7 +27,17 @@ class SettingsViewController: UITableViewController, AccountViewControllerDelega private weak var accountRow: StaticTableViewRow? private var accountExpiryObserver: NSObjectProtocol? - weak var settingsDelegate: SettingsViewControllerDelegate? + private var settingsNavigationController: SettingsNavigationController? { + return self.navigationController as? SettingsNavigationController + } + + init() { + super.init(style: .grouped) + } + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } override func viewDidLoad() { super.viewDidLoad() @@ -72,30 +74,7 @@ class SettingsViewController: UITableViewController, AccountViewControllerDelega // MARK: - IBActions @IBAction func handleDismiss() { - settingsDelegate?.settingsViewController(self, didFinishWithReason: .none) - } - - // MARK: - Navigation - - func navigate(to route: SettingsNavigationRoute) { - switch route { - case .account: - let controller = AccountViewController() - controller.delegate = self - - navigationController?.pushViewController(controller, animated: true) - - case .wireguardKeys: - let controller = WireguardKeysViewController() - - navigationController?.pushViewController(controller, animated: true) - } - } - - // MARK: - AccountViewControllerDelegate - - func accountViewControllerDidLogout(_ controller: AccountViewController) { - settingsDelegate?.settingsViewController(self, didFinishWithReason: .userLoggedOut) + delegate?.settingsViewControllerDidFinish(self) } // MARK: - Private @@ -113,7 +92,7 @@ class SettingsViewController: UITableViewController, AccountViewControllerDelega } accountRow.actionBlock = { [weak self] (indexPath) in - self?.navigate(to: .account) + self?.settingsNavigationController?.navigate(to: .account, animated: true) } let wireguardKeyRow = StaticTableViewRow(reuseIdentifier: CellIdentifier.basicCell.rawValue) { (_, cell) in @@ -125,7 +104,7 @@ class SettingsViewController: UITableViewController, AccountViewControllerDelega } wireguardKeyRow.actionBlock = { [weak self] (indexPath) in - self?.navigate(to: .wireguardKeys) + self?.settingsNavigationController?.navigate(to: .wireguardKeys, animated: true) } self.accountRow = accountRow @@ -172,9 +151,7 @@ class SettingsViewController: UITableViewController, AccountViewControllerDelega } problemReportRow.actionBlock = { [weak self] (indexPath) in - let controller = ProblemReportViewController() - - self?.navigationController?.pushViewController(controller, animated: true) + self?.settingsNavigationController?.navigate(to: .problemReport, animated: true) } bottomSection.addRows([problemReportRow]) |
