Skip to content

Commit a02d615

Browse files
Kaavehclaude
andcommitted
#291 Fix favorite FAB not updating in market detail screen
Three fixes combined: - ViewModel: toggleFavoriteState() was setting isFavorite = isFavorite instead of !isFavorite, so StateFlow never emitted a changed value - Screen: move onFavoriteClick to FloatingActionButton's onClick so the FAB owns the tap event - FavoriteIcon: make onFavoriteClick nullable and skip adding .clickable when null, so the icon no longer silently consumes taps that should reach the parent FAB Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent f312746 commit a02d615

File tree

3 files changed

+7
-12
lines changed

3 files changed

+7
-12
lines changed

feature/marketdetail/src/main/java/ir/composenews/marketdetail/MarketDetailScreen.kt

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,9 @@ private fun MarketDetailFloatingActionButton(
113113
loaded = { data ->
114114
FloatingActionButton(
115115
modifier = modifier,
116-
onClick = {},
116+
onClick = { onFavoriteClick(data) },
117117
) {
118-
FavoriteIcon(isFavorite = data.isFavorite) {
119-
onFavoriteClick(data)
120-
}
118+
FavoriteIcon(isFavorite = data.isFavorite)
121119
}
122120
},
123121
error = { error -> },

feature/marketdetail/src/main/java/ir/composenews/marketdetail/MarketDetailViewModel.kt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ class MarketDetailViewModel @Inject constructor(
123123

124124
private fun toggleFavoriteState() {
125125
val market = (mutableState.value.market as LoadableData.Loaded).data
126-
val isFavorite = market.isFavorite
127-
val newMarket = LoadableData.Loaded(market.copy(isFavorite = isFavorite))
126+
val newMarket = LoadableData.Loaded(market.copy(isFavorite = !market.isFavorite))
128127
mutableState.update { it.copy(market = newMarket) }
129128
}
130129
}

library/designsystem/src/main/java/ir/composenews/designsystem/component/FavoriteIcon.kt

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import ir.composenews.designsystem.theme.ComposeNewsTheme
3232
@Composable
3333
fun FavoriteIcon(
3434
isFavorite: Boolean,
35-
onFavoriteClick: () -> Unit,
35+
onFavoriteClick: (() -> Unit)? = null,
3636
) {
3737
val animationState by remember(isFavorite) { mutableStateOf(isFavorite) }
3838

@@ -71,9 +71,7 @@ fun FavoriteIcon(
7171
scaleY = scale
7272
}
7373
.clip(CircleShape)
74-
.clickable {
75-
onFavoriteClick()
76-
}
74+
.then(if (onFavoriteClick != null) Modifier.clickable { onFavoriteClick() } else Modifier)
7775
.padding(8.dp),
7876
)
7977
}
@@ -84,8 +82,8 @@ private fun FavoriteIconPrev() {
8482
ComposeNewsTheme {
8583
Surface {
8684
Column {
87-
FavoriteIcon(isFavorite = true) {}
88-
FavoriteIcon(isFavorite = false) {}
85+
FavoriteIcon(isFavorite = true)
86+
FavoriteIcon(isFavorite = false)
8987
}
9088
}
9189
}

0 commit comments

Comments
 (0)