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
import im.vector.app.features.home.room.detail.timeline.helper.LocationPinProvider
import im.vector.app.features.location.LocationData
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.channels.trySendBlocking
import androidx.lifecycle.asFlow
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.getRoom
import javax.inject.Inject
class GetListOfUserLiveLocationUseCase @Inject constructor(
private val session: Session,
private val locationPinProvider: LocationPinProvider
private val userLiveLocationViewStateMapper: UserLiveLocationViewStateMapper,
) {
// TODO add unit tests
fun execute(): Flow<List<UserLiveLocationViewState>> {
// TODO get room and call SDK to get the correct flow of locations
return callbackFlow {
val myUserId = session.myUserId
locationPinProvider.create(myUserId) { pinDrawable ->
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()
}
fun execute(roomId: String): Flow<List<UserLiveLocationViewState>> {
return session.getRoom(roomId)
?.locationSharingService()
?.getRunningLiveLocationShareSummaries()
?.asFlow()
?.mapLatest { it.mapNotNull { summary -> userLiveLocationViewStateMapper.map(summary) } }
?: emptyFlow()
}
}

View File

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