-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #195 from lennet/MapViewControllerRefactoring
MapViewController Refactoring
- Loading branch information
Showing
8 changed files
with
184 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// | ||
// AnnotationController.swift | ||
// CriticalMaps | ||
// | ||
// Created by Leonard Thomas on 15.11.19. | ||
// Copyright © 2019 Pokus Labs. All rights reserved. | ||
// | ||
|
||
import MapKit | ||
|
||
class AnnotationController<T: IdentifiableAnnnotation, K: MKAnnotationView> { | ||
var mapView: MKMapView | ||
let annotationType = T.self | ||
let annotationViewType = K.self | ||
|
||
required init(mapView: MKMapView) { | ||
self.mapView = mapView | ||
setup() | ||
} | ||
|
||
open func setup() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// BikeAnnotationController.swift | ||
// CriticalMaps | ||
// | ||
// Created by Leonard Thomas on 14.11.19. | ||
// Copyright © 2019 Pokus Labs. All rights reserved. | ||
// | ||
|
||
import MapKit | ||
|
||
class BikeAnnotation: IdentifiableAnnnotation {} | ||
|
||
class BikeAnnotationController: AnnotationController<BikeAnnotation, BikeAnnoationView> { | ||
public override func setup() { | ||
NotificationCenter.default.addObserver(self, selector: #selector(positionsDidChange(notification:)), name: Notification.positionOthersChanged, object: nil) | ||
} | ||
|
||
@objc private func positionsDidChange(notification: Notification) { | ||
guard let response = notification.object as? ApiResponse else { return } | ||
display(locations: response.locations) | ||
} | ||
|
||
private func display(locations: [String: Location]) { | ||
guard LocationManager.accessPermission == .authorized else { | ||
Logger.log(.info, log: .map, "Bike annotations cannot be displayed because no GPS Access permission granted", parameter: LocationManager.accessPermission.rawValue) | ||
return | ||
} | ||
var unmatchedLocations = locations | ||
var unmatchedAnnotations: [MKAnnotation] = [] | ||
// update existing annotations | ||
mapView.annotations.compactMap { $0 as? BikeAnnotation }.forEach { annotation in | ||
if let location = unmatchedLocations[annotation.identifier] { | ||
annotation.location = location | ||
unmatchedLocations.removeValue(forKey: annotation.identifier) | ||
} else { | ||
unmatchedAnnotations.append(annotation) | ||
} | ||
} | ||
let annotations = unmatchedLocations.map { BikeAnnotation(location: $0.value, identifier: $0.key) } | ||
mapView.addAnnotations(annotations) | ||
|
||
// remove annotations that no longer exist | ||
mapView.removeAnnotations(unmatchedAnnotations) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// | ||
// MKMapView+Register.swift | ||
// CriticalMaps | ||
// | ||
// Created by Leonard Thomas on 14.11.19. | ||
// Copyright © 2019 Pokus Labs. All rights reserved. | ||
// | ||
|
||
import MapKit | ||
|
||
extension MKAnnotationView { | ||
fileprivate class var reuseIdentifier: String { | ||
return String(describing: self) | ||
} | ||
} | ||
|
||
extension MKMapView { | ||
func register<T: MKAnnotationView>(annotationViewType: T.Type) { | ||
if #available(iOS 11.0, *) { | ||
register(annotationViewType, forAnnotationViewWithReuseIdentifier: annotationViewType.reuseIdentifier) | ||
} | ||
} | ||
|
||
func dequeueReusableAnnotationView<T: MKAnnotationView>(ofType annotationType: T.Type, for indexPath: IndexPath? = nil, with annotation: MKAnnotation) -> T { | ||
let annotationView: T | ||
if #available(iOS 11.0, *) { | ||
annotationView = dequeueReusableAnnotationView(withIdentifier: annotationType.reuseIdentifier, for: annotation) as! T | ||
} else { | ||
annotationView = dequeueReusableAnnotationView(withIdentifier: annotationType.reuseIdentifier) as? T ?? T() | ||
annotationView.annotation = annotation | ||
} | ||
return annotationView | ||
} | ||
} |
Oops, something went wrong.