mirror of
https://github.com/JetBrains/intellij-platform-plugin-template.git
synced 2025-12-05 06:11:52 +00:00
Fix: Preselecting location in MyLocations list
This commit is contained in:
parent
e9188b61cb
commit
3dcefe6fed
@ -84,6 +84,8 @@ internal class MyLocationsViewModel(cs: CoroutineScope) : MyLocationsViewModelAp
|
|||||||
}
|
}
|
||||||
|
|
||||||
override fun onLocationSelected(selectedLocationIndex: Int) {
|
override fun onLocationSelected(selectedLocationIndex: Int) {
|
||||||
|
if (this.selectedLocationIndex.value == selectedLocationIndex) return
|
||||||
|
|
||||||
this.selectedLocationIndex.value = selectedLocationIndex
|
this.selectedLocationIndex.value = selectedLocationIndex
|
||||||
|
|
||||||
onReloadWeatherForecast()
|
onReloadWeatherForecast()
|
||||||
|
|||||||
@ -80,12 +80,24 @@ private fun LeftColumn(
|
|||||||
|
|
||||||
val listState = rememberSelectableLazyListState()
|
val listState = rememberSelectableLazyListState()
|
||||||
|
|
||||||
// TODO Check why preselection isn't working
|
// JEWEL-938 This will trigger on SelectableLazyColum's `onSelectedIndexesChange` callback
|
||||||
LaunchedEffect(myLocations) {
|
LaunchedEffect(myLocations) {
|
||||||
listState
|
var lastActiveItemIndex = -1
|
||||||
.selectedKeys = myLocations
|
val selectedItemKeys = mutableSetOf<String>()
|
||||||
.mapIndexedNotNull { index, item -> if (item.isSelected) index else null }
|
myLocations.forEachIndexed { index, location ->
|
||||||
.toSet()
|
if (location.isSelected) {
|
||||||
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
SelectableLazyColumn(
|
SelectableLazyColumn(
|
||||||
@ -99,7 +111,7 @@ private fun LeftColumn(
|
|||||||
) {
|
) {
|
||||||
items(
|
items(
|
||||||
items = myLocations,
|
items = myLocations,
|
||||||
key = { item -> item },
|
key = { item -> item.location.label },
|
||||||
) { item ->
|
) { item ->
|
||||||
|
|
||||||
ContentItemRow(
|
ContentItemRow(
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user