1
0
mirror of https://github.com/TeamNewPipe/NewPipe synced 2025-01-24 16:07:04 +00:00

Added loading indicator to playlist view

This commit is contained in:
Isira Seneviratne 2024-07-06 21:04:48 +05:30
parent 37e4064533
commit 462ed5c79a

View File

@ -18,12 +18,16 @@ import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp import androidx.compose.ui.unit.dp
import androidx.fragment.app.FragmentActivity import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.viewmodel.compose.viewModel import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import androidx.paging.compose.collectAsLazyPagingItems import androidx.paging.compose.collectAsLazyPagingItems
import kotlinx.coroutines.flow.Flow
import my.nanihadesuka.compose.LazyColumnScrollbar import my.nanihadesuka.compose.LazyColumnScrollbar
import my.nanihadesuka.compose.LazyVerticalGridScrollbar import my.nanihadesuka.compose.LazyVerticalGridScrollbar
import org.schabi.newpipe.DownloaderImpl import org.schabi.newpipe.DownloaderImpl
import org.schabi.newpipe.compose.status.LoadingIndicator
import org.schabi.newpipe.compose.stream.StreamCardItem import org.schabi.newpipe.compose.stream.StreamCardItem
import org.schabi.newpipe.compose.stream.StreamGridItem import org.schabi.newpipe.compose.stream.StreamGridItem
import org.schabi.newpipe.compose.stream.StreamListItem import org.schabi.newpipe.compose.stream.StreamListItem
@ -31,24 +35,34 @@ import org.schabi.newpipe.compose.theme.AppTheme
import org.schabi.newpipe.compose.util.determineItemViewMode import org.schabi.newpipe.compose.util.determineItemViewMode
import org.schabi.newpipe.extractor.NewPipe import org.schabi.newpipe.extractor.NewPipe
import org.schabi.newpipe.extractor.ServiceList import org.schabi.newpipe.extractor.ServiceList
import org.schabi.newpipe.extractor.playlist.PlaylistInfo
import org.schabi.newpipe.extractor.stream.StreamInfoItem import org.schabi.newpipe.extractor.stream.StreamInfoItem
import org.schabi.newpipe.info_list.ItemViewMode import org.schabi.newpipe.info_list.ItemViewMode
import org.schabi.newpipe.util.KEY_SERVICE_ID import org.schabi.newpipe.paging.PlaylistItemsSource
import org.schabi.newpipe.util.KEY_URL
import org.schabi.newpipe.util.NavigationHelper import org.schabi.newpipe.util.NavigationHelper
import org.schabi.newpipe.viewmodels.PlaylistViewModel import org.schabi.newpipe.viewmodels.PlaylistViewModel
@Composable @Composable
fun Playlist(playlistViewModel: PlaylistViewModel = viewModel()) { fun Playlist(playlistViewModel: PlaylistViewModel = viewModel()) {
Surface(color = MaterialTheme.colorScheme.background) {
val playlistInfo by playlistViewModel.playlistInfo.collectAsState() val playlistInfo by playlistViewModel.playlistInfo.collectAsState()
val streams = playlistViewModel.streamItems.collectAsLazyPagingItems() playlistInfo?.let {
LoadedPlaylist(it, playlistViewModel.streamItems)
} ?: LoadingIndicator()
}
}
@Composable
private fun LoadedPlaylist(playlistInfo: PlaylistInfo, flow: Flow<PagingData<StreamInfoItem>>) {
val streams = flow.collectAsLazyPagingItems()
val mode = determineItemViewMode()
val context = LocalContext.current
val totalDuration by remember { val totalDuration by remember {
derivedStateOf { derivedStateOf {
streams.itemSnapshotList.sumOf { it!!.duration } streams.itemSnapshotList.sumOf { it!!.duration }
} }
} }
val context = LocalContext.current
val onClick = { stream: StreamInfoItem -> val onClick = { stream: StreamInfoItem ->
NavigationHelper.openVideoDetailFragment( NavigationHelper.openVideoDetailFragment(
context, (context as FragmentActivity).supportFragmentManager, context, (context as FragmentActivity).supportFragmentManager,
@ -56,17 +70,13 @@ fun Playlist(playlistViewModel: PlaylistViewModel = viewModel()) {
) )
} }
playlistInfo?.let {
Surface(color = MaterialTheme.colorScheme.background) {
val mode = determineItemViewMode()
if (mode == ItemViewMode.GRID) { if (mode == ItemViewMode.GRID) {
val gridState = rememberLazyGridState() val gridState = rememberLazyGridState()
LazyVerticalGridScrollbar(state = gridState) { LazyVerticalGridScrollbar(state = gridState) {
LazyVerticalGrid(state = gridState, columns = GridCells.Adaptive(250.dp)) { LazyVerticalGrid(state = gridState, columns = GridCells.Adaptive(250.dp)) {
item(span = { GridItemSpan(maxLineSpan) }) { item(span = { GridItemSpan(maxLineSpan) }) {
PlaylistHeader(playlistInfo = it, totalDuration = totalDuration) PlaylistHeader(playlistInfo, totalDuration)
} }
items(streams.itemCount) { items(streams.itemCount) {
@ -81,7 +91,7 @@ fun Playlist(playlistViewModel: PlaylistViewModel = viewModel()) {
LazyColumnScrollbar(state = listState) { LazyColumnScrollbar(state = listState) {
LazyColumn(state = listState) { LazyColumn(state = listState) {
item { item {
PlaylistHeader(playlistInfo = it, totalDuration = totalDuration) PlaylistHeader(playlistInfo, totalDuration)
} }
items(streams.itemCount) { items(streams.itemCount) {
@ -96,21 +106,24 @@ fun Playlist(playlistViewModel: PlaylistViewModel = viewModel()) {
} }
} }
} }
}
}
@Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO) @Preview(name = "Light mode", uiMode = Configuration.UI_MODE_NIGHT_NO)
@Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES) @Preview(name = "Dark mode", uiMode = Configuration.UI_MODE_NIGHT_YES)
@Composable @Composable
private fun PlaylistPreview() { private fun PlaylistPreview() {
NewPipe.init(DownloaderImpl.init(null)) NewPipe.init(DownloaderImpl.init(null))
val params =
mapOf( val playlistInfo = PlaylistInfo.getInfo(
KEY_SERVICE_ID to ServiceList.YouTube.serviceId, ServiceList.YouTube,
KEY_URL to "https://www.youtube.com/playlist?list=PLAIcZs9N4171hRrG_4v32Ca2hLvSuQ6QI", "https://www.youtube.com/playlist?list=PLAIcZs9N4171hRrG_4v32Ca2hLvSuQ6QI"
) )
val streams = Pager(PagingConfig(pageSize = 20, enablePlaceholders = false)) {
PlaylistItemsSource(playlistInfo)
}.flow
AppTheme { AppTheme {
Playlist(PlaylistViewModel(SavedStateHandle(params))) Surface(color = MaterialTheme.colorScheme.background) {
LoadedPlaylist(playlistInfo, streams)
}
} }
} }