summaryrefslogtreecommitdiffhomepage
path: root/ios/MullvadVPN/View controllers/Tunnel/MapViewController.swift
blob: 6104a4bb277d9217a036f85247d0e441337932b6 (plain)
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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
//
//  MapViewController.swift
//  MullvadVPN
//
//  Created by pronebird on 03/01/2023.
//  Copyright © 2025 Mullvad VPN AB. All rights reserved.
//

import Foundation
import MapKit
import MullvadLogging
import Operations

private let locationMarkerReuseIdentifier = "location"
private let geoJSONSourceFileName = "countries.geo.json"

final class MapViewController: UIViewController, MKMapViewDelegate {
    private let logger = Logger(label: "MapViewController")
    private let animationQueue = AsyncOperationQueue.makeSerial()

    private let locationMarker = MKPointAnnotation()
    private var willChangeRegion = false
    private var regionDidChangeCompletion: (() -> Void)?
    private let mapView = MKMapView()
    private var isFirstLayoutPass = true
    private var center: CLLocationCoordinate2D?
    var alignmentView: UIView?

    // MARK: - View lifecycle

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView.delegate = self
        mapView.register(
            MKAnnotationView.self,
            forAnnotationViewWithReuseIdentifier: locationMarkerReuseIdentifier
        )

        mapView.showsUserLocation = false
        mapView.isZoomEnabled = false
        mapView.isScrollEnabled = false
        mapView.isUserInteractionEnabled = false
        mapView.accessibilityElementsHidden = true

        // Use dark style for the map to dim the map grid
        mapView.overrideUserInterfaceStyle = .dark

