From 2f193110b76ad02d82f0abc13ef7bd220b780c90 Mon Sep 17 00:00:00 2001 From: Uwais Ashraf Date: Fri, 21 Mar 2025 17:58:54 +0000 Subject: [PATCH] Add debounce to TaskView emissions The emissions of each TaskView on Overview open are usually: 1. task exists 2. icon loaded 3. then thumbnail loaded If the 3 updates above come in quick succession, then ideally the first 2 will be debounced and the last one will be consumed immediately. Debouncing only when thumbnails are not present gives the best results. This is because thumbnail loading is a decent predictor of there being no more emissions to come as they are typically the last emission for a TaskView. Bug: 395294954 Flag: com.android.launcher3.enable_refactor_task_thumbnail Test: ABTD run of failing metric Change-Id: Ib57d6dc7d9f58b7bb37ec1177b23d89360c24c0e --- .../quickstep/recents/ui/viewmodel/TaskViewModel.kt | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/quickstep/src/com/android/quickstep/recents/ui/viewmodel/TaskViewModel.kt b/quickstep/src/com/android/quickstep/recents/ui/viewmodel/TaskViewModel.kt index fac0c0a0bb..9f762e724c 100644 --- a/quickstep/src/com/android/quickstep/recents/ui/viewmodel/TaskViewModel.kt +++ b/quickstep/src/com/android/quickstep/recents/ui/viewmodel/TaskViewModel.kt @@ -34,6 +34,7 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.combine +import kotlinx.coroutines.flow.debounce import kotlinx.coroutines.flow.distinctUntilChanged import kotlinx.coroutines.flow.flatMapLatest import kotlinx.coroutines.flow.flowOn @@ -91,6 +92,16 @@ class TaskViewModel( val state: Flow = combine(taskData, isLiveTile, overlayEnabled, isCentralTask, ::mapToTaskTile) .distinctUntilChanged() + .debounce { state -> + // Debouncing only when thumbnails are not present gives the best results. + // This is because thumbnail loading is a decent predictor of there being no more + // emissions to come as they are typically the last emission for a TaskView. + if (state.tasks.any { (it as? TaskData.Data)?.thumbnailData?.thumbnail == null }) { + DEBOUNCE_DELAY_MS + } else { + 0 + } + } .flowOn(dispatcherProvider.background) fun bind(vararg taskId: TaskId) { @@ -150,5 +161,6 @@ class TaskViewModel( private companion object { const val TAG = "TaskViewModel" + const val DEBOUNCE_DELAY_MS = 16L } }