@@ -18,80 +18,88 @@ import androidx.compose.ui.platform.LocalContext
1818import androidx.compose.ui.tooling.preview.Preview
1919import androidx.compose.ui.unit.dp
2020import androidx.fragment.app.FragmentActivity
21- import androidx.lifecycle.SavedStateHandle
2221import androidx.lifecycle.viewmodel.compose.viewModel
22+ import androidx.paging.Pager
23+ import androidx.paging.PagingConfig
24+ import androidx.paging.PagingData
2325import androidx.paging.compose.collectAsLazyPagingItems
26+ import kotlinx.coroutines.flow.Flow
2427import my.nanihadesuka.compose.LazyColumnScrollbar
2528import my.nanihadesuka.compose.LazyVerticalGridScrollbar
2629import org.schabi.newpipe.DownloaderImpl
30+ import org.schabi.newpipe.compose.status.LoadingIndicator
2731import org.schabi.newpipe.compose.stream.StreamCardItem
2832import org.schabi.newpipe.compose.stream.StreamGridItem
2933import org.schabi.newpipe.compose.stream.StreamListItem
3034import org.schabi.newpipe.compose.theme.AppTheme
3135import org.schabi.newpipe.compose.util.determineItemViewMode
3236import org.schabi.newpipe.extractor.NewPipe
3337import org.schabi.newpipe.extractor.ServiceList
38+ import org.schabi.newpipe.extractor.playlist.PlaylistInfo
3439import org.schabi.newpipe.extractor.stream.StreamInfoItem
3540import org.schabi.newpipe.info_list.ItemViewMode
36- import org.schabi.newpipe.util.KEY_SERVICE_ID
37- import org.schabi.newpipe.util.KEY_URL
41+ import org.schabi.newpipe.paging.PlaylistItemsSource
3842import org.schabi.newpipe.util.NavigationHelper
3943import org.schabi.newpipe.viewmodels.PlaylistViewModel
4044
4145@Composable
4246fun Playlist (playlistViewModel : PlaylistViewModel = viewModel()) {
43- val playlistInfo by playlistViewModel.playlistInfo.collectAsState()
44- val streams = playlistViewModel.streamItems.collectAsLazyPagingItems()
47+ Surface (color = MaterialTheme .colorScheme.background) {
48+ val playlistInfo by playlistViewModel.playlistInfo.collectAsState()
49+ playlistInfo?.let {
50+ LoadedPlaylist (it, playlistViewModel.streamItems)
51+ } ? : LoadingIndicator ()
52+ }
53+ }
54+
55+ @Composable
56+ private fun LoadedPlaylist (playlistInfo : PlaylistInfo , flow : Flow <PagingData <StreamInfoItem >>) {
57+ val streams = flow.collectAsLazyPagingItems()
58+ val mode = determineItemViewMode()
59+ val context = LocalContext .current
60+
4561 val totalDuration by remember {
4662 derivedStateOf {
4763 streams.itemSnapshotList.sumOf { it!! .duration }
4864 }
4965 }
50-
51- val context = LocalContext .current
5266 val onClick = { stream: StreamInfoItem ->
5367 NavigationHelper .openVideoDetailFragment(
5468 context, (context as FragmentActivity ).supportFragmentManager,
5569 stream.serviceId, stream.url, stream.name, null , false
5670 )
5771 }
5872
59- playlistInfo?.let {
60- Surface (color = MaterialTheme .colorScheme.background) {
61- val mode = determineItemViewMode()
62-
63- if (mode == ItemViewMode .GRID ) {
64- val gridState = rememberLazyGridState()
73+ if (mode == ItemViewMode .GRID ) {
74+ val gridState = rememberLazyGridState()
6575
66- LazyVerticalGridScrollbar (state = gridState) {
67- LazyVerticalGrid (state = gridState, columns = GridCells .Adaptive (250 .dp)) {
68- item(span = { GridItemSpan (maxLineSpan) }) {
69- PlaylistHeader (playlistInfo = it, totalDuration = totalDuration)
70- }
76+ LazyVerticalGridScrollbar (state = gridState) {
77+ LazyVerticalGrid (state = gridState, columns = GridCells .Adaptive (250 .dp)) {
78+ item(span = { GridItemSpan (maxLineSpan) }) {
79+ PlaylistHeader (playlistInfo, totalDuration)
80+ }
7181
72- items(streams.itemCount) {
73- StreamGridItem (streams[it]!! , onClick)
74- }
75- }
82+ items(streams.itemCount) {
83+ StreamGridItem (streams[it]!! , onClick)
7684 }
77- } else {
78- // Card or list views
79- val listState = rememberLazyListState()
85+ }
86+ }
87+ } else {
88+ // Card or list views
89+ val listState = rememberLazyListState()
8090
81- LazyColumnScrollbar (state = listState) {
82- LazyColumn (state = listState) {
83- item {
84- PlaylistHeader (playlistInfo = it, totalDuration = totalDuration)
85- }
91+ LazyColumnScrollbar (state = listState) {
92+ LazyColumn (state = listState) {
93+ item {
94+ PlaylistHeader (playlistInfo, totalDuration)
95+ }
8696
87- items(streams.itemCount) {
88- val stream = streams[it]!!
89- if (mode == ItemViewMode .CARD ) {
90- StreamCardItem (stream, onClick)
91- } else {
92- StreamListItem (stream, onClick)
93- }
94- }
97+ items(streams.itemCount) {
98+ val stream = streams[it]!!
99+ if (mode == ItemViewMode .CARD ) {
100+ StreamCardItem (stream, onClick)
101+ } else {
102+ StreamListItem (stream, onClick)
95103 }
96104 }
97105 }
@@ -104,13 +112,18 @@ fun Playlist(playlistViewModel: PlaylistViewModel = viewModel()) {
104112@Composable
105113private fun PlaylistPreview () {
106114 NewPipe .init (DownloaderImpl .init (null ))
107- val params =
108- mapOf (
109- KEY_SERVICE_ID to ServiceList .YouTube .serviceId,
110- KEY_URL to " https://www.youtube.com/playlist?list=PLAIcZs9N4171hRrG_4v32Ca2hLvSuQ6QI" ,
111- )
115+
116+ val playlistInfo = PlaylistInfo .getInfo(
117+ ServiceList .YouTube ,
118+ " https://www.youtube.com/playlist?list=PLAIcZs9N4171hRrG_4v32Ca2hLvSuQ6QI"
119+ )
120+ val streams = Pager (PagingConfig (pageSize = 20 , enablePlaceholders = false )) {
121+ PlaylistItemsSource (playlistInfo)
122+ }.flow
112123
113124 AppTheme {
114- Playlist (PlaylistViewModel (SavedStateHandle (params)))
125+ Surface (color = MaterialTheme .colorScheme.background) {
126+ LoadedPlaylist (playlistInfo, streams)
127+ }
115128 }
116129}
0 commit comments