        addTileOverlay()
        loadGeoJSONData()
        addMapView()
    }

    override func viewWillTransition(
        to size: CGSize,
        with coordinator: UIViewControllerTransitionCoordinator
    ) {
        super.viewWillTransition(to: size, with: coordinator)

        coordinator.animate(
            alongsideTransition: nil,
            completion: { context in
                self.recomputeVisibleRegion(animated: context.isAnimated)
            })
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()

        if isFirstLayoutPass {
            isFirstLayoutPass = false
            recomputeVisibleRegion(animated: false)
        }
    }

    // MARK: - Public

    func addLocationMarker(coordinate: CLLocationCoordinate2D) {
        locationMarker.coordinate = coordinate
        mapView.addAnnotation(locationMarker)
    }

    func removeLocationMarker() {
        mapView.removeAnnotation(locationMarker)
    }

    func setCenter(
        _ center: CLLocationCoordinate2D?,
        animated: Bool,
        completion: (() -> Void)? = nil
    ) {
        enqueueAnimation(cancelOtherAnimations: true) { finish in
            self.setCenterInternal(center, animated: animated) {
                finish()
                completion?()
            }
        }
    }

    // MARK: - MKMapViewDelegate

    func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
        if let polygon = overlay as? MKPolygon {
            let renderer = MKPolygonRenderer(polygon: polygon)
            renderer.fillColor = .primaryColor
            renderer.strokeColor = .secondaryColor
            renderer.lineWidth = 1
            renderer.lineCap = .round
            renderer.lineJoin = .round
            return renderer
        }

        if let tileOverlay = overlay as? MKTileOverlay {
            return CustomOverlayRenderer(overlay: tileOverlay)
        }

        return MKOverlayRenderer()
    }

    func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
        guard annotation === locationMarker else { return nil }

        let view = mapView.dequeueReusableAnnotationView(
            withIdentifier: locationMarkerReuseIdentifier,
            for: annotation
        )
        view.isDraggable = false
        view.canShowCallout = false
        view.image = UIImage(named: "LocationMarkerSecure")

        return view
    }

    func mapView(_ mapView: MKMapView, regionWillChangeAnimated animated: Bool) {
        willChangeRegion = true
    }

    func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {
        willChangeRegion = false

        let handler = regionDidChangeCompletion
        regionDidChangeCompletion = nil
        handler?()
    }

    // MARK: - Private

    private func addMapView() {
        mapView.translatesAutoresizingMaskIntoConstraints = false
        view.addSubview(mapView)

        NSLayoutConstraint.activate([
            mapView.topAnchor.constraint(equalTo: view.topAnchor),
            mapView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            mapView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
            mapView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])
    }

    private func addTileOverlay() {
        let tileOverlay = MKTileOverlay(urlTemplate: nil)
        tileOverlay.canReplaceMapContent = true

        mapView.addOverlay(tileOverlay, level: .aboveLabels)
    }

    private func loadGeoJSONData() {
        guard let fileURL = Bundle.main.url(forResource: geoJSONSourceFileName, withExtension: nil)
        else {
            logger.debug("Failed to locate \(geoJSONSourceFileName) in main bundle.")
            return
        }

        do {
            let data = try Data(contentsOf: fileURL)
            guard let features = try MKGeoJSONDecoder().decode(data) as? [MKGeoJSONFeature]
            else { return }

            var overlays = [MKOverlay]()

            for feature in features {
                for geometry in feature.geometry {
                    if let polygon = geometry as? MKPolygon {
                        if let interiorPolygons = polygon.interiorPolygons,
                            !interiorPolygons.isEmpty
                        {
                            overlays
                                .append(
                                    MKPolygon(
                                        points: polygon.points(),
                                        count: polygon.pointCount
                                    ))
                            overlays.append(contentsOf: interiorPolygons)
                        } else {
                            overlays.append(polygon)
                        }
                    }

                    if let multiPolygon = geometry as? MKMultiPolygon {
                        overlays.append(contentsOf: multiPolygon.polygons)
                    }
                }
            }

            mapView.addOverlays(overlays, level: .aboveLabels)
        } catch {
            logger.error(error: error, message: "Failed to load geojson.")
        }
    }

    private func setCenterInternal(
        _ center: CLLocationCoordinate2D?,
        animated: Bool,
        completion: (() -> Void)?
    ) {
        let region = makeRegion(center: center)

        self.center = center

        // Map view does not call delegate methods when attempting to set the same region.
        mapView.setRegion(region, animated: animated)

        if willChangeRegion {
            regionDidChangeCompletion = completion
        } else {
            completion?()
        }
    }

    private func recomputeVisibleRegion(animated: Bool) {
        enqueueAnimation(cancelOtherAnimations: false) { finish in
            self.setCenterInternal(self.center, animated: animated, completion: finish)
        }
    }

    private func enqueueAnimation(
        cancelOtherAnimations: Bool,
        block: @escaping (_ finish: @escaping () -> Void) -> Void
    ) {
        nonisolated(unsafe) let nonisolatedBlock = block
        let operation = AsyncBlockOperation(dispatchQueue: .main) { finish in
            nonisolatedBlock {
                finish(nil)
            }
        }

        if cancelOtherAnimations {
            animationQueue.cancelAllOperations()
        }

        animationQueue.addOperation(operation)
    }

    private func makeRegion(center: CLLocationCoordinate2D?) -> MKCoordinateRegion {
        guard let center else {
            return makeZoomedOutRegion()
        }

        let sourceRegion = makeZoomedInRegion(center: center)

        guard let alignmentView else {
            return sourceRegion
        }

        return makeRegion(from: sourceRegion, withCenterMatching: alignmentView)
    }

    private func makeZoomedInRegion(center: CLLocationCoordinate2D) -> MKCoordinateRegion {
        let span = MKCoordinateSpan(latitudeDelta: 30, longitudeDelta: 30)
        let region = MKCoordinateRegion(center: center, span: span)

        return mapView.regionThatFits(region)
    }

    private func makeZoomedOutRegion() -> MKCoordinateRegion {
        let coordinate = CLLocationCoordinate2D(latitude: 0, longitude: 0)
        let span = MKCoordinateSpan(latitudeDelta: 90, longitudeDelta: 90)
        let region = MKCoordinateRegion(center: coordinate, span: span)

        return mapView.regionThatFits(region)
    }

    private func makeRegion(
        from region: MKCoordinateRegion,
        withCenterMatching alignmentView: UIView
    ) -> MKCoordinateRegion {
        // Map view center lies within layout margins frame.
        let mapViewLayoutFrame = mapView.layoutMarginsGuide.layoutFrame

        guard mapViewLayoutFrame.width > 0, mapView.frame.width > 0,
            region.span.longitudeDelta > 0,
            mapView.region.span.longitudeDelta > 0
        else { return region }

        // MKMapView.convert(_:toRectTo:) returns CGRect scaled to the zoom level derived from
        // currently set region.
        // Calculate the ratio that we can use to translate the rect within its own coordinate
        // system before converting it into MKCoordinateRegion.
        let newZoomLevel = mapViewLayoutFrame.width / region.span.longitudeDelta
        let currentZoomLevel = mapViewLayoutFrame.width / mapView.region.span.longitudeDelta
        let zoomDelta = currentZoomLevel / newZoomLevel

        let alignmentViewRect = alignmentView.convert(alignmentView.bounds, to: mapView)
        let horizontalOffset = (mapViewLayoutFrame.midX - alignmentViewRect.midX) * zoomDelta
        let verticalOffset = (mapViewLayoutFrame.midY - alignmentViewRect.midY) * zoomDelta

        let regionRect = mapView.convert(region, toRectTo: mapView)
        let offsetRegionRect = regionRect.offsetBy(dx: horizontalOffset, dy: verticalOffset)
        let offsetRegion = mapView.convert(offsetRegionRect, toRegionFrom: mapView)

        if CLLocationCoordinate2DIsValid(offsetRegion.center) {
            return offsetRegion
        } else {
            return region
        }
    }
}