Skip to content

Commit

Permalink
Merge pull request #3 from smish-hash/pagination
Browse files Browse the repository at this point in the history
Pagination Support
  • Loading branch information
smish-hash authored Jan 10, 2023
2 parents b78d327 + a0a3c8e commit bda074b
Show file tree
Hide file tree
Showing 12 changed files with 111 additions and 60 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/com/smish/abda/data/db/MovieDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ interface MovieDao {

@Query("DELETE FROM movies")
suspend fun deleteAll()

@Query("SELECT imdbID FROM movies")
fun getSavedMoviesIds(): Flow<List<String>>
}
17 changes: 15 additions & 2 deletions app/src/main/java/com/smish/abda/data/model/movie/Search.kt
Original file line number Diff line number Diff line change
@@ -1,24 +1,37 @@
package com.smish.abda.data.model.movie


import androidx.room.ColumnInfo
import androidx.room.Entity
import androidx.room.PrimaryKey
import com.google.gson.annotations.SerializedName

@Entity(tableName = "movies")
data class Search(
@PrimaryKey
@ColumnInfo(name = "imdbID")
@SerializedName("imdbID")
val imdbID: String,

@SerializedName("Poster")
val poster: String,

@SerializedName("Title")
val title: String,

@SerializedName("Type")
val type: String,

@SerializedName("Year")
val year: String
)
val year: String,

@ColumnInfo(name = "isBookmarked")
var isBookmarked: Boolean
) {
fun toggle() {
isBookmarked = !isBookmarked
}
}

