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

Move filter state to MoviesPagingSource #306

Merged
merged 1 commit into from
Sep 26, 2024
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
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)
}
}