From 58e0ad9f096f09cce64c30577fdfcc9ef3325dbd Mon Sep 17 00:00:00 2001 From: Christian Schabesberger Date: Fri, 6 Sep 2024 15:42:19 +0200 Subject: [PATCH] make goto next or prev streem buttons work --- .../java/net/newpipe/newplayer/NewPlayer.kt | 18 +++--- .../newplayer/model/VideoPlayerUIState.kt | 52 ++++++++++++++++- .../model/VideoPlayerViewModelImpl.kt | 30 +++++++--- .../newplayer/ui/videoplayer/CenterUI.kt | 56 +++++++++++++------ .../ui/videoplayer/StreamSelectUI.kt | 46 +-------------- 5 files changed, 122 insertions(+), 80 deletions(-) diff --git a/new-player/src/main/java/net/newpipe/newplayer/NewPlayer.kt b/new-player/src/main/java/net/newpipe/newplayer/NewPlayer.kt index 379425c..356c14f 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/NewPlayer.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/NewPlayer.kt @@ -79,6 +79,7 @@ interface NewPlayer { val playlist: StateFlow> val currentlyPlaying: StateFlow + var currentlyPlayingPlaylistItem: Int val currentChapters: StateFlow> @@ -96,7 +97,6 @@ interface NewPlayer { fun removePlaylistItem(index: Int) fun playStream(item: String, playMode: PlayMode) fun selectChapter(index: Int) - fun selectPlaylistItem(index: Int) fun playStream(item: String, streamVariant: String, playMode: PlayMode) data class Builder(val app: Application, val repository: MediaRepository) { @@ -209,6 +209,15 @@ class NewPlayerImpl( private val mutableCurrentChapter = MutableStateFlow>(emptyList()) override val currentChapters: StateFlow> = mutableCurrentChapter.asStateFlow() + override var currentlyPlayingPlaylistItem: Int + get() = internalPlayer.currentMediaItemIndex + set(value) { + assert(value in 0.. - mutableUiState.update { it.copy(playList = playlist) } + mutableUiState.update { + it.copy( + playList = playlist, + ) + } } } viewModelScope.launch { newPlayer.currentlyPlaying.collect { playlistItem -> mutableUiState.update { - it.copy(currentlyPlaying = playlistItem ?: PlaylistItem.DEFAULT) + it.copy( + currentlyPlaying = playlistItem ?: PlaylistItem.DEFAULT, + currentPlaylistItemIndex = newPlayer.currentlyPlayingPlaylistItem + ) } } } @@ -261,12 +268,20 @@ class VideoPlayerViewModelImpl @Inject constructor( override fun prevStream() { resetHideUiDelayedJob() - Log.e(TAG, "imeplement prev stream") + newPlayer?.let { newPlayer -> + if (0 <= newPlayer.currentlyPlayingPlaylistItem - 1) { + newPlayer.currentlyPlayingPlaylistItem -= 1 + } + } } override fun nextStream() { resetHideUiDelayedJob() - Log.e(TAG, "implement next stream") + newPlayer?.let { newPlayer -> + if (newPlayer.currentlyPlayingPlaylistItem + 1 < newPlayer.internalPlayer.mediaItemCount) { + newPlayer.currentlyPlayingPlaylistItem += 1 + } + } } override fun showUi() { @@ -427,7 +442,7 @@ class VideoPlayerViewModelImpl @Inject constructor( if (selectChapter) uiState.value.uiMode.getChapterSelectUiState() else uiState.value.uiMode.getStreamSelectUiState() ) - if(selectChapter) { + if (selectChapter) { resetProgressUpdatePeriodicallyJob() } else { resetPlaylistProgressUpdaterJob() @@ -468,8 +483,7 @@ class VideoPlayerViewModelImpl @Inject constructor( } override fun streamSelected(streamId: Int) { - println("gurken stream selected: $streamId") - newPlayer?.selectPlaylistItem(streamId) + newPlayer?.currentlyPlayingPlaylistItem = streamId } override fun cycleRepeatMode() { diff --git a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/CenterUI.kt b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/CenterUI.kt index c9959d8..e075a82 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/CenterUI.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/CenterUI.kt @@ -21,9 +21,15 @@ package net.newpipe.newplayer.ui.videoplayer import androidx.compose.animation.AnimatedVisibility +import androidx.compose.animation.core.tween +import androidx.compose.animation.fadeIn +import androidx.compose.animation.fadeOut import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Box +import androidx.compose.foundation.layout.BoxScope import androidx.compose.foundation.layout.PaddingValues import androidx.compose.foundation.layout.Row +import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.size import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.Pause @@ -33,6 +39,7 @@ import androidx.compose.material.icons.filled.SkipPrevious import androidx.compose.material3.Button import androidx.compose.material3.ButtonDefaults import androidx.compose.material3.Icon +import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Surface import androidx.compose.runtime.Composable import androidx.compose.ui.Alignment @@ -60,13 +67,22 @@ fun CenterUI( modifier = modifier, ) { - CenterControllButton( - buttonModifier = Modifier.size(80.dp), - iconModifier = Modifier.size(40.dp), - icon = Icons.Filled.SkipPrevious, - contentDescription = stringResource(R.string.widget_description_previous_stream), - onClick = viewModel::prevStream - ) + Box(modifier = Modifier.size(80.dp)) { + androidx.compose.animation.AnimatedVisibility( + uiState.currentPlaylistItemIndex != 0, + enter = fadeIn(animationSpec = tween(200)), + exit = fadeOut(animationSpec = tween(400)) + + ) { + CenterControllButton( + buttonModifier = Modifier.fillMaxSize(), + iconModifier = Modifier.size(40.dp), + icon = Icons.Filled.SkipPrevious, + contentDescription = stringResource(R.string.widget_description_previous_stream), + onClick = viewModel::prevStream + ) + } + } CenterControllButton( buttonModifier = Modifier.size(80.dp), @@ -78,14 +94,21 @@ fun CenterUI( ), onClick = if (uiState.playing) viewModel::pause else viewModel::play ) - - CenterControllButton( - buttonModifier = Modifier.size(80.dp), - iconModifier = Modifier.size(40.dp), - icon = Icons.Filled.SkipNext, - contentDescription = stringResource(R.string.widget_description_next_stream), - onClick = viewModel::nextStream - ) + Box(modifier = Modifier.size(80.dp)) { + androidx.compose.animation.AnimatedVisibility( + uiState.currentPlaylistItemIndex < uiState.playList.size - 1, + enter = fadeIn(animationSpec = tween(200)), + exit = fadeOut(animationSpec = tween(400)) + ) { + CenterControllButton( + buttonModifier = Modifier.fillMaxSize(), + iconModifier = Modifier.size(40.dp), + icon = Icons.Filled.SkipNext, + contentDescription = stringResource(R.string.widget_description_next_stream), + onClick = viewModel::nextStream + ) + } + } } } @@ -124,7 +147,8 @@ fun VideoPlayerControllerUICenterUIPreview() { viewModel = VideoPlayerViewModelDummy(), uiState = VideoPlayerUIState.DUMMY.copy( isLoading = false, - playing = true + playing = true, + currentPlaylistItemIndex = 1 ) ) } diff --git a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/StreamSelectUI.kt b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/StreamSelectUI.kt index 9945624..7e83ba2 100644 --- a/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/StreamSelectUI.kt +++ b/new-player/src/main/java/net/newpipe/newplayer/ui/videoplayer/StreamSelectUI.kt @@ -172,25 +172,7 @@ fun VideoPlayerChannelSelectUIPreview() { StreamSelectUI( isChapterSelect = true, viewModel = VideoPlayerViewModelDummy(), - uiState = VideoPlayerUIState.DEFAULT.copy( - chapters = arrayListOf( - Chapter( - chapterStartInMs = 5000, - chapterTitle = "First Chapter", - thumbnail = null - ), - Chapter( - chapterStartInMs = 10000, - chapterTitle = "Second Chapter", - thumbnail = null - ), - Chapter( - chapterStartInMs = 20000, - chapterTitle = "Third Chapter", - thumbnail = null - ), - ) - ) + uiState = VideoPlayerUIState.DUMMY ) } } @@ -205,32 +187,6 @@ fun VideoPlayerStreamSelectUIPreview() { isChapterSelect = false, viewModel = VideoPlayerViewModelDummy(), uiState = VideoPlayerUIState.DUMMY.copy( - playList = arrayListOf( - PlaylistItem( - id = "6502", - title = "Stream 1", - creator = "The Creator", - lengthInS = 6 * 60 + 5, - thumbnail = null, - uniqueId = 0 - ), - PlaylistItem( - id = "6502", - title = "Stream 2", - creator = "The Creator 2", - lengthInS = 2 * 60 + 5, - thumbnail = null, - uniqueId = 1 - ), - PlaylistItem( - id = "6502", - title = "Stream 3", - creator = "The Creator 3", - lengthInS = 29 * 60 + 5, - thumbnail = null, - uniqueId = 2 - ) - ), currentlyPlaying = PlaylistItem.DUMMY.copy(uniqueId = 1) ) )