// since all are type string here, we do not create any type converter for room db else we would have needed one.
/*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,8 @@ class MovieRepositoryImpl(
override fun getBookmarkedMovies(): Flow<List<Search>> {
return movieLocalDataSource.getSavedMoviesFromDB()
}

override fun getSavedMoviesIds(): Flow<List<String>> {
return movieLocalDataSource.getSavedMoviesIdsFromDB()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,6 @@ interface MovieLocalDataSource {
/*defining functions to interact with the db*/
suspend fun saveMovieToDB(movie: Search)
fun getSavedMoviesFromDB(): Flow<List<Search>>
fun getSavedMoviesIdsFromDB(): Flow<List<String>>
suspend fun deleteMovieFromDB(imdbID: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class MovieLocalDataSourceImpl(
return movieDao.getAllSavedMovies()
}

override fun getSavedMoviesIdsFromDB(): Flow<List<String>> {
return movieDao.getSavedMoviesIds()
}

override suspend fun deleteMovieFromDB(imdbID: String) {
return movieDao.delete(imdbID)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ interface MovieRepository {
// to get data from db as a flow.
// asynchronous data stream in the viewmodel, collect it and emit it as live data.
fun getBookmarkedMovies(): Flow<List<Search>>

fun getSavedMoviesIds(): Flow<List<String>>
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@ class GetBookmarkedMovies(private val movieRepository: MovieRepository) {
fun getBookmarkedMovies(): Flow<List<Search>> {
return movieRepository.getBookmarkedMovies()
}

fun getSavedMoviesIds(): Flow<List<String>> {
return movieRepository.getSavedMoviesIds()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,9 @@ import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
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.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
Expand All @@ -37,7 +33,7 @@ fun MovieBookmarkItem(
.padding(12.dp)
.wrapContentSize()
) {
Box() {
Box {
Card(
shape = RoundedCornerShape(12.dp),
backgroundColor = MaterialTheme.colors.background,
Expand Down Expand Up @@ -78,7 +74,7 @@ fun PreviewMovieListItem() {
val (isChecked, setChecked) = remember { mutableStateOf(false) }
AbdaTheme {
MovieBookmarkItem(
Search("123", "fdowbi", "Smishra", "movie", "2021"),
Search("123", "fdowbi", "Smishra", "movie", "2021", false),
onMovieClick = {}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,24 @@ 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.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
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.compose.ui.unit.dp
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,
savedList: List<String>,
onMovieClick: (Search) -> Unit,
onBookmarkClick: (isChecked: Boolean, movie: Search) -> Unit
onBookmarkClick: (movie: Search) -> Unit
) {
when (movies.loadState.refresh) {
LoadState.Loading -> {
Expand Down Expand Up @@ -56,26 +55,54 @@ fun MovieList(
}
}
}*/
LazyColumn(
/*LazyColumn(
state = rememberLazyListState(),
modifier = Modifier.fillMaxSize()
) {
items(movies) { movie ->
if (movie != null) {
val (isChecked, setChecked) = remember { mutableStateOf(false) }
// val (isChecked, setChecked) = remember { mutableStateOf(false) }
if (savedList.contains(movie.imdbID)) movie.isBookmarked = true
MovieListItem(
movie = movie,
onMovieClick = {
// call the movie detail api and trigger the bottom sheet
onMovieClick(movie)
},
isChecked,
onBookmarkClick = {
setChecked(!isChecked)
onBookmarkClick(isChecked, movie)
// setChecked(!isChecked)
movie.toggle()
onBookmarkClick(movie)
}
)
}
}
}*/

LazyVerticalGrid(
modifier = Modifier.fillMaxSize(),
columns = GridCells.Adaptive(minSize = 135.dp)
) {
items(movies.itemCount) { index ->
movies[index].let { movie ->
if (movie != null) {
// val (isChecked, setChecked) = remember { mutableStateOf(false) }
if (savedList.contains(movie.imdbID)) movie.isBookmarked = true
MovieListItem(
movie = movie,
onMovieClick = {
// call the movie detail api and trigger the bottom sheet
onMovieClick(movie)
},
onBookmarkClick = {
// setChecked(!isChecked)
movie.toggle()
onBookmarkClick(movie)
}
)
}
}
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.FavoriteBorder
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.compose.ui.graphics.Color
Expand All @@ -26,7 +24,7 @@ import com.smish.abda.ui.theme.AbdaTheme
fun MovieListItem(
movie: Search,
onMovieClick: (Search) -> Unit,
isChecked: Boolean,
// isChecked: Boolean,
onBookmarkClick: () -> Unit
) {
Surface(
Expand All @@ -39,7 +37,7 @@ fun MovieListItem(
.padding(12.dp)
.wrapContentSize()
) {
Box() {
Box {
Card(
shape = RoundedCornerShape(12.dp),
backgroundColor = MaterialTheme.colors.background,
Expand All @@ -53,18 +51,26 @@ fun MovieListItem(
// Crop, Fit, Inside, FillHeight, FillWidth, None
contentScale = ContentScale.Crop
)
// for preview
/*Box(
modifier = Modifier
.height(150.dp)
.width(135.dp)
.background(color = Color.Cyan)
) {
}*/
}

IconToggleButton(
checked = isChecked, onCheckedChange = { onBookmarkClick() },
checked = movie.isBookmarked, onCheckedChange = { onBookmarkClick() },
modifier = Modifier
.align(Alignment.TopEnd)
.padding(2.dp)
) {
Icon(
imageVector = if (isChecked) Icons.Filled.Favorite else Icons.Filled.FavoriteBorder,
imageVector = if (movie.isBookmarked) Icons.Filled.Favorite else Icons.Filled.FavoriteBorder,
contentDescription = "bookmark",
tint = if (isChecked) Color.Red else Color.White,
tint = if (movie.isBookmarked) Color.Red else Color.White,
modifier = Modifier.size(24.dp)
)
}
Expand All @@ -88,16 +94,22 @@ fun MovieListItem(
}
}

@Preview(showBackground = true, showSystemUi = true)
@Preview(showBackground = true, showSystemUi = false)
@Composable
fun PreviewMovieListItem() {
val (isChecked, setChecked) = remember { mutableStateOf(false) }
AbdaTheme {
// val (isChecked, setChecked) = remember { mutableStateOf(false) }
MovieListItem(
Search("123", "fdowbi", "Smishra", "movie", "2021"),
Search(
"123",
"https://www.google.com/imgres?imgurl=https%3A%2F%2Fmiro.medium.com%2Fmax%2F1024%2F1*zEs8abcQCFrwGeXrVcQ3cg.png&imgrefurl=https%3A%2F%2Fproandroiddev.com%2Fpaging-3-easier-way-to-pagination-part-1-584cad1f4f61&tbnid=Kaq9xtR0nHndYM&vet=12ahUKEwiYibfDv7z8AhW6i9gFHZofA9YQMygAegUIARC7AQ..i&docid=8Dlf-i8q9nZ3nM&w=1024&h=512&q=where%20to%20add%20paging%203%20in%20android&ved=2ahUKEwiYibfDv7z8AhW6i9gFHZofA9YQMygAegUIARC7AQ",
"Smishra",
"movie",
"2021",
false
),
onMovieClick = {},
isChecked,
onBookmarkClick = { setChecked(!isChecked) }
onBookmarkClick = {}
)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.smish.abda.ui.movieList.components

import android.util.Log
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.lazy.LazyRow
Expand All @@ -12,6 +13,7 @@ import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.Close
import androidx.compose.material.icons.filled.Search
import androidx.compose.runtime.*
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand Down Expand Up @@ -57,6 +59,9 @@ fun MovieListScreen(
mutableStateOf(false)
}

val savedList by viewmodel.getSavedMoviesIds().observeAsState()
Log.d("saved", "MovieListScreen: $savedList")

val movieList = viewmodel.getPagingMovies().collectAsLazyPagingItems()
ModalBottomSheetLayout(
sheetState = bottomSheetModalState,
Expand Down Expand Up @@ -123,44 +128,18 @@ fun MovieListScreen(

MovieList(
movies = movieList,
savedList = savedList ?: emptyList(),
onMovieClick = {
showModalSheet.value = !showModalSheet.value
viewmodel.getMovieDetails(it.imdbID)
},
onBookmarkClick = { isChecked, movie ->
if (!isChecked)
onBookmarkClick = { movie ->
if (!movie.isBookmarked)
viewmodel.bookmarkMovie(movie)
else
viewmodel.removeMovie(movie)
}
)

/*LazyVerticalGrid(
modifier = Modifier.fillMaxSize(),
columns = GridCells.Adaptive(minSize = 135.dp)
) {
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)
}
)
}
}
}*/
}

/*if (state.error.isNotBlank()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,10 @@ class MoviesViewmodel @Inject constructor(
emit(it)
}
}

fun getSavedMoviesIds() = liveData {
getBookmarkedMovies.getSavedMoviesIds().collect{
emit(it)
}
}
}

0 comments on commit bda074b

Please sign in to comment.