Bind listener to live playback flag

This commit is contained in:
Florian Renaud 2022-11-10 15:59:28 +01:00
parent 5eb260e674
commit 2d006f8725
3 changed files with 45 additions and 7 deletions

View File

@ -44,7 +44,15 @@ abstract class MessageVoiceBroadcastListeningItem : AbsMessageVoiceBroadcastItem
}
private fun bindVoiceBroadcastItem(holder: Holder) {
playerListener = VoiceBroadcastPlayer.Listener { renderPlayingState(holder, it) }
playerListener = object : VoiceBroadcastPlayer.Listener {
override fun onPlayingStateChanged(state: VoiceBroadcastPlayer.State) {
renderPlayingState(holder, state)
}
override fun onLiveModeChanged(isLive: Boolean) {
renderLiveIndicator(holder)
}
}
player.addListener(voiceBroadcast, playerListener)
bindSeekBar(holder)
bindButtons(holder)

View File

@ -78,10 +78,15 @@ interface VoiceBroadcastPlayer {
/**
* Listener related to [VoiceBroadcastPlayer].
*/
fun interface Listener {
interface Listener {
/**
* Notify about [VoiceBroadcastPlayer.playingState] changes.
*/
fun onStateChanged(state: State)
fun onPlayingStateChanged(state: State) = Unit
/**
* Notify about [VoiceBroadcastPlayer.isLiveListening] changes.
*/
fun onLiveModeChanged(isLive: Boolean) = Unit
}
}

View File

@ -121,7 +121,8 @@ class VoiceBroadcastPlayerImpl @Inject constructor(
listeners[voiceBroadcast.voiceBroadcastId]?.add(listener) ?: run {
listeners[voiceBroadcast.voiceBroadcastId] = CopyOnWriteArrayList<Listener>().apply { add(listener) }
}
listener.onStateChanged(if (voiceBroadcast == currentVoiceBroadcast) playingState else State.IDLE)
listener.onPlayingStateChanged(if (voiceBroadcast == currentVoiceBroadcast) playingState else State.IDLE)
listener.onLiveModeChanged(if (voiceBroadcast == currentVoiceBroadcast) isLiveListening else false)
}
override fun removeListener(voiceBroadcast: VoiceBroadcast, listener: Listener) {
@ -318,17 +319,41 @@ class VoiceBroadcastPlayerImpl @Inject constructor(
State.IDLE -> playbackTicker.stopPlaybackTicker(voiceBroadcastId)
}
// Notify state change to all the listeners attached to the current voice broadcast id
listeners[voiceBroadcastId]?.forEach { listener -> listener.onStateChanged(playingState) }
listeners[voiceBroadcastId]?.forEach { listener -> listener.onPlayingStateChanged(playingState) }
}
}
private fun updateLiveListeningMode(playbackPosition: Int? = null) {
/**
* Update the live listening state according to:
* - the voice broadcast state,
* - the playing state,
* - the potential seek position.
*/
private fun updateLiveListeningMode(seekPosition: Int? = null) {
isLiveListening = when {
// the current voice broadcast is not live (ended)
!currentVoiceBroadcastEvent?.isLive.orFalse() -> false
// the player is stopped or paused
playingState == State.IDLE || playingState == State.PAUSED -> false
playbackPosition != null -> playlist.findByPosition(playbackPosition)?.sequence == playlist.lastOrNull()?.sequence
// the user has sought
seekPosition != null -> {
val seekDirection = seekPosition.compareTo(getCurrentPlaybackPosition() ?: 0)
when {
// backward
seekDirection < 0 -> false
// forward: check if new sequence is the last one
else -> playlist.findByPosition(seekPosition)?.sequence == playlist.lastOrNull()?.sequence
}
}
// otherwise, stay in live or go in live if we reached the last sequence
else -> isLiveListening || playlist.currentSequence == playlist.lastOrNull()?.sequence
}
currentVoiceBroadcast?.voiceBroadcastId?.let { voiceBroadcastId ->
// Notify live mode change to all the listeners attached to the current voice broadcast id
listeners[voiceBroadcastId]?.forEach { listener -> listener.onLiveModeChanged(isLiveListening) }
}
}
private fun getCurrentPlaybackPosition(): Int? {