get chapter infor from NewPlayer

This commit is contained in:
Christian Schabesberger 2024-09-04 14:51:50 +02:00
parent 9f1c06928a
commit a2e9675906
4 changed files with 32 additions and 7 deletions

View File

@ -37,6 +37,7 @@ import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.collect
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import net.newpipe.newplayer.playerInternals.PlaylistItem
@ -79,6 +80,8 @@ interface NewPlayer {
val playlist: StateFlow<List<PlaylistItem>>
val currentlyPlaying: StateFlow<PlaylistItem?>
val currentChapters: StateFlow<List<Chapter>>
// callbacks
val errorFlow: SharedFlow<Exception>
@ -201,6 +204,9 @@ class NewPlayerImpl(
private val mutableCurrentlyPlaying = MutableStateFlow<PlaylistItem?>(null)
override val currentlyPlaying: StateFlow<PlaylistItem?> = mutableCurrentlyPlaying.asStateFlow()
private val mutableCurrentChapter = MutableStateFlow<List<Chapter>>(emptyList())
override val currentChapters: StateFlow<List<Chapter>> = mutableCurrentChapter.asStateFlow()
init {
println("gurken init")
internalPlayer.addListener(object : Player.Listener {
@ -246,12 +252,22 @@ class NewPlayerImpl(
mutableCurrentlyPlaying.update { item }
}
}
}
}
})
playerScope.launch {
currentlyPlaying.collect { playing ->
playing?.let {
try {
val chapters = repository.getChapters(playing.id)
mutableCurrentChapter.update { chapters }
} catch (e: Exception) {
mutableErrorFlow.emit(e)
}
}
}
}
}
private fun updatePlaylistItems() {
@ -363,4 +379,5 @@ class NewPlayerImpl(
mutableErrorFlow.emit(e)
}
}
}

View File

@ -30,6 +30,8 @@ import androidx.core.content.ContextCompat.getSystemService
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewModelScope
import androidx.media3.common.MediaItem
import androidx.media3.common.MediaMetadata
import androidx.media3.common.Player
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
@ -154,10 +156,9 @@ class VideoPlayerViewModelImpl @Inject constructor(
}
// TODO: This is not correctly applicable for loading indicator
override fun onIsLoadingChanged(isLoading: Boolean) {
super.onIsLoadingChanged(isLoading)
if(!player.isPlaying) {
if (!player.isPlaying) {
mutableUiState.update {
it.copy(isLoading = isLoading)
}
@ -205,6 +206,13 @@ class VideoPlayerViewModelImpl @Inject constructor(
}
}
}
viewModelScope.launch {
newPlayer.currentChapters.collect { chapters ->
mutableUiState.update {
it.copy(chapters = chapters)
}
}
}
}
}
@ -477,7 +485,7 @@ class VideoPlayerViewModelImpl @Inject constructor(
}
override fun dialogVisible(visible: Boolean) {
if(visible) {
if (visible) {
uiVisibilityJob?.cancel()
} else {
resetHideUiDelayedJob()

View File

@ -91,7 +91,6 @@ fun StreamSelectUI(
.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(5.dp),
) {
items(uiState.chapters.size) { chapterIndex ->
val chapter = uiState.chapters[chapterIndex]
ChapterItem(

View File

@ -25,6 +25,7 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding