Code review fixes.

This commit is contained in:
Onuray Sahin 2022-04-14 16:23:43 +03:00
parent 023a00d160
commit f49e7d9619
4 changed files with 64 additions and 38 deletions

View file

@ -68,9 +68,16 @@ interface StateService {
/**
* Stops sharing live location in the room
* @param beaconInfoStateEvent Initial beacon info state event
* @param userId user id
*/
suspend fun stopLiveLocation(beaconInfoStateEvent: Event)
suspend fun stopLiveLocation(userId: String)
/**
* Returns beacon info state event of a user
* @param userId user id who is sharing location
* @param filterOnlyLive filters only ongoing live location sharing beacons if true else ended event is included
*/
suspend fun getLiveLocationBeaconInfo(userId: String, filterOnlyLive: Boolean): Event?
/**
* Send a state event to the room

View file

@ -21,6 +21,7 @@ import androidx.lifecycle.LiveData
import dagger.assisted.Assisted
import dagger.assisted.AssistedFactory
import dagger.assisted.AssistedInject
import org.matrix.android.sdk.api.extensions.orFalse
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.events.model.Event
import org.matrix.android.sdk.api.session.events.model.EventType
@ -190,7 +191,8 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private
updateJoinRule(RoomJoinRules.RESTRICTED, null, allowEntries)
}
override suspend fun stopLiveLocation(beaconInfoStateEvent: Event) {
override suspend fun stopLiveLocation(userId: String) {
getLiveLocationBeaconInfo(userId, true)?.let { beaconInfoStateEvent ->
beaconInfoStateEvent.getClearContent()?.toModel<LiveLocationBeaconContent>()?.let { content ->
val beaconContent = LiveLocationBeaconContent(
unstableBeaconInfo = BeaconInfo(
@ -210,4 +212,20 @@ internal class DefaultStateService @AssistedInject constructor(@Assisted private
}
}
}
}
override suspend fun getLiveLocationBeaconInfo(userId: String, filterOnlyLive: Boolean): Event? {
return EventType.STATE_ROOM_BEACON_INFO
.mapNotNull {
stateEventDataSource.getStateEvent(
roomId = roomId,
eventType = it,
stateKey = QueryStringValue.Equals(userId)
)
}
.firstOrNull { beaconInfoEvent ->
!filterOnlyLive ||
beaconInfoEvent.getClearContent()?.toModel<LiveLocationBeaconContent>()?.getBestBeaconInfo()?.isLive.orFalse()
}
}
}

View file

@ -28,7 +28,6 @@ import im.vector.app.features.notifications.NotificationUtils
import im.vector.app.features.session.coroutineScope
import kotlinx.coroutines.launch
import kotlinx.parcelize.Parcelize
import org.matrix.android.sdk.api.query.QueryStringValue
import org.matrix.android.sdk.api.session.Session
import org.matrix.android.sdk.api.session.events.model.EventType
import org.matrix.android.sdk.api.session.events.model.toContent
@ -88,7 +87,7 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
.getSafeActiveSession()
?.let { session ->
session.coroutineScope.launch(session.coroutineDispatchers.io) {
sendBeaconInfo(session, roomArgs)
sendLiveBeaconInfo(session, roomArgs)
}
}
}
@ -96,7 +95,7 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
return START_STICKY
}
private suspend fun sendBeaconInfo(session: Session, roomArgs: RoomArgs) {
private suspend fun sendLiveBeaconInfo(session: Session, roomArgs: RoomArgs) {
val beaconContent = LiveLocationBeaconContent(
unstableBeaconInfo = BeaconInfo(
timeout = roomArgs.durationMillis,
@ -134,7 +133,7 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
Timber.i("### LocationSharingService.stopSharingLocation for $roomId")
// Send a new beacon info state by setting live field as false
updateStoppedBeaconInfo(roomId)
sendStoppedBeaconInfo(roomId)
synchronized(roomArgsList) {
roomArgsList.removeAll { it.roomId == roomId }
@ -145,21 +144,12 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
}
}
private fun updateStoppedBeaconInfo(roomId: String) {
private fun sendStoppedBeaconInfo(roomId: String) {
activeSessionHolder
.getSafeActiveSession()
?.let { session ->
session.coroutineScope.launch(session.coroutineDispatchers.io) {
val room = session.getRoom(roomId)
EventType
.STATE_ROOM_BEACON_INFO
.mapNotNull {
room?.getStateEvent(it, QueryStringValue.Equals(session.myUserId))
}
.firstOrNull()
?.let { beaconInfoEvent ->
room?.stopLiveLocation(beaconInfoEvent)
}
session.getRoom(roomId)?.stopLiveLocation(session.myUserId)
}
}
}
@ -167,16 +157,26 @@ class LocationSharingService : VectorService(), LocationTracker.Callback {
override fun onLocationUpdate(locationData: LocationData) {
Timber.i("### LocationSharingService.onLocationUpdate. Uncertainty: ${locationData.uncertainty}")
val session = activeSessionHolder.getSafeActiveSession()
// Emit location update to all rooms in which live location sharing is active
session?.coroutineScope?.launch(session.coroutineDispatchers.io) {
roomArgsList.toList().forEach { roomArg ->
sendLiveLocation(roomArg.roomId, locationData)
}
}
}
private suspend fun sendLiveLocation(roomId: String, locationData: LocationData) {
val session = activeSessionHolder.getSafeActiveSession()
val room = session?.getRoom(roomId)
val userId = session?.myUserId
if (room == null || userId == null) {
return
}
private fun sendLiveLocation(roomId: String, locationData: LocationData) {
val room = activeSessionHolder.getSafeActiveSession()?.getRoom(roomId)
room
?.getStateEvent(EventType.STATE_ROOM_BEACON_INFO.first())
.getLiveLocationBeaconInfo(userId, true)
?.eventId
?.let {
room.sendLiveLocation(

View file

@ -64,6 +64,7 @@ class LocationSharingServiceConnection @Inject constructor(
override fun onServiceDisconnected(className: ComponentName) {
isBound = false
locationSharingService = null
callback?.onLocationServiceStopped()
}
}