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 8ea857c6b9..e21fbb0605 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 @@ -200,7 +200,15 @@ import kotlinx.coroutines.launch } } } - val userLocationState = userLocationStateFactory.create(hasLocationPermission = permissionsState.isAnyGranted) + val isCurrentlySharing by liveLocationShareManager.isCurrentlySharing(roomId = joinedRoom.roomId).collectAsState() + val hideUserLocationPuck = mode is ShowLocationMode.Live && isCurrentlySharing + val userLocationState = if (hideUserLocationPuck) { + // When sharing with this device, use the user LocationShareItem as source of data instead of the device. + val ownLocationShare by remember { derivedStateOf { updatedLocationShares.find { it.isOwnUser }?.location?.asMapLibreLocation() } } + UserLocationState(ownLocationShare) + } else { + userLocationStateFactory.create(hasLocationPermission = permissionsState.isAnyGranted) + } return ShowLocationState( customMapStyleUrl = customMapStyleUrl, dialogState = dialogState, @@ -210,7 +218,7 @@ import kotlinx.coroutines.launch userLocationState = userLocationState, isLive = mode is ShowLocationMode.Live, appName = appName, - hideUserLocationPuck = false, + hideUserLocationPuck = hideUserLocationPuck, eventSink = ::handleEvent, ) } diff --git a/features/location/impl/src/test/kotlin/io/element/android/features/location/impl/show/ShowLocationPresenterTest.kt b/features/location/impl/src/test/kotlin/io/element/android/features/location/impl/show/ShowLocationPresenterTest.kt index da8d766c88..ab06cc5911 100644 --- a/features/location/impl/src/test/kotlin/io/element/android/features/location/impl/show/ShowLocationPresenterTest.kt +++ b/features/location/impl/src/test/kotlin/io/element/android/features/location/impl/show/ShowLocationPresenterTest.kt @@ -460,4 +460,30 @@ class ShowLocationPresenterTest { } } + @Test + fun `live mode user location state uses own share position when sharing`() = runTest { + val ownLiveLocationShare = aLiveLocationShare() + val fakeRoom = FakeJoinedRoom( + liveLocationSharesFlow = MutableStateFlow(listOf(ownLiveLocationShare)) + ) + val manager = FakeActiveLiveLocationShareManager( + startShareLambda = { _, _ -> Result.success(Unit) } + ) + manager.startShare(fakeRoom.roomId, 1.hours) + + val presenter = createShowLocationPresenter( + mode = ShowLocationMode.Live(senderId = A_USER_ID), + joinedRoom = fakeRoom, + liveLocationShareManager = manager, + ) + val ownLocation = ownLiveLocationShare.lastLocation?.geoUri?.let(Location::fromGeoUri) + presenter.test { + skipItems(1) + val state = awaitItem() + assertThat(state.hideUserLocationPuck).isTrue() + val location = requireNotNull(state.userLocationState.location) + assertThat(location.position.value.latitude).isEqualTo(ownLocation?.lat) + assertThat(location.position.value.longitude).isEqualTo(ownLocation?.lon) + } + } }