-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from smish-hash/pagination
Pagination3 support added
- Loading branch information
Showing
13 changed files
with
242 additions
and
149 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
60 changes: 60 additions & 0 deletions
60
app/src/main/java/com/smish/abda/data/repository/datasource/MoviePagingSource.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,60 @@ | ||
package com.smish.abda.data.repository.datasource | ||
|
||
import androidx.paging.PagingSource | ||
import androidx.paging.PagingState | ||
import com.smish.abda.data.api.MovieApiService | ||
import com.smish.abda.data.model.movie.Search | ||
import okio.IOException | ||
import retrofit2.HttpException | ||
|
||
private const val OMDB_STARTING_PAGE_INDEX = 1 | ||
|
||
class MoviePagingSource( | ||
private val query: String, | ||
private val type: String, | ||
private val movieApiService: MovieApiService | ||
): PagingSource<Int, Search>() { | ||
|
||
override fun getRefreshKey(state: PagingState<Int, Search>): Int? { | ||
/* We need to get the previous key (or next key if previous is null) of the page | ||
that was closest to the most recently accessed index. | ||
Anchor position is the most recently accessed index.*/ | ||
return state.anchorPosition?.let { anchorPosition -> | ||
state.closestPageToPosition(anchorPosition)?.prevKey?.plus(1) | ||
?: state.closestPageToPosition(anchorPosition)?.nextKey?.minus(1) | ||
} | ||
} | ||
|
||
override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Search> { | ||
val pageIndex = params.key ?: OMDB_STARTING_PAGE_INDEX | ||
|
||
return try { | ||
val response = movieApiService.getMovies( | ||
searchQuery = query, | ||
type = type, | ||
page = pageIndex | ||
) | ||
// can return load state . error here | ||
|
||
val movies: List<Search> = (response.movies ?: emptyList()) as List<Search> | ||
val nextKey = | ||
if (movies.isEmpty()) { | ||
null | ||
} else { | ||
/* By default, initial load size = 3 * NETWORK PAGE SIZE | ||
ensure we're not requesting duplicating items at the 2nd request | ||
pageIndex + (params.loadSize / NETWORK_PAGE_SIZE)*/ | ||
pageIndex + 1 | ||
} | ||
LoadResult.Page( | ||
data = movies, | ||
prevKey = if (pageIndex == OMDB_STARTING_PAGE_INDEX) null else pageIndex, | ||
nextKey = nextKey | ||
) | ||
} catch (exception: IOException) { | ||
return LoadResult.Error(exception) | ||
} catch (exception: HttpException) { | ||
return LoadResult.Error(exception) | ||
} | ||
} | ||
} |
6 changes: 4 additions & 2 deletions
6
app/src/main/java/com/smish/abda/data/repository/datasource/MovieRemoteDataSource.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,12 +1,14 @@ | ||
package com.smish.abda.data.repository.datasource | ||
|
||
import com.smish.abda.data.model.movie.Movie | ||
import androidx.paging.PagingData | ||
import com.smish.abda.data.model.movie.Search | ||
import com.smish.abda.data.model.moviedetail.MovieDetail | ||
import kotlinx.coroutines.flow.Flow | ||
import retrofit2.Response | ||
|
||
interface MovieRemoteDataSource { | ||
// defining functions to communicate with the API | ||
// use for both search and all | ||
suspend fun getMovies(searchQuery: String, page: Int): Response<Movie> | ||
fun getMovies(searchQuery: String, type: String, page: Int): Flow<PagingData<Search>> | ||
suspend fun getMovieDetails(imdbId: String): Response<MovieDetail> | ||
} |
19 changes: 16 additions & 3 deletions
19
app/src/main/java/com/smish/abda/data/repository/datasourceimpl/MovieRemoteDataSourceImpl.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
4 changes: 2 additions & 2 deletions
4
app/src/main/java/com/smish/abda/domain/repository/MovieRepository.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
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,11 +1,12 @@ | ||
package com.smish.abda.domain.usecase | ||
|
||
import com.smish.abda.data.model.movie.Movie | ||
import androidx.paging.PagingData | ||
import com.smish.abda.data.model.movie.Search | ||
import com.smish.abda.domain.repository.MovieRepository | ||
import com.smish.abda.util.Resource | ||
import kotlinx.coroutines.flow.Flow | ||
|
||
class GetMovies(private val movieRepository: MovieRepository) { | ||
suspend fun getMovies(searchQuery: String, page: Int): Resource<Movie> { | ||
return movieRepository.getMovies(searchQuery, page) | ||
fun getMovies(searchQuery: String, type: String, page: Int): Flow<PagingData<Search>> { | ||
return movieRepository.getMovies(searchQuery, type, page) | ||
} | ||
} |
82 changes: 82 additions & 0 deletions
82
app/src/main/java/com/smish/abda/ui/movieList/components/MovieList.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,82 @@ | ||
package com.smish.abda.ui.movieList.components | ||
|
||
import androidx.compose.foundation.layout.Arrangement | ||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxSize | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.material.CircularProgressIndicator | ||
import androidx.compose.material.Text | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.mutableStateOf | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.paging.LoadState | ||
import androidx.paging.compose.LazyPagingItems | ||
import androidx.paging.compose.items | ||
import com.smish.abda.data.model.movie.Search | ||
|
||
@Composable | ||
fun MovieList( | ||
movies: LazyPagingItems<Search>, | ||
modifier: Modifier = Modifier, | ||
onMovieClick: (Search) -> Unit, | ||
onBookmarkClick: (isChecked: Boolean, movie: Search) -> Unit | ||
) { | ||
when (movies.loadState.refresh) { | ||
LoadState.Loading -> { | ||
Column( | ||
modifier = Modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
CircularProgressIndicator() | ||
} | ||
} | ||
is LoadState.Error -> { | ||
Column( | ||
modifier = Modifier.fillMaxSize(), | ||
verticalArrangement = Arrangement.Center, | ||
horizontalAlignment = Alignment.CenterHorizontally | ||
) { | ||
Text(text = "Something went wrong") | ||
} | ||
} | ||
else -> { | ||
/*LazyColumn(modifier = modifier) { | ||
itemsIndexed(books) { index, item -> | ||
item?.let { | ||
BookItem( | ||
book = item, | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.background(getBackgroundForIndex(index)) | ||
.padding(vertical = 15.dp) | ||
) | ||
} | ||
} | ||
}*/ | ||
LazyColumn( | ||
modifier = Modifier.fillMaxSize() | ||
) { | ||
items(movies) { movie -> | ||
if (movie != null) { | ||
val (isChecked, setChecked) = remember { mutableStateOf(false) } | ||
MovieListItem( | ||
movie = movie, | ||
onMovieClick = { | ||
// call the movie detail api and trigger the bottom sheet | ||
onMovieClick(movie) | ||
}, | ||
isChecked, | ||
onBookmarkClick = { | ||
setChecked(!isChecked) | ||
onBookmarkClick(isChecked, movie) | ||
} | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.