Extract MyLocations Widget to a separate function

This commit is contained in:
Nebojsa Vuksic 2025-07-31 10:45:24 +02:00
parent 3dcefe6fed
commit 54e147242e

View File

@ -66,8 +66,6 @@ private fun LeftColumn(
myLocationsViewModelApi: MyLocationsViewModelApi, myLocationsViewModelApi: MyLocationsViewModelApi,
modifier: Modifier = Modifier, modifier: Modifier = Modifier,
) { ) {
val myLocations = myLocationsViewModelApi.myLocationsFlow.collectAsState(emptyList()).value
Column(modifier) { Column(modifier) {
GroupHeader( GroupHeader(
ComposeTemplateBundle.message("weather.app.my.locations.header.text"), ComposeTemplateBundle.message("weather.app.my.locations.header.text"),
@ -78,46 +76,52 @@ private fun LeftColumn(
Spacer(modifier = Modifier.height(10.dp)) Spacer(modifier = Modifier.height(10.dp))
val listState = rememberSelectableLazyListState() MyLocationsList(Modifier.fillMaxSize(), myLocationsViewModelApi)
}
}
// JEWEL-938 This will trigger on SelectableLazyColum's `onSelectedIndexesChange` callback @Composable
LaunchedEffect(myLocations) { internal fun MyLocationsList(modifier: Modifier = Modifier, myLocationsViewModelApi: MyLocationsViewModelApi) {
var lastActiveItemIndex = -1 val myLocations = myLocationsViewModelApi.myLocationsFlow.collectAsState(emptyList()).value
val selectedItemKeys = mutableSetOf<String>()
myLocations.forEachIndexed { index, location -> val listState = rememberSelectableLazyListState()
if (location.isSelected) { // JEWEL-938 This will trigger on SelectableLazyColum's `onSelectedIndexesChange` callback
if (lastActiveItemIndex == -1) { LaunchedEffect(myLocations) {
// Only the first selected item should be active var lastActiveItemIndex = -1
lastActiveItemIndex = index val selectedItemKeys = mutableSetOf<String>()
} myLocations.forEachIndexed { index, location ->
// Must match the key used in the `items()` call's `key` parameter to ensure correct item identity. if (location.isSelected) {
selectedItemKeys.add(location.location.label) if (lastActiveItemIndex == -1) {
// Only the first selected item should be active
lastActiveItemIndex = index
} }
// Must match the key used in the `items()` call's `key` parameter to ensure correct item identity.
selectedItemKeys.add(location.location.label)
} }
// Sets the first selected item as an active item to avoid triggering on click event when user clocks on it
listState.lastActiveItemIndex = lastActiveItemIndex
// Sets keys of selected items
listState.selectedKeys = selectedItemKeys
} }
// Sets the first selected item as an active item to avoid triggering on click event when user clocks on it
listState.lastActiveItemIndex = lastActiveItemIndex
// Sets keys of selected items
listState.selectedKeys = selectedItemKeys
}
SelectableLazyColumn( SelectableLazyColumn(
modifier = Modifier.fillMaxSize(), modifier = modifier,
selectionMode = SelectionMode.Single, selectionMode = SelectionMode.Single,
state = listState, state = listState,
onSelectedIndexesChange = { indices -> onSelectedIndexesChange = { indices ->
val selectedLocationIndex = indices.firstOrNull() ?: return@SelectableLazyColumn val selectedLocationIndex = indices.firstOrNull() ?: return@SelectableLazyColumn
myLocationsViewModelApi.onLocationSelected(selectedLocationIndex) myLocationsViewModelApi.onLocationSelected(selectedLocationIndex)
}, },
) { ) {
items( items(
items = myLocations, items = myLocations,
key = { item -> item.location.label }, key = { item -> item.location.label },
) { item -> ) { item ->
ContentItemRow( ContentItemRow(
item = item.location, isSelected = item.isSelected, isActive = isActive item = item.location, isSelected = item.isSelected, isActive = isActive
) )
}
} }
} }
} }