-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Pagination for Search and fix ui bugs
- Loading branch information
1 parent
c188ce3
commit 30b06af
Showing
18 changed files
with
204 additions
and
188 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,6 @@ android { | |
dependencies { | ||
|
||
implementation(project(":data:api:tmdb")) | ||
|
||
api(libs.paging.runtime) | ||
} |
54 changes: 54 additions & 0 deletions
54
data/tmdb-search/src/main/java/io/filmtime/data/tmdb/search/SearchPagingSource.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package io.filmtime.data.tmdb.search | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import io.filmtime.data.api.tmdb.TmdbMoviesRemoteSource | ||
import io.filmtime.data.api.tmdb.TmdbSearchRemoteSource | ||
import io.filmtime.data.model.GeneralError | ||
import io.filmtime.data.model.Result | ||
import io.filmtime.data.model.Result.Failure | ||
import io.filmtime.data.model.Result.Success | ||
import io.filmtime.data.model.SearchResult | ||
import io.filmtime.data.model.SearchType | ||
import io.filmtime.data.model.SearchType.All | ||
import io.filmtime.data.model.SearchType.Movie | ||
import io.filmtime.data.model.SearchType.Show | ||
import io.filmtime.data.model.toThrowable | ||
|
||
class SearchPagingSource constructor( | ||
private val tmdbSearchRemoteSource: TmdbSearchRemoteSource, | ||
private val searchType: SearchType, | ||
private val query: String, | ||
) : PagingSource<Int, SearchResult>() { | ||
override fun getRefreshKey(state: PagingState<Int, SearchResult>): Int = STARTING_PAGE_INDEX | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, SearchResult> = try { | ||
val page = params.key ?: STARTING_PAGE_INDEX | ||
when (val response = search(page, query)) { | ||
is Success -> { | ||
val result = response.data | ||
LoadResult.Page( | ||
data = result, | ||
prevKey = if (page == STARTING_PAGE_INDEX) null else page - 1, | ||
nextKey = if (result.size < PAGE_SIZE) null else page + 1, | ||
) | ||
} | ||
|
||
is Failure -> LoadResult.Error(response.error.toThrowable()) | ||
} | ||
} catch (e: Exception) { | ||
LoadResult.Error(e) | ||
} | ||
|
||
private suspend fun search(page: Int, query: String): Result<List<SearchResult>, GeneralError> = | ||
when (searchType) { | ||
All -> tmdbSearchRemoteSource.searchAll(page, query) | ||
Movie -> tmdbSearchRemoteSource.searchMovies(page, query) | ||
Show -> tmdbSearchRemoteSource.searchTvShows(page, query) | ||
} | ||
|
||
companion object { | ||
private const val STARTING_PAGE_INDEX = 1 | ||
private const val PAGE_SIZE = TmdbMoviesRemoteSource.PAGE_SIZE | ||
} | ||
} |
11 changes: 4 additions & 7 deletions
11
data/tmdb-search/src/main/java/io/filmtime/data/tmdb/search/TmdbSearchRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
package io.filmtime.data.tmdb.search | ||
|
||
import io.filmtime.data.model.GeneralError | ||
import io.filmtime.data.model.Result | ||
import androidx.paging.PagingData | ||
import io.filmtime.data.model.SearchResult | ||
import io.filmtime.data.model.SearchResult.TvShow | ||
import io.filmtime.data.model.SearchType | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface TmdbSearchRepository { | ||
suspend fun searchMovies(query: String): Result<List<SearchResult.Video>, GeneralError> | ||
|
||
suspend fun searchTvShows(query: String): Result<List<TvShow>, GeneralError> | ||
|
||
suspend fun searchAll(query: String): Result<List<SearchResult>, GeneralError> | ||
fun streamSearch(searchType: SearchType, query: String): Flow<PagingData<SearchResult>> | ||
} |
31 changes: 20 additions & 11 deletions
31
data/tmdb-search/src/main/java/io/filmtime/data/tmdb/search/TmdbSearchRepositoryImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,31 @@ | ||
package io.filmtime.data.tmdb.search | ||
|
||
import androidx.paging.Pager | ||
import androidx.paging.PagingConfig | ||
import androidx.paging.PagingData | ||
import io.filmtime.data.api.tmdb.TmdbMoviesRemoteSource | ||
import io.filmtime.data.api.tmdb.TmdbSearchRemoteSource | ||
import io.filmtime.data.model.GeneralError | ||
import io.filmtime.data.model.Result | ||
import io.filmtime.data.model.SearchResult | ||
import io.filmtime.data.model.SearchResult.TvShow | ||
import io.filmtime.data.model.SearchResult.Video | ||
import io.filmtime.data.model.SearchType | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
|
||
internal class TmdbSearchRepositoryImpl @Inject constructor( | ||
private val tmdbDataSource: TmdbSearchRemoteSource, | ||
) : TmdbSearchRepository { | ||
override suspend fun searchMovies(query: String): Result<List<Video>, GeneralError> = | ||
tmdbDataSource.searchMovies(query) | ||
|
||
override suspend fun searchTvShows(query: String): Result<List<TvShow>, GeneralError> = | ||
tmdbDataSource.searchTvShows(query) | ||
|
||
override suspend fun searchAll(query: String): Result<List<SearchResult>, GeneralError> = | ||
tmdbDataSource.searchAll(query) | ||
override fun streamSearch(searchType: SearchType, query: String): Flow<PagingData<SearchResult>> = | ||
Pager( | ||
config = PagingConfig( | ||
pageSize = TmdbMoviesRemoteSource.PAGE_SIZE, | ||
enablePlaceholders = false, | ||
), | ||
pagingSourceFactory = { | ||
SearchPagingSource( | ||
tmdbSearchRemoteSource = tmdbDataSource, | ||
searchType = searchType, | ||
query = query, | ||
) | ||
}, | ||
).flow | ||
} |
6 changes: 3 additions & 3 deletions
6
domain/tmdb-search/src/main/java/io/filmtime/domain/tmdb/search/SearchTmdbUseCase.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,10 @@ | ||
package io.filmtime.domain.tmdb.search | ||
|
||
import io.filmtime.data.model.GeneralError | ||
import io.filmtime.data.model.Result | ||
import androidx.paging.PagingData | ||
import io.filmtime.data.model.SearchResult | ||
import io.filmtime.data.model.SearchType | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
interface SearchTmdbUseCase { | ||
suspend operator fun invoke(query: String, type: SearchType): Result<List<SearchResult>, GeneralError> | ||
operator fun invoke(query: String, type: SearchType): Flow<PagingData<SearchResult>> | ||
} |
16 changes: 5 additions & 11 deletions
16
...in/tmdb-search/src/main/java/io/filmtime/domain/tmdb/search/impl/SearchTmdbUseCaseImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,20 @@ | ||
package io.filmtime.domain.tmdb.search.impl | ||
|
||
import io.filmtime.data.model.GeneralError | ||
import io.filmtime.data.model.Result | ||
import androidx.paging.PagingData | ||
import io.filmtime.data.model.SearchResult | ||
import io.filmtime.data.model.SearchType | ||
import io.filmtime.data.model.SearchType.All | ||
import io.filmtime.data.model.SearchType.Movie | ||
import io.filmtime.data.model.SearchType.Show | ||
import io.filmtime.data.tmdb.search.TmdbSearchRepository | ||
import io.filmtime.domain.tmdb.search.SearchTmdbUseCase | ||
import kotlinx.coroutines.flow.Flow | ||
import javax.inject.Inject | ||
|
||
internal class SearchTmdbUseCaseImpl @Inject constructor( | ||
private val tmdbSearchRepository: TmdbSearchRepository, | ||
) : SearchTmdbUseCase { | ||
|
||
override suspend fun invoke( | ||
override fun invoke( | ||
query: String, | ||
type: SearchType, | ||
): Result<List<SearchResult>, GeneralError> = when (type) { | ||
All -> tmdbSearchRepository.searchAll(query) | ||
Movie -> tmdbSearchRepository.searchMovies(query) | ||
Show -> tmdbSearchRepository.searchTvShows(query) | ||
} | ||
): Flow<PagingData<SearchResult>> = | ||
tmdbSearchRepository.streamSearch(type, query) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.