2022-04-05 16:36:41 +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
|
|
|
|
import Mapbox
|
2022-09-27 07:17:22 +00:00
|
|
|
import SwiftUI
|
2022-04-05 16:36:41 +00:00
|
|
|
|
|
|
|
typealias LiveLocationSharingViewerViewModelType = StateStoreViewModel<LiveLocationSharingViewerViewState,
|
2022-09-27 07:17:22 +00:00
|
|
|
Never,
|
|
|
|
LiveLocationSharingViewerViewAction>
|
2022-04-05 16:36:41 +00:00
|
|
|
class LiveLocationSharingViewerViewModel: LiveLocationSharingViewerViewModelType, LiveLocationSharingViewerViewModelProtocol {
|
|
|
|
// MARK: - Properties
|
|
|
|
|
|
|
|
// MARK: Private
|
|
|
|
|
2022-05-03 13:57:36 +00:00
|
|
|
private var liveLocationSharingViewerService: LiveLocationSharingViewerServiceProtocol
|
2022-04-05 16:36:41 +00:00
|
|
|
|
|
|
|
private var mapViewErrorAlertInfoBuilder: MapViewErrorAlertInfoBuilder
|
2022-05-03 13:57:36 +00:00
|
|
|
|
|
|
|
private var screenUpdateTimer: Timer?
|
2022-07-28 07:26:40 +00:00
|
|
|
|
|
|
|
// Last annotation that could be highlighted
|
|
|
|
// Used to set map position when location sharing is ended
|
|
|
|
private var lastHighlightableAnnotation: LocationAnnotation?
|
2022-04-05 16:36:41 +00:00
|
|
|
|
|
|
|
// MARK: Public
|
|
|
|
|
|
|
|
var completion: ((LiveLocationSharingViewerViewModelResult) -> Void)?
|
|
|
|
|
|
|
|
// MARK: - Setup
|
|
|
|
|
|
|
|
init(mapStyleURL: URL, service: LiveLocationSharingViewerServiceProtocol) {
|
|
|
|
let viewState = LiveLocationSharingViewerViewState(mapStyleURL: mapStyleURL, annotations: [], highlightedAnnotation: nil, listItemsViewData: [])
|
|
|
|
|
|
|
|
liveLocationSharingViewerService = service
|
|
|
|
mapViewErrorAlertInfoBuilder = MapViewErrorAlertInfoBuilder()
|
|
|
|
|
|
|
|
super.init(initialViewState: viewState)
|
|
|
|
|
|
|
|
state.errorSubject.sink { [weak self] error in
|
|
|
|
guard let self = self else { return }
|
|
|
|
self.processError(error)
|
|
|
|
}.store(in: &cancellables)
|
|
|
|
|
2022-09-27 07:17:22 +00:00
|
|
|
setupLocationSharingService()
|
|
|
|
setupScreenUpdateTimer()
|
2022-04-05 16:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Public
|
|
|
|
|
|
|
|
override func process(viewAction: LiveLocationSharingViewerViewAction) {
|
|
|
|
switch viewAction {
|
|
|
|
case .done:
|
|
|
|
completion?(.done)
|
|
|
|
case .stopSharing:
|
2022-05-03 13:57:36 +00:00
|
|
|
stopUserLocationSharing()
|
2022-04-05 16:36:41 +00:00
|
|
|
case .tapListItem(let userId):
|
2022-09-27 07:17:22 +00:00
|
|
|
highlighAnnotation(with: userId)
|
2022-04-05 16:36:41 +00:00
|
|
|
case .share(let userLocationAnnotation):
|
|
|
|
completion?(.share(userLocationAnnotation.coordinate))
|
2022-07-11 12:44:30 +00:00
|
|
|
case .mapCreditsDidTap:
|
|
|
|
state.bindings.showMapCreditsSheet.toggle()
|
2022-04-05 16:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Private
|
|
|
|
|
2022-05-03 13:57:36 +00:00
|
|
|
private func setupLocationSharingService() {
|
2022-09-27 07:17:22 +00:00
|
|
|
updateUsersLiveLocation(highlightFirstLocation: true)
|
2022-05-03 13:57:36 +00:00
|
|
|
|
|
|
|
liveLocationSharingViewerService.didUpdateUsersLiveLocation = { [weak self] liveLocations in
|
|
|
|
self?.update(with: liveLocations, highlightFirstLocation: false)
|
|
|
|
}
|
2022-09-27 07:17:22 +00:00
|
|
|
liveLocationSharingViewerService.startListeningLiveLocationUpdates()
|
2022-05-03 13:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func updateUsersLiveLocation(highlightFirstLocation: Bool) {
|
2022-09-27 07:17:22 +00:00
|
|
|
update(with: liveLocationSharingViewerService.usersLiveLocation, highlightFirstLocation: highlightFirstLocation)
|
2022-05-03 13:57:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func setupScreenUpdateTimer() {
|
2022-09-27 07:17:22 +00:00
|
|
|
screenUpdateTimer = Timer.scheduledTimer(withTimeInterval: 30, repeats: true) { [weak self] _ in
|
2022-05-03 13:57:36 +00:00
|
|
|
|
|
|
|
self?.updateUsersLiveLocation(highlightFirstLocation: false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-05 16:36:41 +00:00
|
|
|
private func processError(_ error: LocationSharingViewError) {
|
|
|
|
guard state.bindings.alertInfo == nil else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-01 10:45:59 +00:00
|
|
|
if case .failedLoadingMap = error {
|
|
|
|
state.showMapLoadingError = true
|
|
|
|
}
|
|
|
|
|
2022-04-05 16:36:41 +00:00
|
|
|
let alertInfo = mapViewErrorAlertInfoBuilder.build(with: error) { [weak self] in
|
|
|
|
|
|
|
|
switch error {
|
|
|
|
case .invalidLocationAuthorization:
|
2022-09-27 07:17:22 +00:00
|
|
|
if let applicationSettingsURL = URL(string: UIApplication.openSettingsURLString) {
|
2022-04-05 16:36:41 +00:00
|
|
|
UIApplication.shared.open(applicationSettingsURL)
|
|
|
|
} else {
|
|
|
|
self?.completion?(.done)
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
self?.completion?(.done)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
state.bindings.alertInfo = alertInfo
|
|
|
|
}
|
|
|
|
|
|
|
|
private func userLocationAnnotations(from usersLiveLocation: [UserLiveLocation]) -> [UserLocationAnnotation] {
|
2022-09-27 07:17:22 +00:00
|
|
|
usersLiveLocation.map { userLiveLocation in
|
|
|
|
UserLocationAnnotation(avatarData: userLiveLocation.avatarData, coordinate: userLiveLocation.coordinate)
|
2022-04-05 16:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func currentUserLocationAnnotation(from annotations: [UserLocationAnnotation]) -> UserLocationAnnotation? {
|
|
|
|
annotations.first { annotation in
|
2022-09-27 07:17:22 +00:00
|
|
|
liveLocationSharingViewerService.isCurrentUserId(annotation.userId)
|
2022-04-05 16:36:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func getHighlightedAnnotation(from annotations: [UserLocationAnnotation]) -> UserLocationAnnotation? {
|
2022-09-27 07:17:22 +00:00
|
|
|
if let userAnnotation = currentUserLocationAnnotation(from: annotations) {
|
2022-04-05 16:36:41 +00:00
|
|
|
return userAnnotation
|
|
|
|
} else {
|
|
|
|
return annotations.first
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private func listItemsViewData(from usersLiveLocation: [UserLiveLocation]) -> [LiveLocationListItemViewData] {
|
|
|
|
var listItemsViewData: [LiveLocationListItemViewData] = []
|
|
|
|
|
|
|
|
let sortedUsersLiveLocation = usersLiveLocation.sorted { userLiveLocation1, userLiveLocation2 in
|
2022-09-27 07:17:22 +00:00
|
|
|
userLiveLocation1.displayName > userLiveLocation2.displayName
|
2022-04-05 16:36:41 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 07:17:22 +00:00
|
|
|
listItemsViewData = sortedUsersLiveLocation.map { userLiveLocation in
|
|
|
|
self.listItemViewData(from: userLiveLocation)
|
|
|
|
}
|
2022-04-05 16:36:41 +00:00
|
|
|
|
|
|
|
let currentUserIndex = listItemsViewData.firstIndex { viewData in
|
2022-09-27 07:17:22 +00:00
|
|
|
viewData.isCurrentUser
|
2022-04-05 16:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Move current user as first item
|
|
|
|
if let currentUserIndex = currentUserIndex {
|
|
|
|
let currentUserViewData = listItemsViewData[currentUserIndex]
|
|
|
|
listItemsViewData.remove(at: currentUserIndex)
|
|
|
|
listItemsViewData.insert(currentUserViewData, at: 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
return listItemsViewData
|
|
|
|
}
|
|
|
|
|
|
|
|
private func listItemViewData(from userLiveLocation: UserLiveLocation) -> LiveLocationListItemViewData {
|
2022-09-27 07:17:22 +00:00
|
|
|
let isCurrentUser = liveLocationSharingViewerService.isCurrentUserId(userLiveLocation.userId)
|
2022-04-05 16:36:41 +00:00
|
|
|
|
2022-09-27 07:17:22 +00:00
|
|
|
let expirationDate = userLiveLocation.timestamp + userLiveLocation.timeout
|
2022-04-05 16:36:41 +00:00
|
|
|
|
|
|
|
return LiveLocationListItemViewData(userId: userLiveLocation.userId, isCurrentUser: isCurrentUser, avatarData: userLiveLocation.avatarData, displayName: userLiveLocation.displayName, expirationDate: expirationDate, lastUpdate: userLiveLocation.lastUpdate)
|
|
|
|
}
|
|
|
|
|
2022-05-03 13:57:36 +00:00
|
|
|
private func update(with usersLiveLocation: [UserLiveLocation], highlightFirstLocation: Bool) {
|
2022-09-27 07:17:22 +00:00
|
|
|
let annotations: [UserLocationAnnotation] = userLocationAnnotations(from: usersLiveLocation)
|
2022-04-05 16:36:41 +00:00
|
|
|
|
2022-07-28 07:26:40 +00:00
|
|
|
var highlightedAnnotation: LocationAnnotation?
|
2022-05-03 13:57:36 +00:00
|
|
|
|
|
|
|
if highlightFirstLocation {
|
2022-09-27 07:17:22 +00:00
|
|
|
highlightedAnnotation = getHighlightedAnnotation(from: annotations)
|
2022-05-03 13:57:36 +00:00
|
|
|
}
|
2022-04-05 16:36:41 +00:00
|
|
|
|
2022-09-27 07:17:22 +00:00
|
|
|
if let highlightableAnnotation = getHighlightedAnnotation(from: annotations) {
|
|
|
|
lastHighlightableAnnotation = highlightableAnnotation
|
2022-07-28 07:26:40 +00:00
|
|
|
}
|
|
|
|
|
2022-09-27 07:17:22 +00:00
|
|
|
if let lastHighlightableAnnotation = lastHighlightableAnnotation, usersLiveLocation.isEmpty {
|
2022-07-28 07:26:40 +00:00
|
|
|
highlightedAnnotation = InvisibleLocationAnnotation(coordinate: lastHighlightableAnnotation.coordinate)
|
|
|
|
}
|
|
|
|
|
2022-09-27 07:17:22 +00:00
|
|
|
let listViewItems = listItemsViewData(from: usersLiveLocation)
|
2022-04-05 16:36:41 +00:00
|
|
|
|
2022-09-27 07:17:22 +00:00
|
|
|
state.annotations = annotations
|
|
|
|
state.highlightedAnnotation = highlightedAnnotation
|
|
|
|
state.listItemsViewData = listViewItems
|
2022-04-05 16:36:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private func highlighAnnotation(with userId: String) {
|
2022-09-27 07:17:22 +00:00
|
|
|
let foundUserAnnotation = state.annotations.first { annotation in
|
2022-04-05 16:36:41 +00:00
|
|
|
annotation.userId == userId
|
|
|
|
}
|
|
|
|
|
|
|
|
guard let foundUserAnnotation = foundUserAnnotation else {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-27 07:17:22 +00:00
|
|
|
state.highlightedAnnotation = foundUserAnnotation
|
2022-04-05 16:36:41 +00:00
|
|
|
}
|
2022-05-03 13:57:36 +00:00
|
|
|
|
|
|
|
private func stopUserLocationSharing() {
|
2022-09-27 07:17:22 +00:00
|
|
|
state.showLoadingIndicator = true
|
2022-05-03 13:57:36 +00:00
|
|
|
|
2022-09-27 07:17:22 +00:00
|
|
|
liveLocationSharingViewerService.stopUserLiveLocationSharing { result in
|
2022-05-03 13:57:36 +00:00
|
|
|
self.state.showLoadingIndicator = false
|
|
|
|
|
|
|
|
switch result {
|
|
|
|
case .success:
|
|
|
|
break
|
2022-09-27 07:17:22 +00:00
|
|
|
case .failure:
|
2022-05-03 13:57:36 +00:00
|
|
|
self.state.bindings.alertInfo = AlertInfo(id: .stopLocationSharingError,
|
|
|
|
title: VectorL10n.error,
|
|
|
|
message: VectorL10n.locationSharingLiveStopSharingError,
|
|
|
|
primaryButton: (VectorL10n.ok, nil))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-05 16:36:41 +00:00
|
|
|
}
|