Refactor live location shares to use callbackFlow

This commit is contained in:
ganfra 2026-04-10 20:45:18 +02:00
parent 775d5692aa
commit 5f3226107a

View file

@ -10,10 +10,12 @@ 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.core.UserId
import io.element.android.libraries.matrix.api.room.location.LastLocation 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.api.room.location.LiveLocationShare
import io.element.android.libraries.matrix.impl.util.mxCallbackFlow import io.element.android.libraries.matrix.impl.util.cancelAndDestroy
import kotlinx.coroutines.channels.Channel import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.buffer import kotlinx.coroutines.flow.buffer
import kotlinx.coroutines.flow.callbackFlow
import org.matrix.rustcomponents.sdk.LiveLocationShareListener import org.matrix.rustcomponents.sdk.LiveLocationShareListener
import org.matrix.rustcomponents.sdk.LiveLocationShareUpdate import org.matrix.rustcomponents.sdk.LiveLocationShareUpdate
import org.matrix.rustcomponents.sdk.RoomInterface import org.matrix.rustcomponents.sdk.RoomInterface
@ -38,9 +40,10 @@ fun RoomInterface.liveLocationSharesFlow(): Flow<List<LiveLocationShare>> {
is LiveLocationShareUpdate.Truncate -> subList(update.length.toInt(), size).clear() is LiveLocationShareUpdate.Truncate -> subList(update.length.toInt(), size).clear()
} }
} }
return mxCallbackFlow { return callbackFlow {
val liveLocationShares = liveLocationShares()
val shares: MutableList<LiveLocationShare> = ArrayList() val shares: MutableList<LiveLocationShare> = ArrayList()
subscribeToLiveLocationShares(object : LiveLocationShareListener { val taskHandle = liveLocationShares.subscribe(object : LiveLocationShareListener {
override fun onUpdate(updates: List<LiveLocationShareUpdate>) { override fun onUpdate(updates: List<LiveLocationShareUpdate>) {
for (update in updates) { for (update in updates) {
shares.applyUpdate(update) shares.applyUpdate(update)
@ -48,6 +51,10 @@ fun RoomInterface.liveLocationSharesFlow(): Flow<List<LiveLocationShare>> {
trySend(shares) trySend(shares)
} }
}) })
awaitClose {
taskHandle.cancelAndDestroy()
liveLocationShares.destroy()
}
}.buffer(Channel.UNLIMITED) }.buffer(Channel.UNLIMITED)
} }