5858: Factorise some code according to PR comments

This commit is contained in:
MaximeE
2022-04-04 16:33:55 +02:00
parent 540ca3e7f3
commit dae1109a59
7 changed files with 52 additions and 42 deletions
@@ -33,10 +33,10 @@ struct LocationSharingMapView: UIViewRepresentable {
let tileServerMapURL: URL
/// Map annotations
let annotations: [UserLocationAnnotation]
let annotations: [LocationAnnotation]
/// Map annotation to focus on
let highlightedAnnotation: UserLocationAnnotation?
let highlightedAnnotation: LocationAnnotation?
/// Current user avatar data, used to replace current location annotation view with the user avatar
let userAvatarData: AvatarInputProtocol?
@@ -117,10 +117,12 @@ extension LocationSharingMapView {
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
if let userLocationAnnotation = annotation as? UserLocationAnnotation {
return UserLocationAnnotatonView(userLocationAnnotation: userLocationAnnotation)
return LocationAnnotatonView(userLocationAnnotation: userLocationAnnotation)
} else if let pinLocationAnnotation = annotation as? LocationAnnotation {
return LocationAnnotatonView(pinLocationAnnotation: pinLocationAnnotation)
} else if annotation is MGLUserLocation && locationSharingMapView.mapCenterCoordinate == nil, let currentUserAvatarData = locationSharingMapView.userAvatarData {
// Replace default current location annotation view with a UserLocationAnnotatonView when the map is center on user location
return UserLocationAnnotatonView(avatarData: currentUserAvatarData)
return LocationAnnotatonView(avatarData: currentUserAvatarData)
}
return nil
@@ -19,7 +19,7 @@ import SwiftUI
import Mapbox
@available(iOS 14, *)
class UserLocationAnnotatonView: MGLUserLocationAnnotationView {
class LocationAnnotatonView: MGLUserLocationAnnotationView {
// MARK: Private
@@ -38,14 +38,15 @@ class UserLocationAnnotatonView: MGLUserLocationAnnotationView {
// TODO: Use a reuseIdentifier
super.init(annotation: userLocationAnnotation, reuseIdentifier: nil)
switch userLocationAnnotation.coordinateType {
case .user:
self.addUserMarkerView(with: userLocationAnnotation.avatarData)
case .pin, .generic:
self.addPinMarkerView()
@unknown default:
return
}
self.addUserMarkerView(with: userLocationAnnotation.avatarData)
}
init(pinLocationAnnotation: LocationAnnotation) {
// TODO: Use a reuseIdentifier
super.init(annotation: pinLocationAnnotation, reuseIdentifier: nil)
self.addPinMarkerView()
}
required init?(coder: NSCoder) {