mirror of
https://github.com/Novaloop-AG/element-x-ios.git
synced 2026-10-31 16:24:11 +00:00
acd18f3aa9
Which would override each other and break the UI state
181 lines
6.4 KiB
Swift
181 lines
6.4 KiB
Swift
//
|
|
// Copyright 2025 Element Creations Ltd.
|
|
// Copyright 2023-2025 New Vector Ltd.
|
|
//
|
|
// SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial.
|
|
// Please see LICENSE files in the repository root for full details.
|
|
//
|
|
|
|
import Compound
|
|
import SwiftUI
|
|
|
|
struct LocationSharingScreen: View {
|
|
@Bindable var context: LocationSharingScreenViewModel.Context
|
|
|
|
var body: some View {
|
|
switch context.viewState.interactionMode {
|
|
case .picker:
|
|
mainContent
|
|
.locationSheet {
|
|
LocationPickerSheet(context: context)
|
|
.alert(item: $context.alertInfo)
|
|
}
|
|
case .viewStatic:
|
|
mainContent
|
|
.locationSheet {
|
|
StaticLocationSheet(context: context)
|
|
.alert(item: $context.alertInfo)
|
|
}
|
|
case .viewLive:
|
|
mainContent
|
|
.locationSheet {
|
|
LiveLocationSheet(context: context)
|
|
.alert(item: $context.alertInfo)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Private
|
|
|
|
private var mainContent: some View {
|
|
mapView
|
|
.ignoresSafeArea(edges: .bottom)
|
|
.track(screen: context.viewState.interactionMode.isPicker ? .LocationSend : .LocationView)
|
|
.navigationTitle(L10n.screenViewLocationTitle)
|
|
.navigationBarTitleDisplayMode(.inline)
|
|
.toolbar { toolbar }
|
|
}
|
|
|
|
private var mapView: some View {
|
|
ZStack(alignment: .center) {
|
|
MapLibreMapView(mapURLBuilder: context.viewState.mapURLBuilder,
|
|
options: mapOptions,
|
|
mediaProvider: context.mediaProvider,
|
|
showsUserLocationMode: $context.showsUserLocationMode,
|
|
error: $context.mapError,
|
|
mapCenterCoordinate: $context.mapCenterLocation,
|
|
hasLoadedUserLocation: $context.hasLoadedUserLocation,
|
|
isLocationAuthorized: $context.isLocationAuthorized,
|
|
geolocationUncertainty: $context.geolocationUncertainty) {
|
|
context.send(viewAction: .userDidPan)
|
|
}
|
|
.ignoresSafeArea(edges: mapSafeAreaEdges)
|
|
|
|
if let pickerMarkerKind = context.viewState.pickerMarkerKind {
|
|
LocationMarkerView(kind: pickerMarkerKind, mediaProvider: context.mediaProvider)
|
|
}
|
|
}
|
|
.overlay(alignment: .topTrailing) {
|
|
centerToUserLocationButton
|
|
}
|
|
}
|
|
|
|
private var mapSafeAreaEdges: Edge.Set {
|
|
context.viewState.interactionMode.isPicker ? .horizontal : [.horizontal, .bottom]
|
|
}
|
|
|
|
@ToolbarContentBuilder
|
|
private var toolbar: some ToolbarContent {
|
|
ToolbarItem(placement: .primaryAction) {
|
|
ToolbarButton(role: .close) {
|
|
context.send(viewAction: .close)
|
|
}
|
|
}
|
|
}
|
|
|
|
private var mapOptions: MapLibreMapView.Options {
|
|
.init(zoomLevel: context.viewState.zoomLevel,
|
|
initialZoomLevel: context.viewState.initialZoomLevel,
|
|
mapCenter: context.viewState.initialMapCenter,
|
|
annotations: context.viewState.annotations)
|
|
}
|
|
|
|
@ViewBuilder
|
|
private var centerToUseIcon: some View {
|
|
if context.viewState.isLocationLoading {
|
|
ProgressView()
|
|
.tint(.compound.iconPrimary)
|
|
.padding(13)
|
|
} else {
|
|
CompoundIcon(context.viewState.isMapCenteredOnUser ? \.locationNavigatorCentred : \.locationNavigator)
|
|
.foregroundStyle(.compound.iconPrimary)
|
|
.padding(13)
|
|
}
|
|
}
|
|
|
|
private var centerToUserLocationButton: some View {
|
|
Button {
|
|
context.send(viewAction: .centerToUser)
|
|
} label: {
|
|
if #available(iOS 26.0, *) {
|
|
centerToUseIcon
|
|
.glassEffect(.regular.interactive(), in: Circle())
|
|
.tint(.compound.bgCanvasDefault)
|
|
} else {
|
|
centerToUseIcon
|
|
.background(.compound.bgCanvasDefault, in: RoundedRectangle(cornerRadius: 6))
|
|
}
|
|
}
|
|
.disabled(context.viewState.isLocationLoading)
|
|
.dynamicTypeSize(.large)
|
|
.padding(13)
|
|
}
|
|
}
|
|
|
|
private extension View {
|
|
/// The Mac hides a sheet's parent sheet while it's presented, taking the close button with it.
|
|
@ViewBuilder
|
|
func locationSheet<Content: View>(@ViewBuilder content: @escaping () -> Content) -> some View {
|
|
if ProcessInfo.processInfo.isiOSAppOnMac {
|
|
safeAreaInset(edge: .bottom, spacing: 0) {
|
|
content()
|
|
.fixedSize(horizontal: false, vertical: true)
|
|
.background(.compound.bgCanvasDefault)
|
|
}
|
|
} else {
|
|
sheet(isPresented: .constant(true), content: content)
|
|
}
|
|
}
|
|
}
|
|
|
|
// MARK: - Previews
|
|
|
|
struct LocationSharingScreen_Previews: PreviewProvider, TestablePreview {
|
|
static let viewModel = LocationSharingScreenViewModel.mock(type: .staticSenderLocation)
|
|
|
|
static let withoutLiveSharingViewModel = LocationSharingScreenViewModel.mock(type: .pickerWithoutLiveLocationOption)
|
|
|
|
static let pinViewModel = LocationSharingScreenViewModel.mock(type: .staticPinLocation)
|
|
|
|
static let pickerViewModel = LocationSharingScreenViewModel.mock(type: .picker)
|
|
|
|
static let liveLocationViewModel = LocationSharingScreenViewModel.mock(type: .viewLive)
|
|
|
|
static var previews: some View {
|
|
ElementNavigationStack {
|
|
LocationSharingScreen(context: pickerViewModel.context)
|
|
}
|
|
.previewDisplayName("Picker")
|
|
|
|
ElementNavigationStack {
|
|
LocationSharingScreen(context: withoutLiveSharingViewModel.context)
|
|
}
|
|
.previewDisplayName("Picker without live location sharing")
|
|
|
|
ElementNavigationStack {
|
|
LocationSharingScreen(context: viewModel.context)
|
|
}
|
|
.previewDisplayName("User Static Location")
|
|
|
|
ElementNavigationStack {
|
|
LocationSharingScreen(context: pinViewModel.context)
|
|
}
|
|
.previewDisplayName("Pin Static Location")
|
|
|
|
ElementNavigationStack {
|
|
LocationSharingScreen(context: liveLocationViewModel.context)
|
|
}
|
|
.previewDisplayName("Live Location")
|
|
}
|
|
}
|