-
Notifications
You must be signed in to change notification settings - Fork 328
/
LocationViewControllerWrapper.swift
154 lines (125 loc) · 4.85 KB
/
LocationViewControllerWrapper.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
//
// 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.rightBarButtonItem = UIBarButtonItem(
systemItem: .done,
primaryAction: UIAction(handler: { [weak self] _ in
self?.didFinish?()
})
)
}
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(
"SEGMENTED_CONTROL_ENTRY",
tableName: "SelectLocation",
value: "Entry",
comment: ""
), at: 0, animated: false)
segmentedControl.insertSegment(withTitle: NSLocalizedString(
"SEGMENTED_CONTROL_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)
}
}