2021-12-16 12:02:42 +00:00
|
|
|
//
|
|
|
|
// Copyright 2021 New Vector Ltd
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
//
|
|
|
|
|
|
|
|
import Combine
|
2021-12-21 12:42:01 +00:00
|
|
|
import CoreLocation
|
2022-09-27 07:17:22 +00:00
|
|
|
import SwiftUI
|
2021-12-16 12:02:42 +00:00
|
|
|
|
2022-01-28 10:58:31 +00:00
|
|
|
typealias LocationSharingViewModelType = StateStoreViewModel<LocationSharingViewState,
|
2022-09-27 07:17:22 +00:00
|
|
|
Never,
|
|
|
|
LocationSharingViewAction>
|
2022-01-28 10:58:31 +00:00
|
|
|
class LocationSharingViewModel: LocationSharingViewModelType, LocationSharingViewModelProtocol {
|
2021-12-16 12:02:42 +00:00
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
2022-05-16 15:04:05 +00:00
|
|
|
private let locationSharingService: LocationSharingServiceProtocol
|
|
|
|
|
2021-12-16 12:02:42 +00:00
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
var completion: ((LocationSharingViewModelResult) -> Void)?
|
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
2022-05-16 15:04:05 +00:00
|
|
|
init(mapStyleURL: URL, avatarData: AvatarInputProtocol, isLiveLocationSharingEnabled: Bool = false, service: LocationSharingServiceProtocol) {
|
2022-09-27 07:17:22 +00:00
|
|
|
locationSharingService = service
|
2022-05-16 15:04:05 +00:00
|
|
|
|
2022-03-16 10:33:36 +00:00
|
|
|
let viewState = LocationSharingViewState(mapStyleURL: mapStyleURL,
|
|
|
|
userAvatarData: avatarData,
|
2022-04-11 08:55:23 +00:00
|
|
|
annotations: [],
|
|
|
|
highlightedAnnotation: nil,
|
|
|
|
showsUserLocation: true,
|
2022-03-23 14:48:19 +00:00
|
|
|
isLiveLocationSharingEnabled: isLiveLocationSharingEnabled)
|
2022-03-16 10:33:36 +00:00
|
|
|
|
2021-12-20 15:33:59 +00:00
|
|
|
super.init(initialViewState: viewState)
|
2021-12-16 12:02:42 +00:00
|
|
|
|
|
|
|
state.errorSubject.sink { [weak self] error in
|
|
|
|
guard let self = self else { return }
|
2022-01-28 10:58:31 +00:00
|
|
|
self.processError(error)
|
2021-12-16 12:02:42 +00:00
|
|
|
}.store(in: &cancellables)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
override func process(viewAction: LocationSharingViewAction) {
|
|
|
|
switch viewAction {
|
|
|
|
case .cancel:
|
|
|
|
completion?(.cancel)
|
|
|
|
case .share:
|
2022-03-16 10:33:36 +00:00
|
|
|
// Share current user location
|
2021-12-16 12:02:42 +00:00
|
|
|
guard let location = state.bindings.userLocation else {
|
2022-01-28 10:58:31 +00:00
|
|
|
processError(.failedLocatingUser)
|
2021-12-16 12:02:42 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-31 14:52:16 +00:00
|
|
|
completion?(.share(latitude: location.latitude, longitude: location.longitude, coordinateType: .user))
|
2022-03-30 09:36:31 +00:00
|
|
|
case .sharePinLocation:
|
|
|
|
guard let pinLocation = state.bindings.pinLocation else {
|
|
|
|
processError(.failedLocatingUser)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-31 14:52:16 +00:00
|
|
|
completion?(.share(latitude: pinLocation.latitude, longitude: pinLocation.longitude, coordinateType: .pin))
|
2022-03-30 09:36:31 +00:00
|
|
|
case .goToUserLocation:
|
2022-06-02 09:34:01 +00:00
|
|
|
state.showsUserLocation = true
|
|
|
|
state.isPinDropSharing = false
|
2022-05-04 09:23:43 +00:00
|
|
|
case .startLiveSharing:
|
2022-09-27 07:17:22 +00:00
|
|
|
startLiveLocationSharing()
|
2022-05-03 13:21:51 +00:00
|
|
|
case .shareLiveLocation(let timeout):
|
2022-05-04 09:23:43 +00:00
|
|
|
state.bindings.showingTimerSelector = false
|
|
|
|
completion?(.shareLiveLocation(timeout: timeout.rawValue))
|
2022-06-02 09:34:01 +00:00
|
|
|
case .userDidPan:
|
|
|
|
state.showsUserLocation = false
|
|
|
|
state.isPinDropSharing = true
|
2022-07-11 12:43:40 +00:00
|
|
|
case .mapCreditsDidTap:
|
|
|
|
state.bindings.showMapCreditsSheet.toggle()
|
2021-12-16 12:02:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-28 10:58:31 +00:00
|
|
|
// MARK: - LocationSharingViewModelProtocol
|
|
|
|
|
|
|
|
public func startLoading() {
|
|
|
|
state.showLoadingIndicator = true
|
|
|
|
}
|
|
|
|
|
2022-03-03 16:30:00 +00:00
|
|
|
func stopLoading(error: LocationSharingAlertType?) {
|
2022-01-28 10:58:31 +00:00
|
|
|
state.showLoadingIndicator = false
|
|
|
|
|
|
|
|
if let error = error {
|
2022-09-27 07:17:22 +00:00
|
|
|
let alertInfo: AlertInfo<LocationSharingAlertType>
|
2022-07-26 08:56:37 +00:00
|
|
|
|
|
|
|
switch error {
|
|
|
|
case .locationSharingPowerLevelError:
|
|
|
|
alertInfo = AlertInfo(id: error,
|
|
|
|
title: VectorL10n.locationSharingInvalidPowerLevelTitle,
|
|
|
|
message: VectorL10n.locationSharingInvalidPowerLevelMessage,
|
|
|
|
primaryButton: (VectorL10n.ok, nil))
|
|
|
|
default:
|
|
|
|
alertInfo = AlertInfo(id: error,
|
|
|
|
title: VectorL10n.locationSharingPostFailureTitle,
|
|
|
|
message: VectorL10n.locationSharingPostFailureSubtitle(AppInfo.current.displayName),
|
|
|
|
primaryButton: (VectorL10n.ok, nil))
|
|
|
|
}
|
|
|
|
|
|
|
|
state.bindings.alertInfo = alertInfo
|
2022-01-28 10:58:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
|
|
|
private func processError(_ error: LocationSharingViewError) {
|
|
|
|
guard state.bindings.alertInfo == nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-27 07:17:22 +00:00
|
|
|
let primaryButtonCompletion: (() -> Void)? = { [weak self] () in
|
2022-01-28 10:58:31 +00:00
|
|
|
self?.completion?(.cancel)
|
|
|
|
}
|
|
|
|
|
|
|
|
switch error {
|
|
|
|
case .failedLoadingMap:
|
2022-03-07 12:22:34 +00:00
|
|
|
state.bindings.alertInfo = AlertInfo(id: .mapLoadingError,
|
|
|
|
title: VectorL10n.locationSharingLoadingMapErrorTitle(AppInfo.current.displayName),
|
|
|
|
primaryButton: (VectorL10n.ok, primaryButtonCompletion))
|
2022-08-01 10:46:15 +00:00
|
|
|
|
|
|
|
state.showMapLoadingError = true
|
|
|
|
|
2022-01-28 10:58:31 +00:00
|
|
|
case .failedLocatingUser:
|
2022-03-07 12:22:34 +00:00
|
|
|
state.bindings.alertInfo = AlertInfo(id: .userLocatingError,
|
|
|
|
title: VectorL10n.locationSharingLocatingUserErrorTitle(AppInfo.current.displayName),
|
|
|
|
primaryButton: (VectorL10n.ok, primaryButtonCompletion))
|
2022-01-28 10:58:31 +00:00
|
|
|
case .invalidLocationAuthorization:
|
2022-03-07 12:22:34 +00:00
|
|
|
state.bindings.alertInfo = AlertInfo(id: .authorizationError,
|
|
|
|
title: VectorL10n.locationSharingInvalidAuthorizationErrorTitle(AppInfo.current.displayName),
|
|
|
|
primaryButton: (VectorL10n.locationSharingInvalidAuthorizationNotNow, primaryButtonCompletion),
|
|
|
|
secondaryButton: (VectorL10n.locationSharingInvalidAuthorizationSettings, {
|
2022-09-27 07:17:22 +00:00
|
|
|
UIApplication.shared.vc_openSettings()
|
|
|
|
}))
|
2022-01-28 10:58:31 +00:00
|
|
|
default:
|
|
|
|
break
|
2021-12-16 12:02:42 +00:00
|
|
|
}
|
|
|
|
}
|
2022-05-16 15:04:05 +00:00
|
|
|
|
2022-07-04 15:39:45 +00:00
|
|
|
private func checkLocationAuthorizationAndPresentTimerSelector() {
|
2022-09-27 07:17:22 +00:00
|
|
|
locationSharingService.requestAuthorization { [weak self] authorizationStatus in
|
2022-05-16 15:04:05 +00:00
|
|
|
|
|
|
|
guard let self = self else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
switch authorizationStatus {
|
|
|
|
case .unknown, .denied:
|
|
|
|
// Show error alert
|
|
|
|
self.state.bindings.alertInfo = AlertInfo(id: .userLocatingError,
|
2022-09-27 07:17:22 +00:00
|
|
|
title: VectorL10n.locationSharingLocatingUserErrorTitle(AppInfo.current.displayName),
|
2022-05-16 15:04:05 +00:00
|
|
|
primaryButton: (VectorL10n.ok, { UIApplication.shared.vc_openSettings()
|
2022-09-27 07:17:22 +00:00
|
|
|
}))
|
2022-05-16 15:04:05 +00:00
|
|
|
case .authorizedInForeground:
|
|
|
|
// When user only authorized location in foreground, advize to use background location
|
|
|
|
self.state.bindings.alertInfo = AlertInfo(id: .userLocatingError,
|
|
|
|
title: VectorL10n.locationSharingAllowBackgroundLocationTitle,
|
2022-09-27 07:17:22 +00:00
|
|
|
message: VectorL10n.locationSharingAllowBackgroundLocationMessage,
|
|
|
|
primaryButton: (VectorL10n.locationSharingAllowBackgroundLocationCancelAction, { }),
|
2022-05-16 15:04:05 +00:00
|
|
|
secondaryButton: (VectorL10n.locationSharingAllowBackgroundLocationValidateAction, { UIApplication.shared.vc_openSettings() }))
|
|
|
|
case .authorizedAlways:
|
|
|
|
self.state.bindings.showingTimerSelector = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-07-04 15:39:45 +00:00
|
|
|
|
|
|
|
private func startLiveLocationSharing() {
|
|
|
|
guard let completion = completion else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-27 07:17:22 +00:00
|
|
|
completion(.checkLiveLocationCanBeStarted { result in
|
2022-07-26 08:56:37 +00:00
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success:
|
2022-07-04 15:39:45 +00:00
|
|
|
self.checkLocationAuthorizationAndPresentTimerSelector()
|
2022-07-26 08:56:37 +00:00
|
|
|
case .failure(let error):
|
|
|
|
if case LiveLocationStartError.powerLevelNotHighEnough = error {
|
|
|
|
self.stopLoading(error: .locationSharingPowerLevelError)
|
|
|
|
}
|
2022-07-04 15:39:45 +00:00
|
|
|
}
|
2022-09-27 07:17:22 +00:00
|
|
|
})
|
2022-07-04 15:39:45 +00:00
|
|
|
}
|
2021-12-16 12:02:42 +00:00
|
|
|
}
|