5982: Clean code and add tests

This commit is contained in:
MaximeE
2022-04-11 10:55:23 +02:00
parent 29bb3a4a68
commit a3758731b9
10 changed files with 93 additions and 134 deletions
@@ -35,38 +35,12 @@ class LocationSharingViewModel: LocationSharingViewModelType, LocationSharingVie
// MARK: - Setup
init(mapStyleURL: URL, avatarData: AvatarInputProtocol, location: CLLocationCoordinate2D? = nil, coordinateType: LocationSharingCoordinateType, isLiveLocationSharingEnabled: Bool = false) {
var sharedAnnotation: LocationAnnotation?
var annotations: [LocationAnnotation] = []
var highlightedAnnotation: LocationAnnotation?
var showsUserLocation: Bool = false
// Displaying an existing location
if let sharedCoordinate = location {
let sharedLocationAnnotation: LocationAnnotation
switch coordinateType {
case .user:
sharedLocationAnnotation = UserLocationAnnotation(avatarData: avatarData, coordinate: sharedCoordinate)
case .pin:
sharedLocationAnnotation = PinLocationAnnotation(coordinate: sharedCoordinate)
}
annotations.append(sharedLocationAnnotation)
highlightedAnnotation = sharedLocationAnnotation
sharedAnnotation = sharedLocationAnnotation
} else {
// Share current location
showsUserLocation = true
}
init(mapStyleURL: URL, avatarData: AvatarInputProtocol, isLiveLocationSharingEnabled: Bool = false) {
let viewState = LocationSharingViewState(mapStyleURL: mapStyleURL,
userAvatarData: avatarData,
sharedAnnotation: sharedAnnotation,
annotations: annotations,
highlightedAnnotation: highlightedAnnotation,
showsUserLocation: showsUserLocation,
annotations: [],
highlightedAnnotation: nil,
showsUserLocation: true,
isLiveLocationSharingEnabled: isLiveLocationSharingEnabled)
super.init(initialViewState: viewState)
@@ -84,12 +58,6 @@ class LocationSharingViewModel: LocationSharingViewModelType, LocationSharingVie
case .cancel:
completion?(.cancel)
case .share:
// Share existing location
if let location = state.sharedAnnotation?.coordinate {
completion?(.share(latitude: location.latitude, longitude: location.longitude, coordinateType: .user))
return
}
// Share current user location
guard let location = state.bindings.userLocation else {
processError(.failedLocatingUser)