Skip to content

Commit

Permalink
Save bookmark state on recomposition
Browse files Browse the repository at this point in the history
  • Loading branch information
smish-hash committed Jan 10, 2023
1 parent 7ef3880 commit a0a3c8e
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 61 deletions.
11 changes: 9 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
Expand Up @@ -23,8 +23,15 @@ data class Search(
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 @@ -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,26 +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.rememberLazyListState
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>,
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 @@ -57,28 +55,54 @@ fun MovieList(
}
}
}*/
LazyColumn(
/*LazyColumn(
state = rememberLazyListState(),
modifier = Modifier.fillMaxSize()
) {
items(movies) { movie ->
if (movie != null) {
val (isChecked, setChecked) = remember { mutableStateOf(false) }
if (savedList.contains(movie.imdbID)) setChecked(true) else setChecked(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 @@ -64,15 +62,15 @@ fun MovieListItem(
}

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 Down Expand Up @@ -100,16 +98,18 @@ fun MovieListItem(
@Composable
fun PreviewMovieListItem() {
AbdaTheme {
val (isChecked, setChecked) = remember { mutableStateOf(false) }
// val (isChecked, setChecked) = remember { mutableStateOf(false) }
MovieListItem(
Search("123",
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"),
"2021",
false
),
onMovieClick = {},
isChecked,
onBookmarkClick = {setChecked(!isChecked)}
onBookmarkClick = {}
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,40 +133,13 @@ fun MovieListScreen(
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

0 comments on commit a0a3c8e

Please sign in to comment.