Fix coroutine scope (#4820)

* Inject the session scope instead of the application scope where it's possible.

* Create AppCoroutineScope annotation to let developers explicitly choose the appropriate CoroutineScope when injecting one.
This commit is contained in:
Benoit Marty 2025-06-04 17:33:51 +02:00 committed by GitHub
parent 36c7c7ab9b
commit 5f191d9f9c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
58 changed files with 172 additions and 72 deletions

View file

@ -15,6 +15,7 @@ import io.element.android.libraries.audio.api.AudioFocus
import io.element.android.libraries.audio.api.AudioFocusRequester
import io.element.android.libraries.di.RoomScope
import io.element.android.libraries.di.SingleIn
import io.element.android.libraries.di.annotations.SessionCoroutineScope
import io.element.android.libraries.mediaplayer.api.MediaPlayer
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.FlowPreview
@ -37,7 +38,8 @@ import kotlin.time.Duration.Companion.seconds
@SingleIn(RoomScope::class)
class DefaultMediaPlayer @Inject constructor(
private val player: SimplePlayer,
private val coroutineScope: CoroutineScope,
@SessionCoroutineScope
private val sessionCoroutineScope: CoroutineScope,
private val audioFocus: AudioFocus,
) : MediaPlayer {
private val listener = object : SimplePlayer.Listener {
@ -50,7 +52,7 @@ class DefaultMediaPlayer @Inject constructor(
)
}
if (isPlaying) {
job = coroutineScope.launch { updateCurrentPosition() }
job = sessionCoroutineScope.launch { updateCurrentPosition() }
} else {
audioFocus.releaseAudioFocus()
job?.cancel()

View file

@ -423,7 +423,7 @@ class DefaultMediaPlayerTest {
audioFocus: AudioFocus = FakeAudioFocus(),
): DefaultMediaPlayer = DefaultMediaPlayer(
player = simplePlayer,
coroutineScope = backgroundScope,
sessionCoroutineScope = backgroundScope,
audioFocus = audioFocus,
)
}