Skip to content

Commit

Permalink
Added Pagination 3 support - with type specific
Browse files Browse the repository at this point in the history
  • Loading branch information
smish-hash committed Jan 3, 2023
1 parent 05a29cd commit c2abaf1
Show file tree
Hide file tree
Showing 10 changed files with 37 additions and 181 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,9 @@ dependencies {
implementation "androidx.lifecycle:lifecycle-viewmodel-compose:2.5.0-rc01"
implementation "androidx.navigation:navigation-compose:2.5.0-rc01"
implementation 'androidx.paging:paging-compose:1.0.0-alpha17'
implementation("androidx.paging:paging-runtime:$paging_version")
// alternatively - without Android dependencies for tests
testImplementation("androidx.paging:paging-common:$paging_version")

// coroutines
implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-core:1.6.1'
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/java/com/smish/abda/data/api/MovieApiService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ interface MovieApiService {
searchQuery: String,
@Query("page")
page: Int,
@Query("type")
type: String,
@Query("apikey")
apiKey: String = BuildConfig.MOVIE_API_KEY
): Movie
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class MovieRepositoryImpl(
private val movieLocalDataSource: MovieLocalDataSource
): MovieRepository {

override fun getMovies(searchQuery: String, page: Int): Flow<PagingData<Search>> {
return movieRemoteDataSource.getMovies(searchQuery, page)
override fun getMovies(searchQuery: String, type: String, page: Int): Flow<PagingData<Search>> {
return movieRemoteDataSource.getMovies(searchQuery, type, page)
/*if (response.isSuccessful) {
response.body()?.let { res ->
if (res.response == "True") {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ 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>() {

Expand All @@ -30,6 +31,7 @@ class MoviePagingSource(
return try {
val response = movieApiService.getMovies(
searchQuery = query,
type = type,
page = pageIndex
)
// can return load state . error here
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.smish.abda.data.repository.datasource

import androidx.paging.PagingData
import com.smish.abda.data.model.movie.Movie
import com.smish.abda.data.model.movie.Search
import com.smish.abda.data.model.moviedetail.MovieDetail
import kotlinx.coroutines.flow.Flow
Expand All @@ -10,6 +9,6 @@ import retrofit2.Response
interface MovieRemoteDataSource {
// defining functions to communicate with the API
// use for both search and all
fun getMovies(searchQuery: String, page: Int): Flow<PagingData<Search>>
fun getMovies(searchQuery: String, type: String, page: Int): Flow<PagingData<Search>>
suspend fun getMovieDetails(imdbId: String): Response<MovieDetail>
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import androidx.paging.Pager
import androidx.paging.PagingConfig
import androidx.paging.PagingData
import com.smish.abda.data.api.MovieApiService
import com.smish.abda.data.model.movie.Movie
import com.smish.abda.data.model.movie.Search
import com.smish.abda.data.model.moviedetail.MovieDetail
import com.smish.abda.data.repository.datasource.MoviePagingSource
Expand All @@ -16,16 +15,14 @@ class MovieRemoteDataSourceImpl(
private val movieAPI: MovieApiService
): MovieRemoteDataSource {

override fun getMovies(searchQuery: String, page: Int): Flow<PagingData<Search>> {
// return movieAPI.getMovies(searchQuery, page)
// have the implementation here for paging data source, return pager here
override fun getMovies(searchQuery: String, type: String, page: Int): Flow<PagingData<Search>> {
return Pager(
config = PagingConfig(
pageSize = 10,
enablePlaceholders = false
),
pagingSourceFactory = {
MoviePagingSource(query = searchQuery, movieApiService = movieAPI)
MoviePagingSource(query = searchQuery, type = type, movieApiService = movieAPI)
}
).flow
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
package com.smish.abda.domain.repository

import androidx.paging.PagingData
import com.smish.abda.data.model.movie.Movie
import com.smish.abda.data.model.movie.Search
import com.smish.abda.data.model.moviedetail.MovieDetail
import com.smish.abda.util.Resource
import kotlinx.coroutines.flow.Flow

interface MovieRepository {
// this will be used for both get all movies and search
fun getMovies(searchQuery: String, page: Int): Flow<PagingData<Search>>
fun getMovies(searchQuery: String, type: String, page: Int): Flow<PagingData<Search>>
suspend fun getMovieDetails(imdbId: String): Resource<MovieDetail>

suspend fun bookmarkMovie(movie: Search)
Expand Down
6 changes: 2 additions & 4 deletions app/src/main/java/com/smish/abda/domain/usecase/GetMovies.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
package com.smish.abda.domain.usecase

import androidx.paging.PagingData
import com.smish.abda.data.model.movie.Movie
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) {
fun getMovies(searchQuery: String, page: Int): Flow<PagingData<Search>> {
return movieRepository.getMovies(searchQuery, page)
fun getMovies(searchQuery: String, type: String, page: Int): Flow<PagingData<Search>> {
return movieRepository.getMovies(searchQuery, type, page)
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
package com.smish.abda.ui.movieList.components

import androidx.compose.animation.core.animateIntAsState
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.LazyRow
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.selection.toggleable
import androidx.compose.foundation.shape.RoundedCornerShape
Expand All @@ -20,7 +16,6 @@ import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalConfiguration
import androidx.compose.ui.res.colorResource
import androidx.compose.ui.text.TextStyle
import androidx.compose.ui.text.font.FontWeight
Expand All @@ -33,7 +28,6 @@ import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.paging.compose.collectAsLazyPagingItems
import androidx.paging.compose.items
import com.smish.abda.R
import com.smish.abda.data.model.movie.Type
import com.smish.abda.data.model.movie.getAllTypes
Expand All @@ -42,16 +36,12 @@ import com.smish.abda.data.model.moviedetail.MovieDetail
import com.smish.abda.ui.viewmodel.MoviesViewmodel
import kotlinx.coroutines.launch

enum class ExpandedType {
HALF, FULL, COLLAPSED
}

@OptIn(ExperimentalMaterialApi::class)
@Composable
fun MovieListScreen(
viewmodel: MoviesViewmodel = hiltViewModel()
) {
val state = viewmodel.movieState.value
val movieDetailState = viewmodel.movieDetailState.value

val textState = remember { mutableStateOf(TextFieldValue("")) }
Expand All @@ -67,19 +57,6 @@ fun MovieListScreen(
mutableStateOf(false)
}

val configuration = LocalConfiguration.current
val screenHeight = configuration.screenHeightDp
var expandedType by remember {
mutableStateOf(ExpandedType.COLLAPSED)
}
val height by animateIntAsState(
when (expandedType) {
ExpandedType.HALF -> screenHeight / 2
ExpandedType.FULL -> screenHeight
ExpandedType.COLLAPSED -> 0
}
)

val movieList = viewmodel.getPagingMovies().collectAsLazyPagingItems()
ModalBottomSheetLayout(
sheetState = bottomSheetModalState,
Expand All @@ -89,7 +66,6 @@ fun MovieListScreen(
movie = movieDetailState.movies
LaunchedEffect(bottomSheetModalState) {
bottomSheetModalState.show()
// bottomSheetScaffoldState.bottomSheetState.expand()
}
}
Column(
Expand Down Expand Up @@ -140,7 +116,7 @@ fun MovieListScreen(
selectedType = selectedType.value,
onSelectedChanged = {
selectedType.value = getType(it)
viewmodel.getSpecificType(it)
viewmodel.setSpecificType(it)
}
)

Expand All @@ -159,32 +135,6 @@ fun MovieListScreen(
}
)

/*LazyColumn(
modifier = Modifier.fillMaxSize()
) {
items(movieList) { movie ->
if (movie != null) {
val (isChecked, setChecked) = remember { mutableStateOf(false) }
MovieListItem(
movie = movie,
onMovieClick = {
// call the movie detail api and trigger the bottom sheet
showModalSheet.value = !showModalSheet.value
viewmodel.getMovieDetails(movie.imdbID)
},
isChecked,
onBookmarkClick = {
setChecked(!isChecked)
if (!isChecked)
viewmodel.bookmarkMovie(movie)
else
viewmodel.removeMovie(movie)
}
)
}
}
}*/

/*LazyVerticalGrid(
modifier = Modifier.fillMaxSize(),
columns = GridCells.Adaptive(minSize = 135.dp)
Expand Down Expand Up @@ -213,7 +163,7 @@ fun MovieListScreen(
}*/
}

if (state.error.isNotBlank()) {
/*if (state.error.isNotBlank()) {
Text(
text = state.error,
color = MaterialTheme.colors.error,
Expand All @@ -223,12 +173,12 @@ fun MovieListScreen(
.padding(horizontal = 20.dp)
.align(Alignment.Center)
)
}
}*/

// Progress Bar
if (state.isLoading) {
/*if (state.isLoading) {
CircularProgressIndicator(modifier = Modifier.align(Alignment.Center))
}
}*/
}
}

Expand Down Expand Up @@ -325,8 +275,8 @@ fun ChipGroup(
Chip(
name = it.value,
isSelected = selectedType == it,
onSelectionChanged = {
onSelectedChanged(it)
onSelectionChanged = { type ->
onSelectedChanged(type)
},
)
}
Expand Down Expand Up @@ -365,13 +315,6 @@ fun Chip(
}
}

@Preview(showBackground = false)
@Composable
fun SearchViewPreview() {
val textState = remember { mutableStateOf(TextFieldValue("")) }
// SearchView(textState)
}

@Preview(showBackground = false)
@Composable
fun TypeGroupPreview() {
Expand Down
Loading

0 comments on commit c2abaf1

Please sign in to comment.