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,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()
}
}