Skip to content

Commit

Permalink
Fix filter state changes not updating movies
Browse files Browse the repository at this point in the history
  • Loading branch information
yasinkacmaz committed Sep 26, 2024
1 parent 2db3704 commit fcaa018
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int, Movie>() {
private val options = movieRequestOptionsMapper.map(filterState)

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Movie> {
return try {
val page = params.key ?: 1
val options = movieRequestOptionsMapper.map(filterDataStore.filterState.first())
val moviesResponse = if (searchQuery.isNotBlank()) {
movieService.search(page, searchQuery)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -63,7 +62,6 @@ class MoviesViewModel(

init {
filterDataStore.filterState
.onEach { filterState -> this.filterState = filterState }
.drop(1)
.onEach { filterStateChanges.emit(it) }
.launchIn(viewModelScope)
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand All @@ -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)

Expand All @@ -49,6 +52,6 @@ class MoviesPagingSourceTest {

private fun initPagingSource(query: String = "") {
moviesPagingSource =
MoviesPagingSource(movieService, movieMapper, movieRequestOptionsMapper, filterState, query)
MoviesPagingSource(movieService, filterDataStore, movieMapper, movieRequestOptionsMapper, query)
}
}

0 comments on commit fcaa018

Please sign in to comment.