in src/main/kotlin/org/jetbrains/plugins/template/weatherApp/ui/WeatherAppViewModel.kt [140:156]
fun withLocationDeleted(locationToRemove: Location): LocationsUIState {
val indexToDelete = locations.indexOf(locationToRemove)
if (indexToDelete < 0) return this // Location not found
val newLocations = locations - locationToRemove
val newSelectedIndex = when {
newLocations.isEmpty() -> -1
indexToDelete <= selectedIndex -> (selectedIndex - 1).coerceIn(0, newLocations.lastIndex)
else -> selectedIndex // Deleted item after selected, no change
}
return LocationsUIState(
locations = newLocations,
selectedIndex = newSelectedIndex
)
}