mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-24 02:22:44 +02:00
5858: Factorise some code according to PR comments
This commit is contained in:
+2
-6
@@ -33,14 +33,12 @@ extension MXEventAssetType {
|
||||
func locationSharingCoordinateType() -> LocationSharingCoordinateType {
|
||||
let coordinateType: LocationSharingCoordinateType
|
||||
switch self {
|
||||
case .user:
|
||||
case .user, .generic:
|
||||
coordinateType = .user
|
||||
case .pin:
|
||||
coordinateType = .pin
|
||||
case .generic:
|
||||
coordinateType = .generic
|
||||
@unknown default:
|
||||
coordinateType = .generic
|
||||
coordinateType = .user
|
||||
}
|
||||
return coordinateType
|
||||
}
|
||||
@@ -54,8 +52,6 @@ extension LocationSharingCoordinateType {
|
||||
eventAssetType = .user
|
||||
case .pin:
|
||||
eventAssetType = .pin
|
||||
case .generic:
|
||||
eventAssetType = .generic
|
||||
}
|
||||
return eventAssetType
|
||||
}
|
||||
|
||||
+18
-11
@@ -17,26 +17,33 @@
|
||||
import Foundation
|
||||
import Mapbox
|
||||
|
||||
class UserLocationAnnotation: NSObject, MGLAnnotation {
|
||||
class LocationAnnotation: NSObject, MGLAnnotation {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
let coordinate: CLLocationCoordinate2D
|
||||
|
||||
// MARK: - Setup
|
||||
|
||||
init(coordinate: CLLocationCoordinate2D) {
|
||||
|
||||
self.coordinate = coordinate
|
||||
}
|
||||
}
|
||||
|
||||
class UserLocationAnnotation: LocationAnnotation {
|
||||
|
||||
// MARK: - Properties
|
||||
|
||||
let avatarData: AvatarInputProtocol
|
||||
|
||||
let coordinate: CLLocationCoordinate2D
|
||||
|
||||
let coordinateType: LocationSharingCoordinateType
|
||||
|
||||
// MARK: - Setup
|
||||
|
||||
init(avatarData: AvatarInputProtocol,
|
||||
coordinate: CLLocationCoordinate2D,
|
||||
coordinateType: LocationSharingCoordinateType) {
|
||||
|
||||
self.coordinate = coordinate
|
||||
coordinate: CLLocationCoordinate2D) {
|
||||
|
||||
self.avatarData = avatarData
|
||||
self.coordinateType = coordinateType
|
||||
|
||||
super.init()
|
||||
super.init(coordinate: coordinate)
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,6 @@ import CoreLocation
|
||||
enum LocationSharingCoordinateType {
|
||||
case user
|
||||
case pin
|
||||
case generic
|
||||
}
|
||||
|
||||
enum LocationSharingViewAction {
|
||||
@@ -55,13 +54,13 @@ struct LocationSharingViewState: BindableState {
|
||||
let userAvatarData: AvatarInputProtocol
|
||||
|
||||
/// Shared annotation to display existing location
|
||||
let sharedAnnotation: UserLocationAnnotation?
|
||||
let sharedAnnotation: LocationAnnotation?
|
||||
|
||||
/// Map annotations to display on map
|
||||
var annotations: [UserLocationAnnotation]
|
||||
var annotations: [LocationAnnotation]
|
||||
|
||||
/// Map annotation to focus on
|
||||
var highlightedAnnotation: UserLocationAnnotation?
|
||||
var highlightedAnnotation: LocationAnnotation?
|
||||
|
||||
/// Indicates whether the user has moved around the map to drop a pin somewhere other than their current location
|
||||
var isPinDropSharing: Bool {
|
||||
|
||||
@@ -37,14 +37,20 @@ class LocationSharingViewModel: LocationSharingViewModelType, LocationSharingVie
|
||||
|
||||
init(mapStyleURL: URL, avatarData: AvatarInputProtocol, location: CLLocationCoordinate2D? = nil, coordinateType: LocationSharingCoordinateType, isLiveLocationSharingEnabled: Bool = false) {
|
||||
|
||||
var sharedAnnotation: UserLocationAnnotation?
|
||||
var annotations: [UserLocationAnnotation] = []
|
||||
var highlightedAnnotation: UserLocationAnnotation?
|
||||
var sharedAnnotation: LocationAnnotation?
|
||||
var annotations: [LocationAnnotation] = []
|
||||
var highlightedAnnotation: LocationAnnotation?
|
||||
var showsUserLocation: Bool = false
|
||||
|
||||
// Displaying an existing location
|
||||
if let sharedCoordinate = location {
|
||||
let sharedLocationAnnotation = UserLocationAnnotation(avatarData: avatarData, coordinate: sharedCoordinate, coordinateType: coordinateType)
|
||||
let sharedLocationAnnotation: LocationAnnotation
|
||||
switch coordinateType {
|
||||
case .user:
|
||||
sharedLocationAnnotation = UserLocationAnnotation(avatarData: avatarData, coordinate: sharedCoordinate)
|
||||
case .pin:
|
||||
sharedLocationAnnotation = LocationAnnotation(coordinate: sharedCoordinate)
|
||||
}
|
||||
|
||||
annotations.append(sharedLocationAnnotation)
|
||||
highlightedAnnotation = sharedLocationAnnotation
|
||||
@@ -80,7 +86,7 @@ class LocationSharingViewModel: LocationSharingViewModelType, LocationSharingVie
|
||||
case .share:
|
||||
// Share existing location
|
||||
if let location = state.sharedAnnotation?.coordinate {
|
||||
completion?(.share(latitude: location.latitude, longitude: location.longitude, coordinateType: .generic))
|
||||
completion?(.share(latitude: location.latitude, longitude: location.longitude, coordinateType: .user))
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
+2
-3
@@ -78,10 +78,9 @@ class LocationSharingViewModelTests: XCTestCase {
|
||||
|
||||
viewModel.completion = { result in
|
||||
switch result {
|
||||
case .share(let latitude, let longitude, let coordinateType):
|
||||
case .share(let latitude, let longitude, _):
|
||||
XCTAssertEqual(latitude, viewModel.context.viewState.sharedAnnotation?.coordinate.latitude)
|
||||
XCTAssertEqual(longitude, viewModel.context.viewState.sharedAnnotation?.coordinate.longitude)
|
||||
XCTAssertEqual(coordinateType, viewModel.context.viewState.sharedAnnotation?.coordinateType)
|
||||
expectation.fulfill()
|
||||
case .cancel:
|
||||
XCTFail()
|
||||
@@ -124,6 +123,6 @@ class LocationSharingViewModelTests: XCTestCase {
|
||||
private func buildViewModel(withLocation: Bool) -> LocationSharingViewModel {
|
||||
LocationSharingViewModel(mapStyleURL: URL(string: "http://empty.com")!,
|
||||
avatarData: AvatarInput(mxContentUri: "", matrixItemId: "", displayName: ""),
|
||||
location: (withLocation ? CLLocationCoordinate2D(latitude: 51.4932641, longitude: -0.257096) : nil), coordinateType: .generic)
|
||||
location: (withLocation ? CLLocationCoordinate2D(latitude: 51.4932641, longitude: -0.257096) : nil), coordinateType: .user)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user