mirror of
https://github.com/TeamNewPipe/NewPipe
synced 2025-01-24 07:56:57 +00:00
Added loading indicator to playlist view
This commit is contained in:
parent
37e4064533
commit
462ed5c79a
@ -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()) {
|
||||||
val playlistInfo by playlistViewModel.playlistInfo.collectAsState()
|
Surface(color = MaterialTheme.colorScheme.background) {
|
||||||
val streams = playlistViewModel.streamItems.collectAsLazyPagingItems()
|
val playlistInfo by playlistViewModel.playlistInfo.collectAsState()
|
||||||
|
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,42 +70,36 @@ fun Playlist(playlistViewModel: PlaylistViewModel = viewModel()) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
playlistInfo?.let {
|
if (mode == ItemViewMode.GRID) {
|
||||||
Surface(color = MaterialTheme.colorScheme.background) {
|
val gridState = rememberLazyGridState()
|
||||||
val mode = determineItemViewMode()
|
|
||||||
|
|
||||||
if (mode == ItemViewMode.GRID) {
|
LazyVerticalGridScrollbar(state = gridState) {
|
||||||
val gridState = rememberLazyGridState()
|
LazyVerticalGrid(state = gridState, columns = GridCells.Adaptive(250.dp)) {
|
||||||
|
item(span = { GridItemSpan(maxLineSpan) }) {
|
||||||
LazyVerticalGridScrollbar(state = gridState) {
|
PlaylistHeader(playlistInfo, totalDuration)
|
||||||
LazyVerticalGrid(state = gridState, columns = GridCells.Adaptive(250.dp)) {
|
|
||||||
item(span = { GridItemSpan(maxLineSpan) }) {
|
|
||||||
PlaylistHeader(playlistInfo = it, totalDuration = totalDuration)
|
|
||||||
}
|
|
||||||
|
|
||||||
items(streams.itemCount) {
|
|
||||||
StreamGridItem(streams[it]!!, onClick)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// Card or list views
|
|
||||||
val listState = rememberLazyListState()
|
|
||||||
|
|
||||||
LazyColumnScrollbar(state = listState) {
|
items(streams.itemCount) {
|
||||||
LazyColumn(state = listState) {
|
StreamGridItem(streams[it]!!, onClick)
|
||||||
item {
|
}
|
||||||
PlaylistHeader(playlistInfo = it, totalDuration = totalDuration)
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// Card or list views
|
||||||
|
val listState = rememberLazyListState()
|
||||||
|
|
||||||
items(streams.itemCount) {
|
LazyColumnScrollbar(state = listState) {
|
||||||
val stream = streams[it]!!
|
LazyColumn(state = listState) {
|
||||||
if (mode == ItemViewMode.CARD) {
|
item {
|
||||||
StreamCardItem(stream, onClick)
|
PlaylistHeader(playlistInfo, totalDuration)
|
||||||
} else {
|
}
|
||||||
StreamListItem(stream, onClick)
|
|
||||||
}
|
items(streams.itemCount) {
|
||||||
}
|
val stream = streams[it]!!
|
||||||
|
if (mode == ItemViewMode.CARD) {
|
||||||
|
StreamCardItem(stream, onClick)
|
||||||
|
} else {
|
||||||
|
StreamListItem(stream, onClick)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -104,13 +112,18 @@ fun Playlist(playlistViewModel: PlaylistViewModel = viewModel()) {
|
|||||||
@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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user