From 6414d74c40141140c1d18d12fb8993263165be6c Mon Sep 17 00:00:00 2001 From: ganfra Date: Fri, 13 Mar 2026 18:41:32 +0100 Subject: [PATCH] Simplify ShowLocationState --- .../impl/common/ui/LocationShareRow.kt | 10 +++-- .../impl/show/ShowLocationPresenter.kt | 34 +---------------- .../location/impl/show/ShowLocationState.kt | 18 ++++++++- .../impl/show/ShowLocationStateProvider.kt | 38 +++---------------- .../location/impl/show/ShowLocationView.kt | 9 +++-- 5 files changed, 37 insertions(+), 72 deletions(-) diff --git a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/common/ui/LocationShareRow.kt b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/common/ui/LocationShareRow.kt index 9e5e35b2ba..b949f55c76 100644 --- a/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/common/ui/LocationShareRow.kt +++ b/features/location/impl/src/main/kotlin/io/element/android/features/location/impl/common/ui/LocationShareRow.kt @@ -78,7 +78,11 @@ fun LocationShareRow( modifier = Modifier.size(16.dp), ) } else { - val icon = if (item.assetType == AssetType.PIN) CompoundIcons.LocationNavigator() else CompoundIcons.LocationNavigatorCentred() + val icon = if (item.assetType == AssetType.PIN) { + CompoundIcons.LocationNavigator() + } else { + CompoundIcons.LocationNavigatorCentred() + } Icon( imageVector = icon, contentDescription = null, @@ -120,8 +124,8 @@ internal fun LocationShareRowPreview() = ElementPreview { size = AvatarSize.UserListItem, ), formattedTimestamp = "Shared 1 min ago", - assetType = AssetType.SENDER, isLive = true, + assetType = AssetType.SENDER, location = Location(0.0, 0.0) ), onShareClick = {}, @@ -136,9 +140,9 @@ internal fun LocationShareRowPreview() = ElementPreview { url = null, size = AvatarSize.UserListItem, ), + isLive = false, assetType = AssetType.PIN, formattedTimestamp = "Shared 5 hours ago", - isLive = false, location = Location(0.0, 0.0) ), onShareClick = {}, 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 7ecab4924e..a2c9a3702d 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 @@ -28,15 +28,12 @@ import io.element.android.features.location.impl.common.permissions.PermissionsP import io.element.android.features.location.impl.common.permissions.PermissionsState import io.element.android.features.location.impl.common.toDialogState 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.architecture.Presenter import io.element.android.libraries.core.meta.BuildMeta import io.element.android.libraries.dateformatter.api.DateFormatter import io.element.android.libraries.dateformatter.api.DateFormatterMode -import io.element.android.libraries.designsystem.components.PinVariant import io.element.android.libraries.designsystem.components.avatar.AvatarData import io.element.android.libraries.designsystem.components.avatar.AvatarSize -import io.element.android.libraries.matrix.api.room.location.AssetType import io.element.android.libraries.ui.strings.CommonStrings import io.element.android.services.toolbox.api.strings.StringProvider import kotlinx.collections.immutable.persistentListOf @@ -99,34 +96,6 @@ class ShowLocationPresenter( } } - val markers = remember { - when (mode) { - is ShowLocationMode.Static -> { - val pinVariant = if (mode.assetType == AssetType.PIN) { - PinVariant.PinnedLocation - } else { - PinVariant.UserLocation( - avatarData = AvatarData( - id = mode.senderId.value, - name = mode.senderName, - url = mode.senderAvatarUrl, - size = AvatarSize.UserListItem, - ), - isLive = false, - ) - } - persistentListOf( - LocationMarkerData( - id = mode.senderId.value, - location = mode.location, - variant = pinVariant, - ) - ) - } - ShowLocationMode.Live -> persistentListOf() - } - } - val locationShares = remember { when (mode) { is ShowLocationMode.Static -> { @@ -146,9 +115,9 @@ class ShowLocationPresenter( size = AvatarSize.UserListItem, ), formattedTimestamp = formattedTimestamp, + location = mode.location, isLive = false, assetType = mode.assetType, - location = mode.location, ) ) } @@ -158,7 +127,6 @@ class ShowLocationPresenter( return ShowLocationState( dialogState = dialogState, - markers = markers, locationShares = locationShares, hasLocationPermission = permissionsState.isAnyGranted, isTrackMyLocation = isTrackMyLocation, 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 f6c2e6e18f..9494db12ec 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 @@ -11,6 +11,7 @@ package io.element.android.features.location.impl.show import io.element.android.features.location.api.Location 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 import io.element.android.libraries.designsystem.components.avatar.AvatarData import io.element.android.libraries.matrix.api.core.UserId import io.element.android.libraries.matrix.api.room.location.AssetType @@ -18,7 +19,6 @@ import kotlinx.collections.immutable.ImmutableList data class ShowLocationState( val dialogState: LocationConstraintsDialogState, - val markers: ImmutableList, val locationShares: ImmutableList, val hasLocationPermission: Boolean, val isTrackMyLocation: Boolean, @@ -37,3 +37,19 @@ data class LocationShareItem( val isLive: Boolean, val assetType: AssetType?, ) + +fun LocationShareItem.toMarkerData(): LocationMarkerData { + val pinVariant = if (assetType == AssetType.PIN) { + PinVariant.PinnedLocation + } else { + PinVariant.UserLocation( + avatarData = avatarData, + isLive = isLive, + ) + } + return LocationMarkerData( + id = userId.value, + location = location, + variant = pinVariant, + ) +} 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 274ccfc8f2..8bee410715 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 @@ -11,16 +11,12 @@ package io.element.android.features.location.impl.show import androidx.compose.ui.tooling.preview.PreviewParameterProvider import io.element.android.features.location.api.Location 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 import io.element.android.libraries.designsystem.components.avatar.AvatarData import io.element.android.libraries.designsystem.components.avatar.AvatarSize import io.element.android.libraries.matrix.api.core.UserId import io.element.android.libraries.matrix.api.room.location.AssetType import kotlinx.collections.immutable.toImmutableList -private const val APP_NAME = "ApplicationName" - class ShowLocationStateProvider : PreviewParameterProvider { override val values: Sequence get() = sequenceOf( @@ -45,13 +41,10 @@ class ShowLocationStateProvider : PreviewParameterProvider { ) } -private val defaultLocation = Location(1.23, 2.34, 4f) -private val defaultSenderId = UserId("@alice:matrix.org") -private const val defaultSenderName = "Alice" +private const val APP_NAME = "ApplicationName" fun aShowLocationState( constraintsDialogState: LocationConstraintsDialogState = LocationConstraintsDialogState.None, - markers: List = listOf(aLocationMarkerData()), locationShares: List = listOf(aLocationShareItem()), hasLocationPermission: Boolean = false, isTrackMyLocation: Boolean = false, @@ -60,7 +53,6 @@ fun aShowLocationState( ): ShowLocationState { return ShowLocationState( dialogState = constraintsDialogState, - markers = markers.toImmutableList(), locationShares = locationShares.toImmutableList(), hasLocationPermission = hasLocationPermission, isTrackMyLocation = isTrackMyLocation, @@ -69,35 +61,17 @@ fun aShowLocationState( ) } -fun aLocationMarkerData( - id: String = defaultSenderId.value, - location: Location = defaultLocation, - variant: PinVariant = PinVariant.UserLocation( - avatarData = AvatarData( - id = defaultSenderId.value, - name = defaultSenderName, - url = null, - size = AvatarSize.LocationPin, - ), - isLive = false, - ), -) = LocationMarkerData( - id = id, - location = location, - variant = variant, -) - fun aLocationShareItem( - userId: UserId = defaultSenderId, - displayName: String = defaultSenderName, + userId: UserId = UserId("@alice:matrix.org"), + displayName: String = "Alice", avatarData: AvatarData = AvatarData( - id = defaultSenderId.value, - name = defaultSenderName, + id = userId.value, + name = displayName, url = null, size = AvatarSize.UserListItem, ), formattedTimestamp: String = "Shared 1 min ago", - location: Location = defaultLocation, + location: Location = Location(1.23, 2.34, 4f), isLive: Boolean = false, assetType: AssetType? = null, ) = LocationShareItem( 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 d97cef81d0..ad2d4cb8ca 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 @@ -66,10 +66,10 @@ fun ShowLocationView( ) val initialPosition = remember { - if (state.markers.isEmpty()) { + if (state.locationShares.isEmpty()) { MapDefaults.defaultCameraPosition } else { - val firstLocation = state.markers.first().location + val firstLocation = state.locationShares.first().location CameraPosition( target = Position(latitude = firstLocation.lat, longitude = firstLocation.lon), zoom = MapDefaults.DEFAULT_ZOOM @@ -147,7 +147,10 @@ fun ShowLocationView( locationState = userLocationState, trackUserLocation = state.isTrackMyLocation ) - LocationPinMarkers(state.markers) + val markers = remember(state.locationShares) { + state.locationShares.map { it.toMarkerData() } + } + LocationPinMarkers(markers) }, overlayContent = { LocationFloatingActionButton(