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 Diff line number Diff line
@@ -48,8 +48,6 @@ import kotlinx.coroutines.flow.map

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

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

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

    private fun cleanupClockViews(
        currentClock: ClockController?,
    fun cleanupClockViews(
        lastClock: ClockController?,
        rootView: ConstraintLayout,
        burnInLayer: Layer?,
    ) {
        if (lastClock == currentClock) {
            return
        }

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

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