diff options
| author | Bug Magnet <marco.nikic@mullvad.net> | 2024-05-13 10:14:38 +0200 |
|---|---|---|
| committer | Bug Magnet <marco.nikic@mullvad.net> | 2024-05-13 10:14:38 +0200 |
| commit | f91ba00a66eb8b253f7bc1e352786ad14fee7e02 (patch) | |
| tree | ab4d8804705d1d219b625245b83bae34ac801f6a /ios/MullvadVPN/View controllers/SelectLocation | |
| parent | c828a96a25b61a07fa2e72565fa3dd709b79aa76 (diff) | |
| parent | 2ad27f50a678ca06da7cb55c637890bd25692b34 (diff) | |
| download | mullvadvpn-f91ba00a66eb8b253f7bc1e352786ad14fee7e02.tar.xz mullvadvpn-f91ba00a66eb8b253f7bc1e352786ad14fee7e02.zip | |
Merge branch 'add-a-new-container-view-to-wrap-the-whole-location-ios-630'
Diffstat (limited to 'ios/MullvadVPN/View controllers/SelectLocation')
| -rw-r--r-- | ios/MullvadVPN/View controllers/SelectLocation/LocationViewController.swift | 57 | ||||
| -rw-r--r-- | ios/MullvadVPN/View controllers/SelectLocation/LocationViewControllerWrapper.swift | 156 |
2 files changed, 169 insertions, 44 deletions
diff --git a/ios/MullvadVPN/View controllers/SelectLocation/LocationViewController.swift b/ios/MullvadVPN/View controllers/SelectLocation/LocationViewController.swift index fa787f86c6..38188122ff 100644 --- a/ios/MullvadVPN/View controllers/SelectLocation/LocationViewController.swift +++ b/ios/MullvadVPN/View controllers/SelectLocation/LocationViewController.swift @@ -6,14 +6,15 @@ // Copyright © 2019 Mullvad VPN AB. All rights reserved. // -import MullvadLogging import MullvadREST import MullvadSettings import MullvadTypes import UIKit protocol LocationViewControllerDelegate: AnyObject { - func didRequestRouteToCustomLists(_ controller: LocationViewController, nodes: [LocationNode]) + func navigateToCustomLists(nodes: [LocationNode]) + func didSelectRelays(relays: UserSelectedRelays) + func didUpdateFilter(filter: RelayFilter) } final class LocationViewController: UIViewController { @@ -24,7 +25,7 @@ final class LocationViewController: UIViewController { private var dataSource: LocationDataSource? private var cachedRelays: CachedRelays? private var filter = RelayFilter() - var relayLocations: UserSelectedRelays? + private var selectedRelays: UserSelectedRelays? weak var delegate: LocationViewControllerDelegate? var customListRepository: CustomListRepositoryProtocol @@ -36,13 +37,9 @@ final class LocationViewController: UIViewController { return (filter.ownership == .any) && (filter.providers == .any) } - var navigateToFilter: (() -> Void)? - var didSelectRelays: ((UserSelectedRelays) -> Void)? - var didUpdateFilter: ((RelayFilter) -> Void)? - var didFinish: (() -> Void)? - - init(customListRepository: CustomListRepositoryProtocol) { + init(customListRepository: CustomListRepositoryProtocol, selectedRelays: UserSelectedRelays?) { self.customListRepository = customListRepository + self.selectedRelays = selectedRelays super.init(nibName: nil, bundle: nil) } @@ -58,34 +55,6 @@ final class LocationViewController: UIViewController { view.accessibilityIdentifier = .selectLocationView view.backgroundColor = .secondaryColor - navigationItem.title = NSLocalizedString( - "NAVIGATION_TITLE", - tableName: "SelectLocation", - value: "Select location", - comment: "" - ) - - navigationItem.leftBarButtonItem = UIBarButtonItem( - title: NSLocalizedString( - "NAVIGATION_TITLE", - tableName: "SelectLocation", - value: "Filter", - comment: "" - ), - primaryAction: UIAction(handler: { [weak self] _ in - self?.navigateToFilter?() - }) - ) - navigationItem.leftBarButtonItem?.accessibilityIdentifier = .selectLocationFilterButton - - navigationItem.rightBarButtonItem = UIBarButtonItem( - systemItem: .done, - primaryAction: UIAction(handler: { [weak self] _ in - self?.didFinish?() - }) - ) - navigationItem.rightBarButtonItem?.accessibilityIdentifier = .closeSelectLocationButton - setUpDataSources() setUpTableView() setUpTopContent() @@ -94,7 +63,7 @@ final class LocationViewController: UIViewController { topContentView.pinEdgesToSuperviewMargins(.all().excluding(.bottom)) tableView.pinEdgesToSuperview(.all().excluding(.top)) - tableView.topAnchor.constraint(equalTo: topContentView.bottomAnchor) + tableView.topAnchor.constraint(equalTo: topContentView.bottomAnchor, constant: 8) } } @@ -121,11 +90,11 @@ final class LocationViewController: UIViewController { filterView.setFilter(filter) } - dataSource?.setRelays(cachedRelays.relays, selectedRelays: relayLocations, filter: filter) + dataSource?.setRelays(cachedRelays.relays, selectedRelays: selectedRelays, filter: filter) } func refreshCustomLists() { - dataSource?.refreshCustomLists(selectedRelays: relayLocations) + dataSource?.refreshCustomLists(selectedRelays: selectedRelays) } // MARK: - Private @@ -138,7 +107,7 @@ final class LocationViewController: UIViewController { ) dataSource?.didSelectRelayLocations = { [weak self] locations in - self?.didSelectRelays?(locations) + self?.delegate?.didSelectRelays(relays: locations) } dataSource?.didTapEditCustomLists = { [weak self] in @@ -147,12 +116,12 @@ final class LocationViewController: UIViewController { if let cachedRelays { let allLocationDataSource = AllLocationDataSource() allLocationDataSource.reload(cachedRelays.relays, relays: cachedRelays.relays.wireguard.relays) - delegate?.didRequestRouteToCustomLists(self, nodes: allLocationDataSource.nodes) + delegate?.navigateToCustomLists(nodes: allLocationDataSource.nodes) } } if let cachedRelays { - dataSource?.setRelays(cachedRelays.relays, selectedRelays: relayLocations, filter: filter) + dataSource?.setRelays(cachedRelays.relays, selectedRelays: selectedRelays, filter: filter) } } @@ -178,7 +147,7 @@ final class LocationViewController: UIViewController { guard let self else { return } filter = $0 - didUpdateFilter?($0) + delegate?.didUpdateFilter(filter: $0) if let cachedRelays { setCachedRelays(cachedRelays, filter: filter) diff --git a/ios/MullvadVPN/View controllers/SelectLocation/LocationViewControllerWrapper.swift b/ios/MullvadVPN/View controllers/SelectLocation/LocationViewControllerWrapper.swift new file mode 100644 index 0000000000..333d965f87 --- /dev/null +++ b/ios/MullvadVPN/View controllers/SelectLocation/LocationViewControllerWrapper.swift @@ -0,0 +1,156 @@ +// +// LocationViewControllerWrapper.swift +// MullvadVPN +// +// Created by Jon Petersson on 2024-04-23. +// Copyright © 2024 Mullvad VPN AB. All rights reserved. +// + +import MullvadREST +import MullvadSettings +import MullvadTypes +import UIKit + +protocol LocationViewControllerWrapperDelegate: AnyObject { + func navigateToCustomLists(nodes: [LocationNode]) + func navigateToFilter() + func didSelectRelays(relays: UserSelectedRelays) + func didUpdateFilter(filter: RelayFilter) +} + +final class LocationViewControllerWrapper: UIViewController { + private let locationViewController: LocationViewController + private let segmentedControl = UISegmentedControl() + + weak var delegate: LocationViewControllerWrapperDelegate? + + init(customListRepository: CustomListRepositoryProtocol, selectedRelays: UserSelectedRelays?) { + locationViewController = LocationViewController( + customListRepository: customListRepository, + selectedRelays: selectedRelays + ) + + super.init(nibName: nil, bundle: nil) + + locationViewController.delegate = self + } + + var didFinish: (() -> Void)? + + required init?(coder: NSCoder) { + fatalError("init(coder:) has not been implemented") + } + + override func viewDidLoad() { + super.viewDidLoad() + + view.accessibilityIdentifier = .selectLocationViewWrapper + view.backgroundColor = .secondaryColor + + setUpNavigation() + setUpSegmentedControl() + addSubviews() + } + + func setCachedRelays(_ cachedRelays: CachedRelays, filter: RelayFilter) { + locationViewController.setCachedRelays(cachedRelays, filter: filter) + } + + func refreshCustomLists() { + locationViewController.refreshCustomLists() + } + + private func setUpNavigation() { + navigationItem.largeTitleDisplayMode = .never + + navigationItem.title = NSLocalizedString( + "NAVIGATION_TITLE", + tableName: "SelectLocation", + value: "Select location", + comment: "" + ) + + navigationItem.leftBarButtonItem = UIBarButtonItem( + title: NSLocalizedString( + "NAVIGATION_FILTER", + tableName: "SelectLocation", + value: "Filter", + comment: "" + ), + primaryAction: UIAction(handler: { [weak self] _ in + self?.delegate?.navigateToFilter() + }) + ) + navigationItem.leftBarButtonItem?.accessibilityIdentifier = .selectLocationFilterButton + + navigationItem.rightBarButtonItem = UIBarButtonItem( + systemItem: .done, + primaryAction: UIAction(handler: { [weak self] _ in + self?.didFinish?() + }) + ) + navigationItem.rightBarButtonItem?.accessibilityIdentifier = .closeSelectLocationButton + } + + private func setUpSegmentedControl() { + segmentedControl.backgroundColor = .SegmentedControl.backgroundColor + segmentedControl.selectedSegmentTintColor = .SegmentedControl.selectedColor + segmentedControl.setTitleTextAttributes([ + .foregroundColor: UIColor.white, + .font: UIFont.systemFont(ofSize: 17, weight: .medium), + ], for: .normal) + + segmentedControl.insertSegment(withTitle: NSLocalizedString( + "MULTIHOP_TAB_ENTRY", + tableName: "SelectLocation", + value: "Entry", + comment: "" + ), at: 0, animated: false) + segmentedControl.insertSegment(withTitle: NSLocalizedString( + "MULTIHOP_TAB_EXIT", + tableName: "SelectLocation", + value: "Exit", + comment: "" + ), at: 1, animated: false) + + segmentedControl.selectedSegmentIndex = 0 + segmentedControl.addTarget(self, action: #selector(segmentedControlDidChange), for: .valueChanged) + } + + private func addSubviews() { + addChild(locationViewController) + locationViewController.didMove(toParent: self) + + view.addConstrainedSubviews([segmentedControl, locationViewController.view]) { + segmentedControl.heightAnchor.constraint(equalToConstant: 44) + segmentedControl.pinEdgesToSuperviewMargins(PinnableEdges([.top(0), .leading(8), .trailing(8)])) + + locationViewController.view.pinEdgesToSuperview(.all().excluding(.top)) + + #if DEBUG + locationViewController.view.topAnchor.constraint(equalTo: segmentedControl.bottomAnchor, constant: 4) + #else + locationViewController.view.pinEdgeToSuperviewMargin(.top(0)) + #endif + } + } + + @objc + private func segmentedControlDidChange(sender: UISegmentedControl) { + refreshCustomLists() + } +} + +extension LocationViewControllerWrapper: LocationViewControllerDelegate { + func navigateToCustomLists(nodes: [LocationNode]) { + delegate?.navigateToCustomLists(nodes: nodes) + } + + func didSelectRelays(relays: UserSelectedRelays) { + delegate?.didSelectRelays(relays: relays) + } + + func didUpdateFilter(filter: RelayFilter) { + delegate?.didUpdateFilter(filter: filter) + } +} |
