Fix ComposeInitializerImpl to work with new lifecycle libraries

Change-Id: Ie142cac242f2aca3793ed3b522919c79b4025c18
Merged-In: Ie142cac242f2aca3793ed3b522919c79b4025c18
Bug: 274722427
Test: SYSTEMUI_USE_COMPOSE=true m SystemUICompose
This commit is contained in:
Jordan Demeulenaere
2023-05-10 14:49:11 +02:00
parent 01a9c09f67
commit 52d705a8c4

View File

@@ -17,9 +17,8 @@
package com.android.systemui.compose
import android.view.View
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewTreeLifecycleOwner
import androidx.savedstate.SavedStateRegistry
import androidx.lifecycle.findViewTreeLifecycleOwner
import androidx.lifecycle.setViewTreeLifecycleOwner
import androidx.savedstate.SavedStateRegistryController
import androidx.savedstate.SavedStateRegistryOwner
import com.android.compose.animation.ViewTreeSavedStateRegistryOwner
@@ -27,7 +26,7 @@ import com.android.systemui.lifecycle.ViewLifecycleOwner
internal object ComposeInitializerImpl : ComposeInitializer {
override fun onAttachedToWindow(root: View) {
if (ViewTreeLifecycleOwner.get(root) != null) {
if (root.findViewTreeLifecycleOwner() != null) {
error("root $root already has a LifecycleOwner")
}
@@ -54,7 +53,7 @@ internal object ComposeInitializerImpl : ComposeInitializer {
override val savedStateRegistry = savedStateRegistryController.savedStateRegistry
override fun getLifecycle(): Lifecycle = lifecycleOwner.lifecycle
override val lifecycle = lifecycleOwner.lifecycle
}
// We must call [ViewLifecycleOwner.onCreate] after creating the [SavedStateRegistryOwner]
@@ -64,13 +63,13 @@ internal object ComposeInitializerImpl : ComposeInitializer {
// Set the owners on the root. They will be reused by any ComposeView inside the root
// hierarchy.
ViewTreeLifecycleOwner.set(root, lifecycleOwner)
root.setViewTreeLifecycleOwner(lifecycleOwner)
ViewTreeSavedStateRegistryOwner.set(root, savedStateRegistryOwner)
}
override fun onDetachedFromWindow(root: View) {
(ViewTreeLifecycleOwner.get(root) as ViewLifecycleOwner).onDestroy()
ViewTreeLifecycleOwner.set(root, null)
(root.findViewTreeLifecycleOwner() as ViewLifecycleOwner).onDestroy()
root.setViewTreeLifecycleOwner(null)
ViewTreeSavedStateRegistryOwner.set(root, null)
}
}