Skip to content

Commit ecbd2d4

Browse files
author
Maxime NATUREL
committed
Replacing callback by a SharedFlow to notify of roomIds updates
1 parent 33714b8 commit ecbd2d4

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

vector/src/main/java/im/vector/app/features/location/LocationSharingAndroidService.kt

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ import im.vector.app.features.notifications.NotificationUtils
2929
import im.vector.app.features.session.coroutineScope
3030
import kotlinx.coroutines.CoroutineScope
3131
import kotlinx.coroutines.Job
32+
import kotlinx.coroutines.flow.MutableSharedFlow
33+
import kotlinx.coroutines.flow.asSharedFlow
3234
import kotlinx.coroutines.flow.distinctUntilChangedBy
3335
import kotlinx.coroutines.flow.filter
3436
import kotlinx.coroutines.flow.launchIn
@@ -66,6 +68,9 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca
6668
private val jobs = mutableListOf<Job>()
6769
private var startInProgress = false
6870

71+
private val _roomIdsOfActiveLives = MutableSharedFlow<Set<String>>(replay = 1)
72+
val roomIdsOfActiveLives = _roomIdsOfActiveLives.asSharedFlow()
73+
6974
override fun onCreate() {
7075
super.onCreate()
7176
Timber.i("onCreate")
@@ -193,13 +198,13 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca
193198
private fun addRoomArgs(beaconEventId: String, roomArgs: RoomArgs) {
194199
Timber.i("adding roomArgs for beaconEventId: $beaconEventId")
195200
roomArgsMap[beaconEventId] = roomArgs
196-
callback?.onRoomIdsUpdate(getRoomIdsOfActiveLives())
201+
launchWithActiveSession { _roomIdsOfActiveLives.emit(getRoomIdsOfActiveLives()) }
197202
}
198203

199204
private fun removeRoomArgs(beaconEventId: String) {
200205
Timber.i("removing roomArgs for beaconEventId: $beaconEventId")
201206
roomArgsMap.remove(beaconEventId)
202-
callback?.onRoomIdsUpdate(getRoomIdsOfActiveLives())
207+
launchWithActiveSession { _roomIdsOfActiveLives.emit(getRoomIdsOfActiveLives()) }
203208
}
204209

205210
private fun listenForLiveSummaryChanges(roomId: String, beaconEventId: String) {
@@ -235,7 +240,6 @@ class LocationSharingAndroidService : VectorAndroidService(), LocationTracker.Ca
235240
}
236241

237242
interface Callback {
238-
fun onRoomIdsUpdate(roomIds: Set<String>)
239243
fun onServiceError(error: Throwable)
240244
}
241245

vector/src/main/java/im/vector/app/features/location/LocationSharingServiceConnection.kt

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,19 @@ import android.content.Context
2121
import android.content.Intent
2222
import android.content.ServiceConnection
2323
import android.os.IBinder
24+
import im.vector.app.core.di.ActiveSessionHolder
25+
import im.vector.app.features.session.coroutineScope
26+
import kotlinx.coroutines.CoroutineScope
27+
import kotlinx.coroutines.flow.launchIn
28+
import kotlinx.coroutines.flow.onEach
2429
import javax.inject.Inject
2530
import javax.inject.Singleton
2631

2732
@Singleton
2833
class LocationSharingServiceConnection @Inject constructor(
29-
private val context: Context
30-
) : ServiceConnection,
31-
LocationSharingAndroidService.Callback {
34+
private val context: Context,
35+
private val activeSessionHolder: ActiveSessionHolder
36+
) : ServiceConnection, LocationSharingAndroidService.Callback {
3237

3338
interface Callback {
3439
fun onLocationServiceRunning(roomIds: Set<String>)
@@ -61,20 +66,29 @@ class LocationSharingServiceConnection @Inject constructor(
6166
}
6267

6368
override fun onServiceConnected(className: ComponentName, binder: IBinder) {
64-
locationSharingAndroidService = (binder as LocationSharingAndroidService.LocalBinder).getService().also {
65-
it.callback = this
69+
locationSharingAndroidService = (binder as LocationSharingAndroidService.LocalBinder).getService().also { service ->
70+
service.callback = this
71+
getActiveSessionCoroutineScope()?.let { scope ->
72+
service.roomIdsOfActiveLives
73+
.onEach(::onRoomIdsUpdate)
74+
.launchIn(scope)
75+
}
6676
}
6777
isBound = true
6878
}
6979

80+
private fun getActiveSessionCoroutineScope(): CoroutineScope? {
81+
return activeSessionHolder.getSafeActiveSession()?.coroutineScope
82+
}
83+
7084
override fun onServiceDisconnected(className: ComponentName) {
7185
isBound = false
7286
locationSharingAndroidService?.callback = null
7387
locationSharingAndroidService = null
7488
onCallbackActionNoArg(Callback::onLocationServiceStopped)
7589
}
7690

77-
override fun onRoomIdsUpdate(roomIds: Set<String>) {
91+
private fun onRoomIdsUpdate(roomIds: Set<String>) {
7892
forwardRoomIdsToCallbacks(roomIds)
7993
}
8094

0 commit comments

Comments
 (0)