Skip to content

Commit

Permalink
Fix incorrect behaviour bug when saving & reading user favourite genres
Browse files Browse the repository at this point in the history
  • Loading branch information
mr3y-the-programmer committed Nov 1, 2023
1 parent 30c076c commit ad3262d
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class DefaultProtoDataStoreMutator(

override suspend fun addGameToFavourites(game: UserFavouriteGame) {
favGamesStore.updateData {
if (game !in it.favGame) {
if (game.id !in it.favGame.map { fGame -> fGame.id }) {
it.copy(favGame = it.favGame + game)
} else {
it
Expand All @@ -52,7 +52,7 @@ class DefaultProtoDataStoreMutator(

override suspend fun removeGameFromFavourites(game: UserFavouriteGame) {
favGamesStore.updateData {
it.copy(favGame = it.favGame - game)
it.copy(favGame = it.favGame - it.favGame.filter { it.id == game.id }.toSet())
}
}

Expand All @@ -68,7 +68,7 @@ class DefaultProtoDataStoreMutator(

override suspend fun removeGenreFromFavourites(genre: UserFavouriteGenre) {
favGenresStore.updateData {
it.copy(favGenre = it.favGenre - genre)
it.copy(favGenre = it.favGenre - it.favGenre.filter { it.id == genre.id }.toSet())
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ fun SelectingFavouriteGamesPage(
contentType = games.itemContentType { it }
) { index ->
val game = games[index]
val isFavGame = game?.let { FavouriteGame(it.id, it.name, it.imageUrl, it.rating) } in favouriteUserGames
val isFavGame = game?.id in favouriteUserGames.map { it.id }
if (!isFavGame) {
GameTile(
game = game,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ fun GenresPage(
horizontalArrangement = Arrangement.spacedBy(8.dp)
) {
allGenres.data.forEach { genre ->
val isSelected = genre.id in selectedGenres.map { it.id }
Row(
modifier = Modifier
.padding(vertical = 8.dp)
Expand All @@ -118,16 +119,16 @@ fun GenresPage(
)
.clip(RoundedCornerShape(8.dp))
.conditionalBackground(
predicate = genre in selectedGenres,
predicate = isSelected,
color = MaterialTheme.colorScheme.primary.copy(alpha = 0.2f)
)
.selectable(
selected = genre in selectedGenres,
selected = isSelected,
interactionSource = remember { MutableInteractionSource() },
indication = null,
role = Role.Checkbox,
onClick = {
if (genre in selectedGenres) {
if (isSelected) {
onUnselectingGenre(genre)
} else {
onSelectingGenre(genre)
Expand All @@ -137,9 +138,9 @@ fun GenresPage(
.animateContentSize()
.padding(8.dp)
.clearAndSetSemantics {
val stateDesc = if (genre in selectedGenres) strings.genres_page_genre_on_state_desc(genre.name) else strings.genres_page_genre_off_state_desc(genre.name)
val stateDesc = if (isSelected) strings.genres_page_genre_on_state_desc(genre.name) else strings.genres_page_genre_off_state_desc(genre.name)
stateDescription = stateDesc
selected = genre in selectedGenres
selected = isSelected
role = Role.Checkbox
},
verticalAlignment = Alignment.CenterVertically,
Expand All @@ -157,7 +158,7 @@ fun GenresPage(
modifier = Modifier.width(IntrinsicSize.Min),
textAlign = TextAlign.Center
)
if (genre in selectedGenres) {
if (isSelected) {
Icon(
imageVector = Icons.Filled.Check,
contentDescription = null,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,14 @@ fun GenresList(
val genres = state.allGenres.data
val strings = LocalStrings.current
genres.forEachIndexed { index, gameGenre ->
val isSelected = gameGenre.id in state.favouriteGenres.map { it.id }
Row(
modifier = modifier
.fillMaxWidth()
.selectable(
gameGenre in state.favouriteGenres,
isSelected,
onClick = {
if (gameGenre in state.favouriteGenres) {
if (isSelected) {
onRemovingGenreFromFavourites(gameGenre)
} else {
onAddingGenreToFavourites(gameGenre)
Expand All @@ -334,12 +335,12 @@ fun GenresList(
.padding(8.dp)
.clearAndSetSemantics {
stateDescription =
if (gameGenre in state.favouriteGenres) {
if (isSelected) {
strings.edit_preferences_page_genre_on_state_desc(gameGenre.name)
} else {
strings.edit_preferences_page_genre_off_state_desc(gameGenre.name)
}
selected = gameGenre in state.favouriteGenres
selected = isSelected
},
horizontalArrangement = Arrangement.SpaceBetween,
verticalAlignment = Alignment.CenterVertically
Expand All @@ -351,7 +352,7 @@ fun GenresList(
style = MaterialTheme.typography.titleLarge
)
Checkbox(
checked = gameGenre in state.favouriteGenres,
checked = isSelected,
onCheckedChange = null
)
}
Expand Down

0 comments on commit ad3262d

Please sign in to comment.