From 537063d899ce2577147798c7c7eb7bc2312d6de1 Mon Sep 17 00:00:00 2001 From: ganfra Date: Fri, 10 Apr 2026 21:11:30 +0200 Subject: [PATCH] Add focused location tracking when opening the map --- .../features/location/api/ShowLocationMode.kt | 4 +++- .../impl/show/ShowLocationPresenter.kt | 13 ++++++++---- .../location/impl/show/ShowLocationState.kt | 2 +- .../impl/show/ShowLocationStateProvider.kt | 2 ++ .../location/impl/show/ShowLocationView.kt | 21 +++++++++++-------- .../messages/impl/MessagesFlowNode.kt | 2 +- 6 files changed, 28 insertions(+), 16 deletions(-) diff --git a/features/location/api/src/main/kotlin/io/element/android/features/location/api/ShowLocationMode.kt b/features/location/api/src/main/kotlin/io/element/android/features/location/api/ShowLocationMode.kt index 1227ddec46..3feeeff57d 100644 --- a/features/location/api/src/main/kotlin/io/element/android/features/location/api/ShowLocationMode.kt +++ b/features/location/api/src/main/kotlin/io/element/android/features/location/api/ShowLocationMode.kt @@ -24,5 +24,7 @@ sealed interface ShowLocationMode : Parcelable { ) : ShowLocationMode @Parcelize - data object Live : ShowLocationMode + data class Live( + val senderId: UserId + ) : ShowLocationMode } diff --git a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationPresenter.kt b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationPresenter.kt index c46805684e..2f9c3d0d81 100644 --- a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationPresenter.kt +++ b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationPresenter.kt @@ -131,8 +131,8 @@ class ShowLocationPresenter( ) } } - ShowLocationMode.Live -> { - val liveShares by produceState(persistentListOf()) { + is ShowLocationMode.Live -> { + produceState(persistentListOf()) { val liveLocationSharesFlow = joinedRoom.subscribeToLiveLocationShares() val membersStateFlow = joinedRoom.membersStateFlow.mapState { it.joinedRoomMembers() } combine(liveLocationSharesFlow, membersStateFlow) { liveShares, members -> @@ -158,14 +158,19 @@ class ShowLocationPresenter( ) }.toPersistentList() }.collect { value = it } - } - liveShares + }.value } } + val focusedLocation = when (mode) { + is ShowLocationMode.Static -> locationShares.firstOrNull() + is ShowLocationMode.Live -> locationShares.firstOrNull { it.userId == mode.senderId } + } + return ShowLocationState( dialogState = dialogState, locationShares = locationShares, + focusedLocation = focusedLocation, hasLocationPermission = permissionsState.isAnyGranted, isTrackMyLocation = isTrackMyLocation, isLive = mode is ShowLocationMode.Live, diff --git a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationState.kt b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationState.kt index 24090a1504..3d4df465f9 100644 --- a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationState.kt +++ b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationState.kt @@ -9,7 +9,6 @@ package io.element.android.features.location.impl.show import io.element.android.features.location.api.Location -import io.element.android.features.location.api.ShowLocationMode import io.element.android.features.location.impl.common.ui.LocationConstraintsDialogState import io.element.android.features.location.impl.common.ui.LocationMarkerData import io.element.android.libraries.designsystem.components.PinVariant @@ -22,6 +21,7 @@ data class ShowLocationState( val isLive: Boolean, val dialogState: LocationConstraintsDialogState, val locationShares: ImmutableList, + val focusedLocation: LocationShareItem?, val hasLocationPermission: Boolean, val isTrackMyLocation: Boolean, val appName: String, diff --git a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationStateProvider.kt b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationStateProvider.kt index 774a97d284..3b08e81890 100644 --- a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationStateProvider.kt +++ b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationStateProvider.kt @@ -50,6 +50,7 @@ fun aShowLocationState( isLive: Boolean = false, constraintsDialogState: LocationConstraintsDialogState = LocationConstraintsDialogState.None, locationShares: List = listOf(aLocationShareItem(isLive = isLive)), + focusedLocation: LocationShareItem? = locationShares.firstOrNull(), hasLocationPermission: Boolean = false, isTrackMyLocation: Boolean = false, appName: String = APP_NAME, @@ -58,6 +59,7 @@ fun aShowLocationState( return ShowLocationState( dialogState = constraintsDialogState, locationShares = locationShares.toImmutableList(), + focusedLocation = focusedLocation, hasLocationPermission = hasLocationPermission, isTrackMyLocation = isTrackMyLocation, appName = appName, diff --git a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationView.kt b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationView.kt index 30bb027bb5..de35430b39 100644 --- a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationView.kt +++ b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/show/ShowLocationView.kt @@ -22,8 +22,11 @@ import androidx.compose.material3.rememberBottomSheetScaffoldState import androidx.compose.material3.rememberStandardBottomSheetState import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.runtime.setValue import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource @@ -67,25 +70,25 @@ fun ShowLocationView( onDismiss = { state.eventSink(ShowLocationEvent.DismissDialog) }, ) - val initialPosition = remember { - if (state.locationShares.isEmpty()) { - MapDefaults.defaultCameraPosition - } else { - val firstLocation = state.locationShares.first().location - CameraPosition( - target = Position(latitude = firstLocation.lat, longitude = firstLocation.lon), + val cameraState = rememberCameraState(firstPosition = MapDefaults.defaultCameraPosition) + var hasAnimatedToFocusedLocation by remember { mutableStateOf(false) } + LaunchedEffect(state.focusedLocation) { + if (state.focusedLocation != null && !hasAnimatedToFocusedLocation) { + hasAnimatedToFocusedLocation = true + val position = CameraPosition( + target = Position(latitude = state.focusedLocation.location.lat, longitude = state.focusedLocation.location.lon), zoom = MapDefaults.DEFAULT_ZOOM ) + cameraState.position = position } } - val cameraState = rememberCameraState(firstPosition = initialPosition) - val userLocationState = rememberUserLocationState(state.hasLocationPermission) LaunchedEffect(cameraState.isCameraMoving) { if (cameraState.moveReason == CameraMoveReason.GESTURE) { state.eventSink(ShowLocationEvent.TrackMyLocation(false)) } } + val userLocationState = rememberUserLocationState(state.hasLocationPermission) val scaffoldState = rememberBottomSheetScaffoldState( bottomSheetState = rememberStandardBottomSheetState( initialValue = diff --git a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/MessagesFlowNode.kt b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/MessagesFlowNode.kt index dc0ade0dc2..5affdb4484 100644 --- a/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/MessagesFlowNode.kt +++ b/features/messages/impl/src/main/kotlin/io/element/android/features/messages/impl/MessagesFlowNode.kt @@ -571,7 +571,7 @@ class MessagesFlowNode( } is TimelineItemLocationContent -> { val mode = when(event.content.mode){ - is TimelineItemLocationContent.Mode.Live -> ShowLocationMode.Live + is TimelineItemLocationContent.Mode.Live -> ShowLocationMode.Live(event.senderId) is TimelineItemLocationContent.Mode.Static -> ShowLocationMode.Static( location = event.content.mode.location, senderName = event.safeSenderName,