Replacing callback by a SharedFlow to notify of roomIds updates

This commit is contained in:
Maxime NATUREL 2022-07-19 14:43:13 +02:00
parent 33714b850f
commit ecbd2d48a7
2 changed files with 27 additions and 9 deletions

View File

@ -29,6 +29,8 @@ import im.vector.app.features.notifications.NotificationUtils
import im.vector.app.features.session.coroutineScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.distinctUntilChangedBy
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.launchIn
@ -66,6 +68,9 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca
private val jobs = mutableListOf<Job>()
private var startInProgress = false
private val _roomIdsOfActiveLives = MutableSharedFlow<Set<String>>(replay = 1)
val roomIdsOfActiveLives = _roomIdsOfActiveLives.asSharedFlow()
override fun onCreate() {
super.onCreate()
Timber.i("onCreate")
@ -193,13 +198,13 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca
private fun addRoomArgs(beaconEventId: String, roomArgs: RoomArgs) {
Timber.i("adding roomArgs for beaconEventId: $beaconEventId")
roomArgsMap[beaconEventId] = roomArgs
callback?.onRoomIdsUpdate(getRoomIdsOfActiveLives())
launchWithActiveSession { _roomIdsOfActiveLives.emit(getRoomIdsOfActiveLives()) }
}
private fun removeRoomArgs(beaconEventId: String) {
Timber.i("removing roomArgs for beaconEventId: $beaconEventId")
roomArgsMap.remove(beaconEventId)
callback?.onRoomIdsUpdate(getRoomIdsOfActiveLives())
launchWithActiveSession { _roomIdsOfActiveLives.emit(getRoomIdsOfActiveLives()) }
}
private fun listenForLiveSummaryChanges(roomId: String, beaconEventId: String) {
@ -235,7 +240,6 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca
}
interface Callback {
fun onRoomIdsUpdate(roomIds: Set<String>)
fun onServiceError(error: Throwable)
}

View File

@ -21,14 +21,19 @@ import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import im.vector.app.core.di.ActiveSessionHolder
import im.vector.app.features.session.coroutineScope
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class LocationSharingServiceConnection @Inject constructor(
private val context: Context
) : ServiceConnection,
LocationSharingAndroidService.Callback {
private val context: Context,
private val activeSessionHolder: ActiveSessionHolder
) : ServiceConnection, LocationSharingAndroidService.Callback {
interface Callback {
fun onLocationServiceRunning(roomIds: Set<String>)
@ -61,12 +66,21 @@ class LocationSharingServiceConnection @Inject constructor(
}
override fun onServiceConnected(className: ComponentName, binder: IBinder) {
locationSharingAndroidService = (binder as LocationSharingAndroidService.LocalBinder).getService().also {
it.callback = this
locationSharingAndroidService = (binder as LocationSharingAndroidService.LocalBinder).getService().also { service ->
service.callback = this
getActiveSessionCoroutineScope()?.let { scope ->
service.roomIdsOfActiveLives
.onEach(::onRoomIdsUpdate)
.launchIn(scope)
}
}
isBound = true
}
private fun getActiveSessionCoroutineScope(): CoroutineScope? {
return activeSessionHolder.getSafeActiveSession()?.coroutineScope
}
override fun onServiceDisconnected(className: ComponentName) {
isBound = false
locationSharingAndroidService?.callback = null
@ -74,7 +88,7 @@ class LocationSharingServiceConnection @Inject constructor(
onCallbackActionNoArg(Callback::onLocationServiceStopped)
}
override fun onRoomIdsUpdate(roomIds: Set<String>) {
private fun onRoomIdsUpdate(roomIds: Set<String>) {
forwardRoomIdsToCallbacks(roomIds)
}