joelkanyi / Muviz

A Jetpack compose app that consumes the TMDB API to display movie and Tv shows and their details
199 stars 30 forks source link

FilterList #96

Closed Rafelcf closed 1 month ago

Rafelcf commented 2 months ago

when filterList for example originalLanguage in popularmovies? and sortBy date

joelkanyi commented 2 months ago

Hey @Rafelcf I think your issue is not clear, could you please explain it well

Rafelcf commented 2 months ago

Thanks, my question is: In PopuarMovies If I want it to only show me the movies in their original language "original_language" = EU Where and how to apply the filter? *.filter { it.original_language == "EU"}

joelkanyi commented 1 month ago

Hey @Rafelcf sorry for the late reply but to filter out some movies, you can do that from the paging source e.g

LoadResult.Page(
    data = trendingMoviesList.searches.filter {
        it.originalLanguage == "en"
    },
    prevKey = if (nextPage == 1) null else nextPage - 1,
    nextKey = if (trendingMoviesList.searches.isEmpty()) null else trendingMoviesList.page + 1
)

Full paging source example:

class TrendingMoviesSource(private val api: TMDBApi) :
    PagingSource<Int, Movie>() {
    override fun getRefreshKey(state: PagingState<Int, Movie>): Int? {
        return state.anchorPosition
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Movie> {
        return try {
            val nextPage = params.key ?: 1
            val trendingMoviesList = api.getTrendingTodayMovies(nextPage)
            LoadResult.Page(
                data = trendingMoviesList.searches.filter {
                    it.originalLanguage == "en"
                },
                prevKey = if (nextPage == 1) null else nextPage - 1,
                nextKey = if (trendingMoviesList.searches.isEmpty()) null else trendingMoviesList.page + 1
            )
        } catch (exception: IOException) {
            return LoadResult.Error(exception)
        } catch (exception: HttpException) {
            return LoadResult.Error(exception)
        }
    }
}