Fixed recomposition logic when toggling favorites filter

This commit is contained in:
Sad Ellie 2023-04-05 21:57:29 +03:00
parent 89036b6ea6
commit c5e0291789
2 changed files with 7 additions and 6 deletions

View File

@ -64,7 +64,7 @@ import com.sadellie.unitto.feature.unitslist.components.UnitListItem
@Composable @Composable
internal fun LeftSideScreen( internal fun LeftSideScreen(
viewModel: UnitsListViewModel, viewModel: UnitsListViewModel,
currentUnitId: String, currentUnitId: String?,
navigateUp: () -> Unit, navigateUp: () -> Unit,
navigateToSettingsAction: () -> Unit, navigateToSettingsAction: () -> Unit,
selectAction: (AbstractUnit) -> Unit selectAction: (AbstractUnit) -> Unit
@ -142,8 +142,11 @@ internal fun LeftSideScreen(
} }
} }
// This block is called only once on initial composition LaunchedEffect(uiState.value.shownUnitGroups, currentUnitId) {
LaunchedEffect(uiState.value.shownUnitGroups) { if (currentUnitId == null) return@LaunchedEffect
// This is still wrong, but works good enough.
// Ideally we shouldn't use uiState.value.shownUnitGroups
viewModel.setSelectedChip(currentUnitId)
val groupToSelect = uiState.value.shownUnitGroups.indexOf(uiState.value.chosenUnitGroup) val groupToSelect = uiState.value.shownUnitGroups.indexOf(uiState.value.chosenUnitGroup)
if (groupToSelect > -1) { if (groupToSelect > -1) {
chipsRowLazyListState.animateScrollToItem(groupToSelect) chipsRowLazyListState.animateScrollToItem(groupToSelect)

View File

@ -50,11 +50,9 @@ fun NavGraphBuilder.leftScreen(
composable( composable(
route = "$leftSideRoute/{$unitFromIdArg}" route = "$leftSideRoute/{$unitFromIdArg}"
) { ) {
val unitFromId = it.arguments?.getString(unitFromIdArg) ?: return@composable
viewModel.setSelectedChip(unitFromId)
LeftSideScreen( LeftSideScreen(
viewModel = viewModel, viewModel = viewModel,
currentUnitId = unitFromId, currentUnitId = it.arguments?.getString(unitFromIdArg),
navigateUp = navigateUp, navigateUp = navigateUp,
navigateToSettingsAction = navigateToUnitGroups, navigateToSettingsAction = navigateToUnitGroups,
selectAction = onSelect selectAction = onSelect