launcher: fix lost update when grid setters run back-to-back
- Commit
19e38a5a1dd3949cc2dea6ccc1a576e17c17f9cb- Author
- Vayun Mathur <[email protected]>
- When
- 2026-11-07 08:03 UTC (29 days ago)
- Parents
04fa154
changeGridImpl and changeGridForSettings computed the new grid from homeState.value.grid before launching their coroutine, then published that whole snapshot. Two setters called in quick succession (the settings screen's rows and hotseat-slots pickers, or any caller doing both) each snapshotted the same grid, so whichever coroutine landed last silently undid the other's change - the grid could end up with the old row count or the old hotseat size depending on scheduling. Apply the transform inside the launch, against the then-current grid, so sequential setters compose. Found via the intermittently failing gridSettersRenumber device test, which only passed when the main looper happened to run the first coroutine before the second setter was invoked. (cherry picked from commit 418a8dd1cc037fe96a736c91321ed6f6857304ec)
Diff
launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherActionOps.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherActionOps.kt b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherActionOps.ktindex bd9993cfc0..e61909b919 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherActionOps.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherActionOps.kt@@ -154,8 +154,10 @@ internal object LauncherIconOpsHolder : IconLoader { /** One-line grid mutation shared by the three grid setters. */ internal fun LauncherViewModel.changeGridForSettings(transform: (GridSpec) -> GridSpec) {- val next = transform(homeState.value.grid)+ // See changeGridImpl: the transform must read the grid inside the+ // launch, or back-to-back setters lose each other's changes. viewModelScope.launch {+ val next = transform(homeState.value.grid) repository.regrid(next) homeState.value = homeState.value.copy(grid = next) settingsState.value = settingsState.value.copy(
launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherMenuOps.kt
diff --git a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherMenuOps.kt b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherMenuOps.ktindex cbd9f29247..af4815eacb 100644--- a/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherMenuOps.kt+++ b/launcher/src/main/java/com/vayunmathur/launcher/platform/LauncherMenuOps.kt@@ -125,8 +125,13 @@ internal fun LauncherViewModel.setHotseatSlotsImpl(slots: Int) = changeGridImpl * hanging off the edge. */ internal fun LauncherViewModel.changeGridImpl(transform: (GridSpec) -> GridSpec) {- val next = transform(homeState.value.grid)+ // The transform is applied inside the launch, against the then-current+ // grid: computing it here would snapshot the grid before any earlier+ // setter's coroutine lands, so two setters in a row (rows, then hotseat+ // slots) would each publish a whole grid and the second would silently+ // undo the first. viewModelScope.launch {+ val next = transform(homeState.value.grid) repository.regrid(next) homeState.value = homeState.value.copy(grid = next) settingsState.value = settingsState.value.copy(