Refactor LiveLocationShare to include structured LastLocation

This commit is contained in:
ganfra 2026-04-10 14:43:24 +02:00
parent d6999b5334
commit 9ba8798175
3 changed files with 26 additions and 9 deletions

View file

@ -138,7 +138,8 @@ class ShowLocationPresenter(
val membersStateFlow = joinedRoom.membersStateFlow.mapState { it.joinedRoomMembers() }
combine(liveLocationSharesFlow, membersStateFlow) { liveShares, members ->
liveShares.mapNotNull { share ->
val location = Location.fromGeoUri(share.lastGeoUri) ?: return@mapNotNull null
val lastLocation = share.lastLocation ?: return@mapNotNull null
val location = Location.fromGeoUri(lastLocation.geoUri) ?: return@mapNotNull null
val member = members.find { it.userId == share.userId }
val displayName = member?.getBestName() ?: share.userId.value
val avatarUrl = member?.avatarUrl
@ -154,7 +155,7 @@ class ShowLocationPresenter(
formattedTimestamp = "Sharing live location",
location = location,
isLive = true,
assetType = AssetType.SENDER,
assetType = lastLocation.assetType,
)
}.toPersistentList()
}.collect { value = it }

View file

@ -15,8 +15,17 @@ import io.element.android.libraries.matrix.api.core.UserId
data class LiveLocationShare(
/** The user who is sharing their location. */
val userId: UserId,
/** The last known geo URI (e.g., "geo:51.5074,-0.1278"). */
val lastGeoUri: String,
/** The timestamp of the last location update. */
val lastTimestamp: Long,
/** The last known location if any. */
val lastLocation: LastLocation?,
/** The timestamp when location sharing ends, in milliseconds. */
val endTimestamp: Long,
)
data class LastLocation(
/** The last known geo URI (e.g., "geo:51.5074,-0.1278"). */
val geoUri: String,
/** The timestamp of the last location update. */
val timestamp: Long,
/** The asset of the last location update. */
val assetType: AssetType,
)

View file

@ -8,15 +8,16 @@
package io.element.android.libraries.matrix.impl.room.location
import io.element.android.libraries.matrix.api.core.UserId
import io.element.android.libraries.matrix.api.room.location.LastLocation
import io.element.android.libraries.matrix.api.room.location.LiveLocationShare
import io.element.android.libraries.matrix.impl.util.mxCallbackFlow
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.buffer
import org.matrix.rustcomponents.sdk.LiveLocationShare as RustLiveLocationShare
import org.matrix.rustcomponents.sdk.LiveLocationShareListener
import org.matrix.rustcomponents.sdk.LiveLocationShareUpdate
import org.matrix.rustcomponents.sdk.RoomInterface
import org.matrix.rustcomponents.sdk.LiveLocationShare as RustLiveLocationShare
fun RoomInterface.liveLocationSharesFlow(): Flow<List<LiveLocationShare>> {
fun MutableList<LiveLocationShare>.applyUpdate(update: LiveLocationShareUpdate) {
@ -53,8 +54,14 @@ fun RoomInterface.liveLocationSharesFlow(): Flow<List<LiveLocationShare>> {
private fun RustLiveLocationShare.into(): LiveLocationShare {
return LiveLocationShare(
userId = UserId(userId),
lastGeoUri = lastLocation?.location?.geoUri.orEmpty(),
lastTimestamp = lastLocation?.ts?.toLong() ?: 0,
lastLocation = lastLocation?.let {
LastLocation(
geoUri = it.location.geoUri,
timestamp = it.ts.toLong(),
assetType = it.location.asset.into(),
)
},
endTimestamp = (startTs + timeout).toLong()
)
}