Skip to content

Commit

Permalink
Merge pull request #85 from joelkanyi/fix-pagination
Browse files Browse the repository at this point in the history
fix films and search pagination
  • Loading branch information
joelkanyi authored May 21, 2024
2 parents 3704324 + ab9579c commit 3b06585
Show file tree
Hide file tree
Showing 7 changed files with 431 additions and 535 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ object Constants {

const val MUVIZ_PREFERENCES = "MEALTIME_PREFERENCES"
val THEME_OPTIONS = intPreferencesKey(name = "theme_option")
const val PAGING_SIZE = 20
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.kanyideveloper.muviz.common.data.network.TMDBApi
import com.kanyideveloper.muviz.common.util.Constants.PAGING_SIZE
import com.kanyideveloper.muviz.home.domain.model.Series
import com.kanyideveloper.muviz.home.data.paging.AiringTodayTvSeriesSource
import com.kanyideveloper.muviz.home.data.paging.OnTheAirSeriesSource
Expand All @@ -31,7 +32,7 @@ import javax.inject.Inject
class TvSeriesRepository @Inject constructor(private val api: TMDBApi) {
fun getTrendingThisWeekTvSeries(): Flow<PagingData<Series>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 27),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGING_SIZE),
pagingSourceFactory = {
TrendingSeriesSource(api)
}
Expand All @@ -40,7 +41,7 @@ class TvSeriesRepository @Inject constructor(private val api: TMDBApi) {

fun getOnTheAirTvSeries(): Flow<PagingData<Series>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 27),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGING_SIZE),
pagingSourceFactory = {
OnTheAirSeriesSource(api)
}
Expand All @@ -49,7 +50,7 @@ class TvSeriesRepository @Inject constructor(private val api: TMDBApi) {

fun getTopRatedTvSeries(): Flow<PagingData<Series>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 27),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGING_SIZE),
pagingSourceFactory = {
TopRatedSeriesSource(api)
}
Expand All @@ -58,7 +59,7 @@ class TvSeriesRepository @Inject constructor(private val api: TMDBApi) {

fun getAiringTodayTvSeries(): Flow<PagingData<Series>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 27),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGING_SIZE),
pagingSourceFactory = {
AiringTodayTvSeriesSource(api)
}
Expand All @@ -67,7 +68,7 @@ class TvSeriesRepository @Inject constructor(private val api: TMDBApi) {

fun getPopularTvSeries(): Flow<PagingData<Series>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 27),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGING_SIZE),
pagingSourceFactory = {
PopularSeriesSource(api)
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ class HomeViewModel @Inject constructor(
}

fun refreshAllData() {
getSeriesGenres()
getMoviesGenres()
getTrendingMovies(homeUiState.value.selectedGenre?.id)
getNowPayingMovies(homeUiState.value.selectedGenre?.id)
getUpcomingMovies(homeUiState.value.selectedGenre?.id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package com.kanyideveloper.muviz.search.data.repository

import android.nfc.tech.MifareUltralight.PAGE_SIZE
import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
Expand All @@ -28,7 +29,7 @@ import javax.inject.Inject
class SearchRepositoryImpl @Inject constructor(private val api: TMDBApi): SearchRepository {
override fun multiSearch(queryParam: String): Flow<PagingData<Search>> {
return Pager(
config = PagingConfig(enablePlaceholders = false, pageSize = 27),
config = PagingConfig(enablePlaceholders = false, pageSize = PAGE_SIZE),
pagingSourceFactory = {
SearchPagingSource(api, queryParam)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,17 @@ import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.wrapContentHeight
import androidx.compose.foundation.layout.wrapContentSize
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardOptions
Expand Down Expand Up @@ -164,7 +161,8 @@ fun SearchScreenContent(
Modifier
.padding(innerPadding)
.padding(horizontal = 16.dp)
.fillMaxSize()
.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
val focusRequester = remember { FocusRequester() }
LaunchedEffect(Unit) {
Expand All @@ -178,68 +176,44 @@ fun SearchScreenContent(
onEvent = onEvent,
state = state,
)
Spacer(modifier = Modifier.height(8.dp))

Box(
LazyColumn(
modifier = Modifier.fillMaxSize(),
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
LazyColumn(
verticalArrangement = Arrangement.spacedBy(8.dp),
) {
items(
count = searchResult.itemCount,
) { index ->
val search = searchResult[index]
SearchItem(
search = search,
state = state,
onClick = {
onEvent(SearchUiEvents.OpenFilmDetails(search))
}
)
}

if (searchResult.loadState.append == LoadState.Loading) {
item {
CircularProgressIndicator(
modifier = Modifier
.fillMaxWidth()
.wrapContentWidth(Alignment.CenterHorizontally)
)
items(searchResult.itemCount) { index ->
val search = searchResult[index]
SearchItem(
search = search,
state = state,
onClick = {
onEvent(SearchUiEvents.OpenFilmDetails(search))
}
}
)
}

searchResult.apply {
when (loadState.refresh) {
is LoadState.Error -> {
val e = searchResult.loadState.refresh as LoadState.Error
Text(
text = when (e.error) {
is HttpException -> {
"Oops, something went wrong!"
}

is IOException -> {
"Couldn't reach server, check your internet connection!"
}

else -> {
"Unknown error occurred"
}
},
modifier = Modifier
.align(alignment = Alignment.Center)
.padding(12.dp),
textAlign = TextAlign.Center,
color = MaterialTheme.colorScheme.primary,
)
searchResult.loadState.let { loadState ->
when {
loadState.refresh is LoadState.Loading -> {
item {
Column(
modifier = Modifier
.fillParentMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
CircularProgressIndicator(
strokeWidth = 2.dp,
)
}
}
}

is LoadState.NotLoading -> {
if (searchResult.itemCount <= 0) {
loadState.refresh is LoadState.NotLoading && searchResult.itemCount < 1 -> {
item {
Column(
Modifier.fillMaxSize(),
modifier = Modifier
.fillParentMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Expand All @@ -253,7 +227,69 @@ fun SearchScreenContent(
}
}

else -> {}

loadState.refresh is LoadState.Error -> {
item {
Column(
modifier = Modifier
.fillParentMaxSize(),
verticalArrangement = Arrangement.Center,
horizontalAlignment = Alignment.CenterHorizontally
) {
Text(
modifier = Modifier
.fillMaxWidth(),
text = when ((loadState.refresh as LoadState.Error).error) {
is HttpException -> {
"Oops, something went wrong!"
}

is IOException -> {
"Couldn't reach server, check your internet connection!"
}

else -> {
"Unknown error occurred"
}
},
style = MaterialTheme.typography.bodyMedium,
textAlign = TextAlign.Center,
color = MaterialTheme.colorScheme.primary,
)
}
}
}

loadState.append is LoadState.Loading -> {
item {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center,
) {
CircularProgressIndicator(
modifier = Modifier
.size(16.dp),
strokeWidth = 2.dp,
)
}
}
}

loadState.append is LoadState.Error -> {
item {
Row(
modifier = Modifier.fillMaxWidth(),
horizontalArrangement = Arrangement.Center,
) {
Text(
modifier = Modifier.fillMaxWidth(),
text = "An error occurred",
style = MaterialTheme.typography.bodyMedium,
textAlign = TextAlign.Center,
)
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,14 @@ package com.kanyideveloper.muviz.search.presentation
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import androidx.paging.cachedIn
import androidx.paging.filter
import com.kanyideveloper.muviz.common.util.Resource
import com.kanyideveloper.muviz.genre.domain.usecase.GetMovieGenresUseCase
import com.kanyideveloper.muviz.genre.domain.usecase.GetTvSeriesGenresUseCase
import com.kanyideveloper.muviz.search.domain.repository.SearchRepository
import com.kanyideveloper.muviz.search.domain.usecase.SearchFilmUseCase
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Job
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.map
import kotlinx.coroutines.flow.update
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand Down

0 comments on commit 3b06585

Please sign in to comment.