@@ -10,62 +10,72 @@ import androidx.compose.runtime.derivedStateOf
10
10
import androidx.compose.runtime.getValue
11
11
import androidx.compose.runtime.remember
12
12
import androidx.compose.ui.tooling.preview.Preview
13
- import androidx.lifecycle.SavedStateHandle
14
13
import androidx.lifecycle.viewmodel.compose.viewModel
14
+ import androidx.paging.PagingData
15
15
import androidx.paging.compose.collectAsLazyPagingItems
16
- import org.schabi.newpipe.DownloaderImpl
17
- import org.schabi.newpipe.compose.status.LoadingIndicator
16
+ import kotlinx.coroutines.flow.Flow
17
+ import kotlinx.coroutines.flow.flowOf
18
+ import org.schabi.newpipe.compose.common.LoadingIndicator
19
+ import org.schabi.newpipe.compose.stream.StreamInfoItem
18
20
import org.schabi.newpipe.compose.stream.StreamList
19
21
import org.schabi.newpipe.compose.theme.AppTheme
20
- import org.schabi.newpipe.extractor.NewPipe
21
- import org.schabi.newpipe.extractor.ServiceList
22
- import org.schabi.newpipe.util.KEY_SERVICE_ID
23
- import org.schabi.newpipe.util.KEY_URL
22
+ import org.schabi.newpipe.extractor.stream.Description
23
+ import org.schabi.newpipe.extractor.stream.StreamInfoItem
24
+ import org.schabi.newpipe.extractor.stream.StreamType
24
25
import org.schabi.newpipe.viewmodels.PlaylistViewModel
25
26
26
27
@Composable
27
28
fun Playlist (playlistViewModel : PlaylistViewModel = viewModel()) {
28
29
Surface (color = MaterialTheme .colorScheme.background) {
29
30
val playlistInfo by playlistViewModel.playlistInfo.collectAsState()
31
+ Playlist (playlistInfo, playlistViewModel.streamItems)
32
+ }
33
+ }
30
34
31
- playlistInfo?.let {
32
- val streams = playlistViewModel.streamItems.collectAsLazyPagingItems()
33
- val totalDuration by remember {
34
- derivedStateOf {
35
- streams.itemSnapshotList.sumOf { it!! .duration }
36
- }
35
+ @Composable
36
+ private fun Playlist (
37
+ playlistInfo : PlaylistInfo ? ,
38
+ streamFlow : Flow <PagingData <StreamInfoItem >>
39
+ ) {
40
+ playlistInfo?.let {
41
+ val streams = streamFlow.collectAsLazyPagingItems()
42
+ val totalDuration by remember {
43
+ derivedStateOf {
44
+ streams.itemSnapshotList.sumOf { it!! .duration }
37
45
}
46
+ }
38
47
39
- StreamList (
40
- streams = streams,
41
- gridHeader = {
42
- item(span = { GridItemSpan (maxLineSpan) }) {
43
- PlaylistHeader (it, totalDuration)
44
- }
45
- },
46
- listHeader = {
47
- item {
48
- PlaylistHeader (it, totalDuration)
49
- }
48
+ StreamList (
49
+ streams = streams,
50
+ gridHeader = {
51
+ item(span = { GridItemSpan (maxLineSpan) }) {
52
+ PlaylistHeader (it, totalDuration)
50
53
}
51
- )
52
- } ? : LoadingIndicator ()
53
- }
54
+ },
55
+ listHeader = {
56
+ item {
57
+ PlaylistHeader (it, totalDuration)
58
+ }
59
+ }
60
+ )
61
+ } ? : LoadingIndicator ()
54
62
}
55
63
56
64
@Preview(name = " Light mode" , uiMode = Configuration .UI_MODE_NIGHT_NO )
57
65
@Preview(name = " Dark mode" , uiMode = Configuration .UI_MODE_NIGHT_YES )
58
66
@Composable
59
67
private fun PlaylistPreview () {
60
- NewPipe .init (DownloaderImpl .init (null ))
61
-
62
- val params = mapOf (
63
- KEY_SERVICE_ID to ServiceList .YouTube .serviceId,
64
- KEY_URL to " https://www.youtube.com/playlist?list=PLAIcZs9N4171hRrG_4v32Ca2hLvSuQ6QI"
68
+ val description = Description (" Example description" , Description .PLAIN_TEXT )
69
+ val playlistInfo = PlaylistInfo (
70
+ " " , 1 , " " , " Example playlist" , description, listOf (), 1L ,
71
+ null , " Uploader" , listOf (), null
65
72
)
73
+ val stream = StreamInfoItem (streamType = StreamType .VIDEO_STREAM )
74
+ val streamFlow = flowOf(PagingData .from(listOf (stream)))
75
+
66
76
AppTheme {
67
77
Surface (color = MaterialTheme .colorScheme.background) {
68
- Playlist (PlaylistViewModel ( SavedStateHandle (params)) )
78
+ Playlist (playlistInfo, streamFlow )
69
79
}
70
80
}
71
81
}
0 commit comments