Skip to content

Commit

Permalink
Merge pull request #73 from CodandoTV/feature/72-dados-de-paginao-est…
Browse files Browse the repository at this point in the history
…o-sendo-perdidos-

[ISSUE-72] - Mantendo paginas já carregadas após recomposição
  • Loading branch information
gabrielbmoro committed Aug 14, 2023
2 parents 8b773f4 + 2c95dbe commit 1f0c969
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 48 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package com.codandotv.streamplayerapp.feature_list_streams.list.presentation.screens

import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.PagingData
import androidx.paging.cachedIn
import androidx.paging.map
import com.codandotv.streamplayerapp.core_networking.handleError.catchFailure
import com.codandotv.streamplayerapp.feature.list.streams.R
Expand All @@ -17,7 +16,7 @@ import com.codandotv.streamplayerapp.feature_list_streams.list.domain.model.High
import com.codandotv.streamplayerapp.feature_list_streams.list.domain.model.IconAndTextInfo
import com.codandotv.streamplayerapp.feature_list_streams.list.domain.model.Stream
import com.codandotv.streamplayerapp.feature_list_streams.list.presentation.widgets.StreamsCardContent
import kotlinx.coroutines.flow.Flow
import com.codandotv.streamplayerapp.feature_list_streams.list.presentation.widgets.StreamsCarouselContent
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.combine
Expand All @@ -37,7 +36,7 @@ class ListStreamViewModel(

private val _uiState = MutableStateFlow(
ListStreamsUIState(
genres = emptyList(),
streamsCarouselContent = emptyList(),
isLoading = false
)
)
Expand All @@ -47,9 +46,7 @@ class ListStreamViewModel(
initialValue = _uiState.value
)

override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)

init {
viewModelScope.launch {
latestStream()
.combine(
Expand All @@ -64,29 +61,18 @@ class ListStreamViewModel(
.catchFailure {
println(">>>> ${it.errorMessage}")
}
.collect {
val (latest, genres) = it
.collect { pair ->
val (latest, genres) = pair

_uiState.update {
it.copy(
genres = genres,
streamsCarouselContent = genres.map { genreTarget ->
getStreamsByGenre(genreTarget)
},
highlightBanner = getHighlightBanner(latest)
)
}
}

if (uiState.value.genres.isEmpty()) {
listGenres()
.onStart { onLoading() }
.catchFailure {
println(">>>> ${it.errorMessage}")
}
.onCompletion { loaded() }
.collect { genres ->
_uiState.update {
it.copy(genres = genres)
}
}
}
}
}

Expand Down Expand Up @@ -114,16 +100,19 @@ class ListStreamViewModel(
),
)

fun loadMovies(genre: Genre): Flow<PagingData<StreamsCardContent>> {
return listStreams(genre).map {
it.map { stream ->
StreamsCardContent(
contentDescription = stream.name,
url = stream.posterPathUrl,
id = stream.id
)
}
}
private fun getStreamsByGenre(genre: Genre): StreamsCarouselContent {
return StreamsCarouselContent(
genre.name,
listStreams(genre).map {
it.map { stream ->
StreamsCardContent(
contentDescription = stream.name,
url = stream.posterPathUrl,
id = stream.id
)
}
}.cachedIn(viewModelScope)
)
}

private fun loaded() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,9 @@ fun ListStreamsScreen(

HighlightBanner(data = uiState.highlightBanner)

uiState.genres.forEach { genre ->
uiState.streamsCarouselContent.forEach { streamCarouselContent ->
StreamsCarousel(
title = genre.name,
contentList = viewModel.loadMovies(genre),
content = streamCarouselContent,
onNavigateDetailList = onNavigateDetailList,
)
Spacer(modifier = Modifier.height(12.dp))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.codandotv.streamplayerapp.feature_list_streams.list.presentation.screens

import com.codandotv.streamplayerapp.feature_list_streams.list.domain.model.Genre
import com.codandotv.streamplayerapp.feature_list_streams.list.domain.model.HighlightBanner
import com.codandotv.streamplayerapp.feature_list_streams.list.presentation.widgets.StreamsCarouselContent

data class ListStreamsUIState(
val highlightBanner: HighlightBanner? = null,
val genres: List<Genre>,
val streamsCarouselContent: List<StreamsCarouselContent>,
val isLoading: Boolean
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.Preview
Expand All @@ -23,21 +22,23 @@ import androidx.paging.compose.itemKey
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.emptyFlow

data class StreamsCarouselContent(
val genreTitle: String,
val contentList: Flow<PagingData<StreamsCardContent>>
)

@Composable
fun StreamsCarousel(
title: String,
contentList: Flow<PagingData<StreamsCardContent>>,
content: StreamsCarouselContent,
modifier: Modifier = Modifier,
onNavigateDetailList: (String) -> Unit = {},
) {
val flow = remember { contentList }

val lazyPagingItems = flow.collectAsLazyPagingItems()
val lazyPagingItems = content.contentList.collectAsLazyPagingItems()
val lazyListState = rememberLazyListState()

Column(modifier = modifier) {
Text(
title,
content.genreTitle,
style = MaterialTheme.typography.headlineMedium.copy(
fontWeight = FontWeight.Bold,
fontSize = 20.sp
Expand Down Expand Up @@ -73,7 +74,9 @@ fun StreamsCarousel(
@Preview
fun StreamsCarouselPreview() {
StreamsCarousel(
title = "Ação",
contentList = emptyFlow()
content = StreamsCarouselContent(
genreTitle = "Ação",
contentList = emptyFlow()
)
)
}

0 comments on commit 1f0c969

Please sign in to comment.