vector-im/element-ios/issues/5298 - Implemented location sharing from the input toolbar action menu.

This commit is contained in:
Stefan Ceriu
2021-12-16 14:02:42 +02:00
committed by Stefan Ceriu
parent fb16d96ac1
commit 37db201124
30 changed files with 867 additions and 36 deletions
@@ -0,0 +1,101 @@
//
// 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 UIKit
import SwiftUI
struct LocationSharingCoordinatorParameters {
let navigationRouter: NavigationRouterType
let roomDataSource: MXKRoomDataSource
let mediaManager: MXMediaManager
let user: MXUser
}
final class LocationSharingCoordinator: Coordinator {
// MARK: - Properties
// MARK: Private
private let parameters: LocationSharingCoordinatorParameters
private let locationSharingHostingController: UIViewController
private var _locationSharingViewModel: Any? = nil
@available(iOS 14.0, *)
fileprivate var locationSharingViewModel: LocationSharingViewModel {
return _locationSharingViewModel as! LocationSharingViewModel
}
// MARK: Public
// Must be used only internally
var childCoordinators: [Coordinator] = []
// MARK: - Setup
@available(iOS 14.0, *)
init(parameters: LocationSharingCoordinatorParameters) {
self.parameters = parameters
let avatarData = AvatarInput(mxContentUri: parameters.user.avatarUrl,
matrixItemId: parameters.user.userId,
displayName: parameters.user.displayname)
let viewModel = LocationSharingViewModel(accessToken: "bDAfUcrMPWTAB1KB38r6", avatarData: avatarData)
let view = LocationSharingView(context: viewModel.context)
.addDependency(AvatarService.instantiate(mediaManager: parameters.mediaManager))
_locationSharingViewModel = viewModel
locationSharingHostingController = VectorHostingController(rootView: view)
}
// MARK: - Public
func start() {
guard #available(iOS 14.0, *) else {
MXLog.error("[LocationSharingCoordinator] start: Invalid iOS version, returning.")
return
}
parameters.navigationRouter.present(locationSharingHostingController, animated: true)
locationSharingViewModel.completion = { [weak self] result in
guard let self = self else { return }
switch result {
case .cancel:
self.parameters.navigationRouter.dismissModule(animated: true, completion: nil)
case .share(let latitude, let longitude):
self.locationSharingViewModel.dispatch(action: .startLoading)
self.parameters.roomDataSource.sendLocation(withLatitude: latitude,
longitude: longitude,
description: nil) { [weak self] _ in
guard let self = self else { return }
self.parameters.navigationRouter.dismissModule(animated: true, completion: nil)
self.locationSharingViewModel.dispatch(action: .stopLoading(nil))
} failure: { [weak self] error in
guard let self = self else { return }
MXLog.error("[LocationSharingCoordinator] Failed sharing location with error: \(String(describing: error))")
self.locationSharingViewModel.dispatch(action: .stopLoading(error))
}
}
}
}
}
@@ -0,0 +1,76 @@
//
// 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 SwiftUI
import Combine
import CoreLocation
enum LocationSharingViewError {
case failedLoadingMap
case failedLocatingUser
case invalidLocationAuthorization
case failedSharingLocation
}
enum LocationSharingStateAction {
case error(LocationSharingViewError, LocationSharingViewModelCallback?)
case startLoading
case stopLoading(Error?)
}
enum LocationSharingViewAction {
case cancel
case share
}
typealias LocationSharingViewModelCallback = ((LocationSharingViewModelResult) -> Void)
enum LocationSharingViewModelResult {
case cancel
case share(Double, Double)
}
@available(iOS 14, *)
struct LocationSharingViewState: BindableState {
let accessToken: String
let avatarData: AvatarInputProtocol
var shareButtonEnabled: Bool = true
var showLoadingIndicator: Bool = false
let errorSubject = PassthroughSubject<LocationSharingViewError, Never>()
var bindings = LocationSharingViewStateBindings()
}
struct LocationSharingViewStateBindings {
var alertInfo: ErrorAlertInfo?
var userLocation: CLLocationCoordinate2D?
}
struct ErrorAlertInfo: Identifiable {
enum AlertType {
case mapLoadingError
case userLocatingError
case authorizationError
case locationSharingError
}
let id: AlertType
let title: String
let message: String
let callback: (() -> Void)?
}
@@ -0,0 +1,35 @@
//
// 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 SwiftUI
@available(iOS 14.0, *)
enum MockLocationSharingScreenState: MockScreenState, CaseIterable {
case standard
var screenType: Any.Type {
MockLocationSharingScreenState.self
}
var screenView: ([Any], AnyView) {
let viewModel = LocationSharingViewModel(accessToken: "bDAfUcrMPWTAB1KB38r6",
avatarData: AvatarInput(mxContentUri: "", matrixItemId: "", displayName: "Alice"))
return ([viewModel],
AnyView(LocationSharingView(context: viewModel.context)
.addDependency(MockAvatarService.example)))
}
}
@@ -0,0 +1,111 @@
//
// 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 SwiftUI
import Combine
@available(iOS 14, *)
typealias LocationSharingViewModelType = StateStoreViewModel< LocationSharingViewState,
LocationSharingStateAction,
LocationSharingViewAction >
@available(iOS 14, *)
class LocationSharingViewModel: LocationSharingViewModelType {
// MARK: - Properties
// MARK: Private
// MARK: Public
let accessToken: String
let avatarData: AvatarInputProtocol
var completion: ((LocationSharingViewModelResult) -> Void)?
// MARK: - Setup
init(accessToken: String, avatarData: AvatarInputProtocol) {
self.accessToken = accessToken
self.avatarData = avatarData
super.init(initialViewState: LocationSharingViewState(accessToken: accessToken, avatarData: avatarData))
state.errorSubject.sink { [weak self] error in
guard let self = self else { return }
self.dispatch(action: .error(error, self.completion))
}.store(in: &cancellables)
}
// MARK: - Public
override func process(viewAction: LocationSharingViewAction) {
switch viewAction {
case .cancel:
completion?(.cancel)
case .share:
guard let location = state.bindings.userLocation else {
dispatch(action: .error(.failedLocatingUser, completion))
return
}
completion?(.share(location.latitude, location.longitude))
}
}
override class func reducer(state: inout LocationSharingViewState, action: LocationSharingStateAction) {
switch action {
case .error(let error, let completion):
let alertCallback: () -> Void = {
completion?(.cancel)
}
switch error {
case .failedLoadingMap:
state.bindings.alertInfo = ErrorAlertInfo(id: .mapLoadingError,
title: VectorL10n.locationSharingLoadingMapErrorTitle,
message: VectorL10n.locationSharingLoadingMapErrorMessage,
callback: alertCallback)
case .failedLocatingUser:
state.bindings.alertInfo = ErrorAlertInfo(id: .userLocatingError,
title: VectorL10n.locationSharingLocatingUserErrorTitle,
message: VectorL10n.locationSharingLocatingUserErrorMessage,
callback: alertCallback)
case .invalidLocationAuthorization:
state.bindings.alertInfo = ErrorAlertInfo(id: .authorizationError,
title: VectorL10n.locationSharingInvalidAuthorizationErrorTitle,
message: VectorL10n.locationSharingInvalidAuthorizationErrorMessage,
callback: alertCallback)
default:
break
}
case .startLoading:
state.showLoadingIndicator = true
state.shareButtonEnabled = false
case .stopLoading(let error):
state.showLoadingIndicator = false
state.shareButtonEnabled = true
if error != nil {
state.bindings.alertInfo = ErrorAlertInfo(id: .locationSharingError,
title: VectorL10n.locationSharingInvalidAuthorizationErrorTitle,
message: VectorL10n.locationSharingInvalidAuthorizationErrorMessage,
callback: nil)
}
}
}
}
@@ -0,0 +1,36 @@
//
// 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 XCTest
import RiotSwiftUI
@available(iOS 14.0, *)
class LocationSharingUITests: XCTestCase {
private var app: XCUIApplication!
override func setUp() {
continueAfterFailure = false
app = XCUIApplication()
app.launch()
app.buttons[MockLocationSharingScreenState.screenStateKeys.first!].tap()
}
func testInitialStateComponents() {
}
}
@@ -0,0 +1,35 @@
//
// 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 XCTest
import Combine
@testable import RiotSwiftUI
@available(iOS 14.0, *)
class LocationSharingViewModelTests: XCTestCase {
var viewModel: LocationSharingViewModel!
var context: LocationSharingViewModelType.Context!
var cancellables = Set<AnyCancellable>()
override func setUpWithError() throws {
context = viewModel.context
}
func testInitialState() {
}
}
@@ -0,0 +1,128 @@
//
// 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 SwiftUI
import Combine
import Mapbox
@available(iOS 14, *)
struct LocationSharingMapView: UIViewRepresentable {
private struct Constants {
static let mapZoomLevel = 15.0
static let mapStyleURLString = "https://api.maptiler.com/maps/streets/style.json?key="
}
let accessToken: String
let avatarData: AvatarInputProtocol
let errorSubject: PassthroughSubject<LocationSharingViewError, Never>
@Binding var userLocation: CLLocationCoordinate2D?
func makeUIView(context: Context) -> some UIView {
let url = URL(string: Constants.mapStyleURLString + accessToken)
let mapView = MGLMapView(frame: .zero, styleURL: url)
mapView.delegate = context.coordinator
mapView.logoView.isHidden = true
mapView.attributionButton.isHidden = true
mapView.showsUserLocation = true
mapView.userTrackingMode = .follow
return mapView
}
func updateUIView(_ uiView: UIViewType, context: Context) {
}
func makeCoordinator() -> LocationSharingMapViewCoordinator {
LocationSharingMapViewCoordinator(avatarData: avatarData,
errorSubject: errorSubject,
userLocation: $userLocation)
}
}
@available(iOS 14, *)
class LocationSharingMapViewCoordinator: NSObject, MGLMapViewDelegate {
private let avatarData: AvatarInputProtocol
private let errorSubject: PassthroughSubject<LocationSharingViewError, Never>
@Binding var userLocation: CLLocationCoordinate2D?
init(avatarData: AvatarInputProtocol,
errorSubject: PassthroughSubject<LocationSharingViewError, Never>,
userLocation: Binding<CLLocationCoordinate2D?>) {
self.avatarData = avatarData
self.errorSubject = errorSubject
self._userLocation = userLocation
}
// MARK: - MGLMapViewDelegate
func mapView(_ mapView: MGLMapView, viewFor annotation: MGLAnnotation) -> MGLAnnotationView? {
guard let _ = annotation as? MGLUserLocation else {
return nil
}
return UserLocationAnnotatonView(avatarData: avatarData)
}
func mapViewDidFailLoadingMap(_ mapView: MGLMapView, withError error: Error) {
errorSubject.send(.failedLoadingMap)
}
func mapView(_ mapView: MGLMapView, didFailToLocateUserWithError error: Error) {
errorSubject.send(.failedLocatingUser)
}
func mapView(_ mapView: MGLMapView, didUpdate userLocation: MGLUserLocation?) {
self.userLocation = userLocation?.coordinate
}
func mapView(_ mapView: MGLMapView, didChangeLocationManagerAuthorization manager: MGLLocationManager) {
switch manager.authorizationStatus {
case .restricted:
fallthrough
case .denied:
errorSubject.send(.failedLocatingUser)
default:
break
}
}
}
@available(iOS 14, *)
private class UserLocationAnnotatonView: MGLUserLocationAnnotationView {
init(avatarData: AvatarInputProtocol) {
super.init(frame: .zero)
guard let avatarImageView = UIHostingController(rootView: LocationSharingUserMarkerView(avatarData: avatarData)).view else {
return
}
addSubview(avatarImageView)
addConstraints([topAnchor.constraint(equalTo: avatarImageView.topAnchor),
leadingAnchor.constraint(equalTo: avatarImageView.leadingAnchor),
bottomAnchor.constraint(equalTo: avatarImageView.bottomAnchor),
trailingAnchor.constraint(equalTo: avatarImageView.trailingAnchor)])
}
required init?(coder: NSCoder) {
fatalError()
}
}
@@ -0,0 +1,53 @@
//
// 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 SwiftUI
@available(iOS 14.0, *)
struct LocationSharingUserMarkerView: View {
// MARK: - Properties
// MARK: Private
@Environment(\.theme) private var theme: ThemeSwiftUI
// MARK: Public
let avatarData: AvatarInputProtocol
var body: some View {
ZStack(alignment: .center) {
Image(uiImage: Asset.Images.locationUserMarker.image)
AvatarImage(avatarData: avatarData, size: .large)
.offset(.init(width: 0.0, height: -1.5))
}
.accentColor(theme.colors.accent)
}
}
// MARK: - Previews
@available(iOS 14.0, *)
struct LocationSharingUserMarkerView_Previews: PreviewProvider {
static var previews: some View {
let avatarData = AvatarInput(mxContentUri: "",
matrixItemId: "",
displayName: "Alice")
LocationSharingUserMarkerView(avatarData: avatarData)
}
}
@@ -0,0 +1,86 @@
//
// 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 SwiftUI
import CoreLocation
@available(iOS 14.0, *)
struct LocationSharingView: View {
// MARK: - Properties
// MARK: Private
@Environment(\.theme) private var theme: ThemeSwiftUI
// MARK: Public
@ObservedObject var context: LocationSharingViewModel.Context
var body: some View {
NavigationView {
LocationSharingMapView(accessToken: context.viewState.accessToken,
avatarData: context.viewState.avatarData,
errorSubject: context.viewState.errorSubject,
userLocation: $context.userLocation)
.toolbar {
ToolbarItem(placement: .navigationBarLeading) {
Button(VectorL10n.cancel, action: {
context.send(viewAction: .cancel)
})
}
ToolbarItem(placement: .principal) {
Text(VectorL10n.locationSharingTitle)
.font(.headline)
.foregroundColor(theme.colors.primaryContent)
}
ToolbarItem(placement: .navigationBarTrailing) {
Button(VectorL10n.locationSharingShareAction, action: {
context.send(viewAction: .share)
})
.disabled(!context.viewState.shareButtonEnabled)
}
}
.navigationBarTitleDisplayMode(.inline)
.ignoresSafeArea()
.alert(item: $context.alertInfo) { info in
Alert(title: Text(info.title), message: Text(info.message), dismissButton:
.default(Text(VectorL10n.pollTimelineVoteNotRegisteredAction)) {
info.callback?()
})
}
}
.accentColor(theme.colors.accent)
.activityIndicator(show: context.viewState.showLoadingIndicator)
}
@ViewBuilder
private var activityIndicator: some View {
if context.viewState.showLoadingIndicator {
ActivityIndicator()
}
}
}
// MARK: - Previews
@available(iOS 14.0, *)
struct LocationSharingView_Previews: PreviewProvider {
static let stateRenderer = MockLocationSharingScreenState.stateRenderer
static var previews: some View {
stateRenderer.screenGroup()
}
}