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

Commit 2b7a2dcc authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Cleanup LastClock references when detached" into main

parents 28d89f58 21efc2c8
Loading
Loading
Loading
Loading
+30 −23
Original line number Original line Diff line number Diff line
@@ -48,8 +48,6 @@ import kotlinx.coroutines.flow.map


object KeyguardClockViewBinder {
object KeyguardClockViewBinder {
    private val TAG = KeyguardClockViewBinder::class.simpleName!!
    private val TAG = KeyguardClockViewBinder::class.simpleName!!
    // When changing to new clock, we need to remove old clock views from burnInLayer
    private var lastClock: ClockController? = null


    @JvmStatic
    @JvmStatic
    fun bind(
    fun bind(
@@ -72,10 +70,20 @@ object KeyguardClockViewBinder {
        disposables +=
        disposables +=
            keyguardRootView.repeatWhenAttached {
            keyguardRootView.repeatWhenAttached {
                repeatOnLifecycle(Lifecycle.State.CREATED) {
                repeatOnLifecycle(Lifecycle.State.CREATED) {
                    // When changing to new clock, we need to remove old views from burnInLayer
                    var lastClock: ClockController? = null
                    launch {
                    launch {
                            if (!MigrateClocksToBlueprint.isEnabled) return@launch
                            if (!MigrateClocksToBlueprint.isEnabled) return@launch
                            viewModel.currentClock.collect { currentClock ->
                            viewModel.currentClock.collect { currentClock ->
                            cleanupClockViews(currentClock, keyguardRootView, viewModel.burnInLayer)
                                if (lastClock != currentClock) {
                                    cleanupClockViews(
                                        lastClock,
                                        keyguardRootView,
                                        viewModel.burnInLayer,
                                    )
                                    lastClock = currentClock
                                }

                                addClockViews(currentClock, keyguardRootView)
                                addClockViews(currentClock, keyguardRootView)
                                updateBurnInLayer(
                                updateBurnInLayer(
                                    keyguardRootView,
                                    keyguardRootView,
@@ -85,6 +93,10 @@ object KeyguardClockViewBinder {
                                applyConstraints(clockSection, keyguardRootView, true)
                                applyConstraints(clockSection, keyguardRootView, true)
                            }
                            }
                        }
                        }
                        .invokeOnCompletion {
                            cleanupClockViews(lastClock, keyguardRootView, viewModel.burnInLayer)
                            lastClock = null
                        }


                    launch {
                    launch {
                        if (!MigrateClocksToBlueprint.isEnabled) return@launch
                        if (!MigrateClocksToBlueprint.isEnabled) return@launch
@@ -185,23 +197,18 @@ object KeyguardClockViewBinder {
        viewModel.burnInLayer?.updatePostLayout(keyguardRootView)
        viewModel.burnInLayer?.updatePostLayout(keyguardRootView)
    }
    }


    private fun cleanupClockViews(
    fun cleanupClockViews(
        currentClock: ClockController?,
        lastClock: ClockController?,
        rootView: ConstraintLayout,
        rootView: ConstraintLayout,
        burnInLayer: Layer?,
        burnInLayer: Layer?,
    ) {
    ) {
        if (lastClock == currentClock) {
        lastClock?.run {
            return
            smallClock.layout.views.forEach {
        }

        lastClock?.let { clock ->
            clock.smallClock.layout.views.forEach {
                burnInLayer?.removeView(it)
                burnInLayer?.removeView(it)
                rootView.removeView(it)
                rootView.removeView(it)
            }
            }
            clock.largeClock.layout.views.forEach { rootView.removeView(it) }
            largeClock.layout.views.forEach { rootView.removeView(it) }
        }
        }
        lastClock = currentClock
    }
    }


    @VisibleForTesting
    @VisibleForTesting
+1 −0
Original line number Original line Diff line number Diff line
@@ -123,6 +123,7 @@ object KeyguardPreviewClockViewBinder {
                                )
                                )
                            }
                            }
                        }
                        }
                        lastClock = null
                    }
                    }
            }
            }
        }
        }