Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit f7093d44 authored by Uwais Ashraf's avatar Uwais Ashraf
Browse files

Create scope for TTV attached to run collections that modify UI within.

Bug: 335396935
Test: Manual checking shows clear improvement of preformance problems
Flag: com.android.launcher3.enable_refactor_task_thumbnail
Change-Id: I92aa4ce3edfe297cc5a177a2d93837d33cdfb548
parent fd1cc120
Loading
Loading
Loading
Loading
+25 −13
Original line number Diff line number Diff line
@@ -40,8 +40,13 @@ import com.android.quickstep.views.RecentsView
import com.android.quickstep.views.RecentsViewContainer
import com.android.quickstep.views.TaskView
import com.android.systemui.shared.system.QuickStepContract
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch
import kotlinx.coroutines.CoroutineName
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.SupervisorJob
import kotlinx.coroutines.cancel
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach

class TaskThumbnailView : FrameLayout, ViewPool.Reusable {
    // TODO(b/335649589): Ideally create and obtain this from DI. This ViewModel should be scoped
@@ -58,6 +63,7 @@ class TaskThumbnailView : FrameLayout, ViewPool.Reusable {
            recentsView.mTasksRepository!!,
        )
    }
    private lateinit var viewAttachedScope: CoroutineScope

    private val scrimView: View by lazy { findViewById(R.id.task_thumbnail_scrim) }
    private val liveTileView: LiveTileView by lazy { findViewById(R.id.task_thumbnail_live_tile) }
@@ -87,9 +93,10 @@ class TaskThumbnailView : FrameLayout, ViewPool.Reusable {

    override fun onAttachedToWindow() {
        super.onAttachedToWindow()
        // TODO(b/335396935) replace MainScope with shorter lifecycle.
        MainScope().launch {
            viewModel.uiState.collect { viewModelUiState ->
        viewAttachedScope =
            CoroutineScope(SupervisorJob() + Dispatchers.Main + CoroutineName("TaskThumbnailView"))
        viewModel.uiState
            .onEach { viewModelUiState ->
                resetViews()
                when (viewModelUiState) {
                    is Uninitialized -> {}
@@ -98,20 +105,20 @@ class TaskThumbnailView : FrameLayout, ViewPool.Reusable {
                    is BackgroundOnly -> drawBackground(viewModelUiState.backgroundColor)
                }
            }
        }
        MainScope().launch {
            viewModel.dimProgress.collect { dimProgress ->
            .launchIn(viewAttachedScope)
        viewModel.dimProgress
            .onEach { dimProgress ->
                // TODO(b/348195366) Add fade in/out for scrim
                scrimView.alpha = dimProgress * MAX_SCRIM_ALPHA
            }
        }
        MainScope().launch { viewModel.cornerRadiusProgress.collect { invalidateOutline() } }
        MainScope().launch {
            viewModel.inheritedScale.collect { viewModelInheritedScale ->
            .launchIn(viewAttachedScope)
        viewModel.cornerRadiusProgress.onEach { invalidateOutline() }.launchIn(viewAttachedScope)
        viewModel.inheritedScale
            .onEach { viewModelInheritedScale ->
                inheritedScale = viewModelInheritedScale
                invalidateOutline()
            }
        }
            .launchIn(viewAttachedScope)

        clipToOutline = true
        outlineProvider =
@@ -122,6 +129,11 @@ class TaskThumbnailView : FrameLayout, ViewPool.Reusable {
            }
    }

    override fun onDetachedFromWindow() {
        super.onDetachedFromWindow()
        viewAttachedScope.cancel("TaskThumbnailView detaching from window")
    }

    override fun onRecycle() {
        // Do nothing
    }