@@ -5,11 +5,11 @@ import androidx.compose.foundation.lazy.grid.GridItemSpan
55import androidx.compose.material3.MaterialTheme
66import androidx.compose.material3.Surface
77import androidx.compose.runtime.Composable
8- import androidx.compose.runtime.collectAsState
98import androidx.compose.runtime.derivedStateOf
109import androidx.compose.runtime.getValue
1110import androidx.compose.runtime.remember
1211import androidx.compose.ui.tooling.preview.Preview
12+ import androidx.lifecycle.compose.collectAsStateWithLifecycle
1313import androidx.lifecycle.viewmodel.compose.viewModel
1414import androidx.paging.PagingData
1515import androidx.paging.compose.collectAsLazyPagingItems
@@ -23,48 +23,67 @@ import org.schabi.newpipe.ui.components.items.ItemList
2323import org.schabi.newpipe.ui.components.items.stream.StreamInfoItem
2424import org.schabi.newpipe.ui.components.playlist.PlaylistHeader
2525import org.schabi.newpipe.ui.components.playlist.PlaylistInfo
26+ import org.schabi.newpipe.ui.emptystate.EmptyStateComposable
27+ import org.schabi.newpipe.ui.emptystate.EmptyStateSpec
2628import org.schabi.newpipe.ui.theme.AppTheme
2729import org.schabi.newpipe.viewmodels.PlaylistViewModel
30+ import org.schabi.newpipe.viewmodels.util.Resource
2831
2932@Composable
3033fun PlaylistScreen (playlistViewModel : PlaylistViewModel = viewModel()) {
3134 Surface (color = MaterialTheme .colorScheme.background) {
32- val playlistInfo by playlistViewModel.playlistInfo.collectAsState ()
33- PlaylistScreen (playlistInfo , playlistViewModel.streamItems)
35+ val uiState by playlistViewModel.uiState.collectAsStateWithLifecycle ()
36+ PlaylistScreen (uiState , playlistViewModel.streamItems)
3437 }
3538}
3639
3740@Composable
3841private fun PlaylistScreen (
39- playlistInfo : PlaylistInfo ? ,
42+ uiState : Resource < PlaylistInfo > ,
4043 streamFlow : Flow <PagingData <StreamInfoItem >>
4144) {
42- playlistInfo?.let {
43- val streams = streamFlow.collectAsLazyPagingItems()
45+ when (uiState) {
46+ is Resource .Success -> {
47+ val info = uiState.data
48+ val streams = streamFlow.collectAsLazyPagingItems()
4449
45- // Paging's load states only indicate when loading is currently happening, not if it can/will
46- // happen. As such, the duration initially displayed will be the incomplete duration if more
47- // items can be loaded.
48- val totalDuration by remember {
49- derivedStateOf {
50- streams.itemSnapshotList.sumOf { it!! .duration }
50+ // Paging's load states only indicate when loading is currently happening, not if it can/will
51+ // happen. As such, the duration initially displayed will be the incomplete duration if more
52+ // items can be loaded.
53+ val totalDuration by remember {
54+ derivedStateOf {
55+ streams.itemSnapshotList.sumOf { it!! .duration }
56+ }
5157 }
52- }
5358
54- ItemList (
55- items = streams,
56- gridHeader = {
57- item(span = { GridItemSpan (maxLineSpan) }) {
58- PlaylistHeader (it, totalDuration)
59- }
60- },
61- listHeader = {
62- item {
63- PlaylistHeader (it, totalDuration)
59+ ItemList (
60+ items = streams,
61+ gridHeader = {
62+ item(span = { GridItemSpan (maxLineSpan) }) {
63+ PlaylistHeader (info, totalDuration)
64+ }
65+ },
66+ listHeader = {
67+ item {
68+ PlaylistHeader (info, totalDuration)
69+ }
6470 }
65- }
66- )
67- } ? : LoadingIndicator ()
71+ )
72+ }
73+
74+ is Resource .Loading -> {
75+ LoadingIndicator ()
76+ }
77+
78+ is Resource .Error -> {
79+ // TODO use error panel instead
80+ EmptyStateComposable (
81+ EmptyStateSpec .DisabledComments .copy(
82+ descriptionText = { " Could not load streams" }
83+ )
84+ )
85+ }
86+ }
6887}
6988
7089@Preview(name = " Light mode" , uiMode = Configuration .UI_MODE_NIGHT_NO )
@@ -81,7 +100,7 @@ private fun PlaylistPreview() {
81100
82101 AppTheme {
83102 Surface (color = MaterialTheme .colorScheme.background) {
84- PlaylistScreen (playlistInfo, streamFlow)
103+ PlaylistScreen (Resource . Success ( playlistInfo) , streamFlow)
85104 }
86105 }
87106}
0 commit comments