From fcaa0184cf21fe60407074ecc00afd702a5e2509 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yasin=20Ka=C3=A7maz?= Date: Thu, 26 Sep 2024 12:17:49 +0300 Subject: [PATCH] Fix filter state changes not updating movies --- .../jetflix/ui/movies/MoviesPagingSource.kt | 10 +++++----- .../jetflix/ui/movies/MoviesViewModel.kt | 4 +--- .../jetflix/ui/movies/MoviesPagingSourceTest.kt | 13 ++++++++----- 3 files changed, 14 insertions(+), 13 deletions(-) diff --git a/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesPagingSource.kt b/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesPagingSource.kt index c4c245a0..260fd8d3 100644 --- a/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesPagingSource.kt +++ b/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesPagingSource.kt @@ -3,23 +3,23 @@ package com.yasinkacmaz.jetflix.ui.movies import androidx.paging.PagingSource import androidx.paging.PagingState import com.yasinkacmaz.jetflix.data.service.MovieService -import com.yasinkacmaz.jetflix.ui.filter.FilterState +import com.yasinkacmaz.jetflix.ui.filter.FilterDataStore import com.yasinkacmaz.jetflix.ui.filter.MovieRequestOptionsMapper import com.yasinkacmaz.jetflix.ui.movies.movie.Movie import com.yasinkacmaz.jetflix.ui.movies.movie.MovieMapper +import kotlinx.coroutines.flow.first class MoviesPagingSource( private val movieService: MovieService, + private val filterDataStore: FilterDataStore, private val movieMapper: MovieMapper, - movieRequestOptionsMapper: MovieRequestOptionsMapper, - filterState: FilterState? = null, + private val movieRequestOptionsMapper: MovieRequestOptionsMapper, private val searchQuery: String = "", ) : PagingSource() { - private val options = movieRequestOptionsMapper.map(filterState) - override suspend fun load(params: LoadParams): LoadResult { return try { val page = params.key ?: 1 + val options = movieRequestOptionsMapper.map(filterDataStore.filterState.first()) val moviesResponse = if (searchQuery.isNotBlank()) { movieService.search(page, searchQuery) } else { diff --git a/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesViewModel.kt b/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesViewModel.kt index d69afd26..28c95636 100644 --- a/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesViewModel.kt +++ b/app/src/main/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesViewModel.kt @@ -38,14 +38,13 @@ class MoviesViewModel( pagingSourceFactory = { MoviesPagingSource( movieService, + filterDataStore, movieMapper, movieRequestOptionsMapper, - filterState, searchQuery.value, ) }, ).flow.cachedIn(viewModelScope) - private var filterState: FilterState? = null private val _searchQuery = MutableStateFlow("") val searchQuery = _searchQuery.asStateFlow() @@ -63,7 +62,6 @@ class MoviesViewModel( init { filterDataStore.filterState - .onEach { filterState -> this.filterState = filterState } .drop(1) .onEach { filterStateChanges.emit(it) } .launchIn(viewModelScope) diff --git a/app/src/test/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesPagingSourceTest.kt b/app/src/test/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesPagingSourceTest.kt index 23aba621..a8ef3afc 100644 --- a/app/src/test/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesPagingSourceTest.kt +++ b/app/src/test/kotlin/com/yasinkacmaz/jetflix/ui/movies/MoviesPagingSourceTest.kt @@ -1,12 +1,14 @@ package com.yasinkacmaz.jetflix.ui.movies import androidx.paging.PagingSource -import com.yasinkacmaz.jetflix.ui.filter.FilterState +import com.yasinkacmaz.jetflix.ui.filter.FilterDataStore import com.yasinkacmaz.jetflix.ui.filter.MovieRequestOptionsMapper import com.yasinkacmaz.jetflix.ui.movies.movie.Movie import com.yasinkacmaz.jetflix.ui.movies.movie.MovieMapper import com.yasinkacmaz.jetflix.util.CoroutineTestRule +import com.yasinkacmaz.jetflix.util.FakeStringDataStore import com.yasinkacmaz.jetflix.util.client.FakeMovieClient +import com.yasinkacmaz.jetflix.util.json import io.kotest.matchers.shouldBe import io.kotest.matchers.types.shouldBeInstanceOf import kotlinx.coroutines.test.runTest @@ -21,13 +23,14 @@ class MoviesPagingSourceTest { private val movieMapper = MovieMapper() private val movieRequestOptionsMapper = MovieRequestOptionsMapper() - private val filterState = FilterState() private val loadParams = PagingSource.LoadParams.Refresh(1, 1, true) + private val filterDataStore = FilterDataStore(json, FakeStringDataStore()) + private lateinit var moviesPagingSource: MoviesPagingSource @Test - fun `should call movies endpoint when query is empty`() = runTest { + fun `Should call movies endpoint when query is empty`() = runTest { initPagingSource() val loadResult = moviesPagingSource.load(loadParams) @@ -37,7 +40,7 @@ class MoviesPagingSourceTest { } @Test - fun `should call search endpoint when query is not empty`() = runTest { + fun `Should call search endpoint when query is not empty`() = runTest { val query = "query" initPagingSource(query) @@ -49,6 +52,6 @@ class MoviesPagingSourceTest { private fun initPagingSource(query: String = "") { moviesPagingSource = - MoviesPagingSource(movieService, movieMapper, movieRequestOptionsMapper, filterState, query) + MoviesPagingSource(movieService, filterDataStore, movieMapper, movieRequestOptionsMapper, query) } }