Merge pull request #6161 from vector-im/steve/5722_device_location

Location sharing: Support sending live device location
This commit is contained in:
SBiOSoftWhare
2022-05-19 14:56:46 +02:00
committed by GitHub
23 changed files with 898 additions and 10 deletions
@@ -77,9 +77,12 @@ final class LocationSharingCoordinator: Coordinator, Presentable {
init(parameters: LocationSharingCoordinatorParameters) {
self.parameters = parameters
let locationSharingService = LocationSharingService(userLocationService: parameters.roomDataSource.mxSession.userLocationService)
let viewModel = LocationSharingViewModel(mapStyleURL: BuildSettings.tileServerMapStyleURL,
avatarData: parameters.avatarData,
isLiveLocationSharingEnabled: BuildSettings.liveLocationSharingEnabled)
isLiveLocationSharingEnabled: BuildSettings.liveLocationSharingEnabled, service: locationSharingService)
let view = LocationSharingView(context: viewModel.context)
.addDependency(AvatarService.instantiate(mediaManager: parameters.mediaManager))
@@ -28,10 +28,12 @@ enum MockLocationSharingScreenState: MockScreenState, CaseIterable {
var screenView: ([Any], AnyView) {
let locationSharingService = MockLocationSharingService()
let mapStyleURL = URL(string: "https://api.maptiler.com/maps/streets/style.json?key=fU3vlMsMn4Jb6dnEIFsx")!
let viewModel = LocationSharingViewModel(mapStyleURL: mapStyleURL,
avatarData: AvatarInput(mxContentUri: "", matrixItemId: "alice:matrix.org", displayName: "Alice"),
isLiveLocationSharingEnabled: true)
isLiveLocationSharingEnabled: true, service: locationSharingService)
return ([viewModel],
AnyView(LocationSharingView(context: viewModel.context)
.addDependency(MockAvatarService.example)))
@@ -29,13 +29,18 @@ class LocationSharingViewModel: LocationSharingViewModelType, LocationSharingVie
// MARK: Private
private let locationSharingService: LocationSharingServiceProtocol
// MARK: Public
var completion: ((LocationSharingViewModelResult) -> Void)?
// MARK: - Setup
init(mapStyleURL: URL, avatarData: AvatarInputProtocol, isLiveLocationSharingEnabled: Bool = false) {
init(mapStyleURL: URL, avatarData: AvatarInputProtocol, isLiveLocationSharingEnabled: Bool = false, service: LocationSharingServiceProtocol) {
self.locationSharingService = service
let viewState = LocationSharingViewState(mapStyleURL: mapStyleURL,
userAvatarData: avatarData,
annotations: [],
@@ -75,7 +80,7 @@ class LocationSharingViewModel: LocationSharingViewModelType, LocationSharingVie
case .goToUserLocation:
state.bindings.pinLocation = nil
case .startLiveSharing:
state.bindings.showingTimerSelector = true
self.startLiveLocationSharing()
case .shareLiveLocation(let timeout):
state.bindings.showingTimerSelector = false
completion?(.shareLiveLocation(timeout: timeout.rawValue))
@@ -124,12 +129,38 @@ class LocationSharingViewModel: LocationSharingViewModelType, LocationSharingVie
title: VectorL10n.locationSharingInvalidAuthorizationErrorTitle(AppInfo.current.displayName),
primaryButton: (VectorL10n.locationSharingInvalidAuthorizationNotNow, primaryButtonCompletion),
secondaryButton: (VectorL10n.locationSharingInvalidAuthorizationSettings, {
if let applicationSettingsURL = URL(string:UIApplication.openSettingsURLString) {
UIApplication.shared.open(applicationSettingsURL)
}
UIApplication.shared.vc_openSettings()
}))
default:
break
}
}
private func startLiveLocationSharing() {
self.locationSharingService.requestAuthorization { [weak self] authorizationStatus in
guard let self = self else {
return
}
switch authorizationStatus {
case .unknown, .denied:
// Show error alert
self.state.bindings.alertInfo = AlertInfo(id: .userLocatingError,
title: VectorL10n.locationSharingLocatingUserErrorTitle(AppInfo.current.displayName),
primaryButton: (VectorL10n.ok, { UIApplication.shared.vc_openSettings()
}))
case .authorizedInForeground:
// When user only authorized location in foreground, advize to use background location
self.state.bindings.alertInfo = AlertInfo(id: .userLocatingError,
title: VectorL10n.locationSharingAllowBackgroundLocationTitle,
message: VectorL10n.locationSharingAllowBackgroundLocationMessage,
primaryButton: (VectorL10n.locationSharingAllowBackgroundLocationCancelAction, { [weak self] in self?.state.bindings.showingTimerSelector = true }),
secondaryButton: (VectorL10n.locationSharingAllowBackgroundLocationValidateAction, { UIApplication.shared.vc_openSettings() }))
case .authorizedAlways:
self.state.bindings.showingTimerSelector = true
}
}
}
}
@@ -0,0 +1,33 @@
//
// Copyright 2022 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 Foundation
/// Location authorization status
enum LocationAuthorizationStatus {
/// Location status unknown
case unknown
/// Location access is denied
case denied
/// Location only authorized in foreground
case authorizedInForeground
/// Location only authorized in foreground and background
case authorizedAlways
}
@@ -0,0 +1,28 @@
//
// 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 Foundation
import Combine
import CoreLocation
/// Location authorization request handler
typealias LocationAuthorizationHandler = (_ authorizationStatus: LocationAuthorizationStatus) -> Void
protocol LocationSharingServiceProtocol {
/// Request location authorization
func requestAuthorization(_ handler: @escaping LocationAuthorizationHandler)
}
@@ -0,0 +1,47 @@
//
// 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 Foundation
import CoreLocation
import MatrixSDK
class LocationSharingService: LocationSharingServiceProtocol {
// MARK: - Properties
// MARK: Private
private let userLocationService: UserLocationServiceProtocol?
// MARK: Public
// MARK: - Setup
init(userLocationService: UserLocationServiceProtocol?) {
self.userLocationService = userLocationService
}
// MARK: - Public
func requestAuthorization(_ handler: @escaping LocationAuthorizationHandler) {
guard let userLocationService = self.userLocationService else {
handler(LocationAuthorizationStatus.unknown)
return
}
userLocationService.requestAuthorization(handler)
}
}
@@ -0,0 +1,26 @@
//
// 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 Foundation
import Combine
import CoreLocation
@available(iOS 14.0, *)
class MockLocationSharingService: LocationSharingServiceProtocol {
func requestAuthorization(_ handler: @escaping LocationAuthorizationHandler) {
handler(.authorizedAlways)
}
}
@@ -49,7 +49,7 @@ class LocationSharingViewModelTests: XCTestCase {
XCTFail()
case .cancel:
expectation.fulfill()
case .shareLiveLocation(timeout: let timeout):
case .shareLiveLocation:
XCTFail()
}
}
@@ -94,7 +94,10 @@ class LocationSharingViewModelTests: XCTestCase {
}
private func buildViewModel() -> LocationSharingViewModel {
LocationSharingViewModel(mapStyleURL: URL(string: "http://empty.com")!,
avatarData: AvatarInput(mxContentUri: "", matrixItemId: "", displayName: ""))
let service = MockLocationSharingService()
return LocationSharingViewModel(mapStyleURL: URL(string: "http://empty.com")!,
avatarData: AvatarInput(mxContentUri: "", matrixItemId: "", displayName: ""), service: service)
}
}