From ecbd2d48a71cc0e6d91a1ba052cde915ad9aaec4 Mon Sep 17 00:00:00 2001 From: Maxime NATUREL Date: Tue, 19 Jul 2022 14:43:13 +0200 Subject: [PATCH] Replacing callback by a SharedFlow to notify of roomIds updates --- .../location/LocationSharingAndroidService.kt | 10 ++++--- .../LocationSharingServiceConnection.kt | 26 ++++++++++++++----- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingAndroidService.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingAndroidService.kt index 6fd7f27ece..f3be651ea3 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingAndroidService.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingAndroidService.kt @@ -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() private var startInProgress = false + private val _roomIdsOfActiveLives = MutableSharedFlow>(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) fun onServiceError(error: Throwable) } diff --git a/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt b/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt index 9feffb7554..3be73e9fd4 100644 --- a/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt +++ b/vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt @@ -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) @@ -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) { + private fun onRoomIdsUpdate(roomIds: Set) { forwardRoomIdsToCallbacks(roomIds) }