Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ISSUE-72] - Mantendo paginas já carregadas após recomposição #73

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

onCreate estava sendo chamado mais de uma vez, trigando ao retornar da tela. Para realizar a chamada uma única vez, eu movi para o init block

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)
)
}
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removido por causa que era uma request duplicada (já realizada anteriormente no combine)

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()
)
)
}