Observe the current live location shares in a room

This commit is contained in:
Maxime NATUREL 2022-05-16 18:01:54 +02:00
parent 7a7af40d61
commit 81b90df909
3 changed files with 69 additions and 38 deletions

View File

@ -16,51 +16,26 @@
package im.vector.app.features.location.live.map package im.vector.app.features.location.live.map
import im.vector.app.features.home.room.detail.timeline.helper.LocationPinProvider import androidx.lifecycle.asFlow
import im.vector.app.features.location.LocationData
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking
import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.mapLatest
import org.matrix.android.sdk.api.session.Session import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.getRoom
import javax.inject.Inject import javax.inject.Inject
class GetListOfUserLiveLocationUseCase @Inject constructor( class GetListOfUserLiveLocationUseCase @Inject constructor(
private val session: Session, private val session: Session,
private val locationPinProvider: LocationPinProvider private val userLiveLocationViewStateMapper: UserLiveLocationViewStateMapper,
) { ) {
// TODO add unit tests // TODO add unit tests
fun execute(): Flow<List<UserLiveLocationViewState>> { fun execute(roomId: String): Flow<List<UserLiveLocationViewState>> {
// TODO get room and call SDK to get the correct flow of locations return session.getRoom(roomId)
?.locationSharingService()
return callbackFlow { ?.getRunningLiveLocationShareSummaries()
val myUserId = session.myUserId ?.asFlow()
?.mapLatest { it.mapNotNull { summary -> userLiveLocationViewStateMapper.map(summary) } }
locationPinProvider.create(myUserId) { pinDrawable -> ?: emptyFlow()
val user1 = UserLiveLocationViewState(
userId = session.myUserId,
pinDrawable = pinDrawable,
locationData = LocationData(
latitude = 48.863447,
longitude = 2.328608,
uncertainty = null
)
)
val user2 = UserLiveLocationViewState(
userId = session.myUserId,
pinDrawable = pinDrawable,
locationData = LocationData(
latitude = 48.843816,
longitude = 2.359235,
uncertainty = null
)
)
val userLocations = listOf(user1, user2)
trySendBlocking(userLocations)
channel.close()
}
awaitClose()
}
} }
} }

View File

@ -40,7 +40,7 @@ class LocationLiveMapViewModel @AssistedInject constructor(
companion object : MavericksViewModelFactory<LocationLiveMapViewModel, LocationLiveMapViewState> by hiltMavericksViewModelFactory() companion object : MavericksViewModelFactory<LocationLiveMapViewModel, LocationLiveMapViewState> by hiltMavericksViewModelFactory()
init { init {
getListOfUserLiveLocationUseCase.execute() getListOfUserLiveLocationUseCase.execute(initialState.roomId)
.onEach { setState { copy(userLocations = it) } } .onEach { setState { copy(userLocations = it) } }
.launchIn(viewModelScope) .launchIn(viewModelScope)
} }

View File

@ -0,0 +1,56 @@
/*
* Copyright (c) 2022 New Vector Ltd
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package im.vector.app.features.location.live.map
import im.vector.app.features.home.room.detail.timeline.helper.LocationPinProvider
import im.vector.app.features.location.toLocationData
import kotlinx.coroutines.suspendCancellableCoroutine
import org.matrix.android.sdk.api.session.room.model.livelocation.LiveLocationShareAggregatedSummary
import javax.inject.Inject
// TODO add unit tests
class UserLiveLocationViewStateMapper @Inject constructor(
private val locationPinProvider: LocationPinProvider,
) {
suspend fun map(liveLocationShareAggregatedSummary: LiveLocationShareAggregatedSummary) =
suspendCancellableCoroutine<UserLiveLocationViewState?> { continuation ->
val userId = liveLocationShareAggregatedSummary.userId
val locationData = liveLocationShareAggregatedSummary.lastLocationDataContent
?.getBestLocationInfo()
?.geoUri
.toLocationData()
when {
userId.isNullOrEmpty() || locationData == null -> continuation.resume(null) {
// do nothing on cancellation
}
else -> {
locationPinProvider.create(userId) { pinDrawable ->
val viewState = UserLiveLocationViewState(
userId = userId,
pinDrawable = pinDrawable,
locationData = locationData
)
continuation.resume(viewState) {
// do nothing on cancellation
}
}
}
}
}
}