mirror of
https://gitlab.opencode.de/bwi/bundesmessenger/clients/bundesmessenger-ios.git
synced 2026-04-21 17:12:45 +02:00
vector-im/element-ios/issues/5298 - Implemented location sharing from the input toolbar action menu.
This commit is contained in:
committed by
Stefan Ceriu
parent
fb16d96ac1
commit
37db201124
@@ -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()
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user