diff --git a/precompose/src/androidMain/kotlin/moe/tlaster/precompose/lifecycle/PreComposeActivity.kt b/precompose/src/androidMain/kotlin/moe/tlaster/precompose/lifecycle/PreComposeActivity.kt index ac0df901..fdc7c59f 100644 --- a/precompose/src/androidMain/kotlin/moe/tlaster/precompose/lifecycle/PreComposeActivity.kt +++ b/precompose/src/androidMain/kotlin/moe/tlaster/precompose/lifecycle/PreComposeActivity.kt @@ -1,79 +1,36 @@ package moe.tlaster.precompose.lifecycle -import android.view.ViewGroup import androidx.activity.ComponentActivity +import androidx.activity.compose.setContent import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionContext -import androidx.compose.ui.platform.ComposeView -import androidx.lifecycle.findViewTreeLifecycleOwner -import androidx.lifecycle.findViewTreeViewModelStoreOwner -import androidx.lifecycle.setViewTreeLifecycleOwner -import androidx.lifecycle.setViewTreeViewModelStoreOwner -import androidx.savedstate.findViewTreeSavedStateRegistryOwner -import androidx.savedstate.setViewTreeSavedStateRegistryOwner import moe.tlaster.precompose.PreComposeApp @Deprecated( - message = "Use PreComposeApp instead", - replaceWith = ReplaceWith("PreComposeApp(content)"), + message = """ + Use ComponentActivity directly instead. And make sure wrap your content with PreComposeApp. + PreComposeActivity will be removed in the future release. + For migration guide, please refer to https://github.com/Tlaster/PreCompose/releases/tag/1.5.5 + """, + replaceWith = ReplaceWith("ComponentActivity"), ) typealias PreComposeActivity = ComponentActivity @Deprecated( - message = "Use PreComposeApp instead", - replaceWith = ReplaceWith("PreComposeApp(content)"), + message = """ + Use androidx.activity.compose.setContent directly instead. And make sure wrap your content with PreComposeApp. + PreComposeActivity.setContent will be removed in the future release. + For migration guide, please refer to https://github.com/Tlaster/PreCompose/releases/tag/1.5.5 + """, + replaceWith = ReplaceWith("androidx.activity.compose.setContent"), ) -fun ComponentActivity.setContent( +fun PreComposeActivity.setContent( parent: CompositionContext? = null, content: @Composable () -> Unit, ) { - val existingComposeView = - window.decorView.findViewById(android.R.id.content).getChildAt(0) as? ComposeView - - if (existingComposeView != null) { - with(existingComposeView) { - setParentCompositionContext(parent) - setContent { - ContentInternal(content) - } - } - } else { - ComposeView(this).apply { - // Set content and parent **before** setContentView - // to have ComposeView create the composition on attach - setParentCompositionContext(parent) - setContent { - ContentInternal(content) - } - // Set the view tree owners before setting the content view so that the inflation process - // and attach listeners will see them already present - setOwners() - setContentView(this, DefaultActivityContentLayoutParams) + setContent(parent) { + PreComposeApp { + content.invoke() } } } - -private fun ComponentActivity.setOwners() { - val decorView = window.decorView - if (decorView.findViewTreeLifecycleOwner() == null) { - decorView.setViewTreeLifecycleOwner(this) - } - if (decorView.findViewTreeViewModelStoreOwner() == null) { - decorView.setViewTreeViewModelStoreOwner(this) - } - if (decorView.findViewTreeSavedStateRegistryOwner() == null) { - decorView.setViewTreeSavedStateRegistryOwner(this) - } -} - -@Composable -private fun ComponentActivity.ContentInternal(content: @Composable () -> Unit) { - PreComposeApp { - content.invoke() - } -} - -private val DefaultActivityContentLayoutParams = ViewGroup.LayoutParams( - ViewGroup.LayoutParams.WRAP_CONTENT, - ViewGroup.LayoutParams.WRAP_CONTENT, -) diff --git a/precompose/src/iosMain/kotlin/moe/tlaster/precompose/PreComposeApplication.kt b/precompose/src/iosMain/kotlin/moe/tlaster/precompose/PreComposeApplication.kt index ba0c0610..19de622d 100644 --- a/precompose/src/iosMain/kotlin/moe/tlaster/precompose/PreComposeApplication.kt +++ b/precompose/src/iosMain/kotlin/moe/tlaster/precompose/PreComposeApplication.kt @@ -27,8 +27,12 @@ import platform.UIKit.UIViewController @Suppress("FunctionName") @Deprecated( - message = "Use PreComposeApp instead", - replaceWith = ReplaceWith("PreComposeApp(content)"), + message = """ + Use ComposeUIViewController directly instead. And make sure wrap your content with PreComposeApp. + PreComposeApplication will be removed in the future release. + For migration guide, please refer to https://github.com/Tlaster/PreCompose/releases/tag/1.5.5 + """, + replaceWith = ReplaceWith("ComposeUIViewController"), ) fun PreComposeApplication( configure: ComposeUIViewControllerConfiguration.() -> Unit = {}, diff --git a/precompose/src/jvmMain/kotlin/moe/tlaster/precompose/PreComposeWindow.kt b/precompose/src/jvmMain/kotlin/moe/tlaster/precompose/PreComposeWindow.kt index b01149e0..167d0ec4 100644 --- a/precompose/src/jvmMain/kotlin/moe/tlaster/precompose/PreComposeWindow.kt +++ b/precompose/src/jvmMain/kotlin/moe/tlaster/precompose/PreComposeWindow.kt @@ -23,8 +23,12 @@ import java.awt.event.WindowAdapter import java.awt.event.WindowEvent @Deprecated( - message = "Use PreComposeApp instead", - replaceWith = ReplaceWith("PreComposeApp(content)"), + message = """ + Use Window directly instead. And make sure wrap your content with PreComposeApp. + PreComposeWindow will be removed in the future release. + For migration guide, please refer to https://github.com/Tlaster/PreCompose/releases/tag/1.5.5 + """, + replaceWith = ReplaceWith("PreComposeWindow"), ) @Composable fun PreComposeWindow( diff --git a/precompose/src/macosMain/kotlin/moe/tlaster/precompose/PreComposeWindow.kt b/precompose/src/macosMain/kotlin/moe/tlaster/precompose/PreComposeWindow.kt index d225643d..60293b65 100644 --- a/precompose/src/macosMain/kotlin/moe/tlaster/precompose/PreComposeWindow.kt +++ b/precompose/src/macosMain/kotlin/moe/tlaster/precompose/PreComposeWindow.kt @@ -2,6 +2,7 @@ package moe.tlaster.precompose import androidx.compose.runtime.Composable import androidx.compose.runtime.CompositionLocalProvider +import androidx.compose.runtime.ExperimentalComposeApi import androidx.compose.runtime.remember import moe.tlaster.precompose.lifecycle.LifecycleOwner import moe.tlaster.precompose.lifecycle.LifecycleRegistry @@ -12,6 +13,7 @@ import moe.tlaster.precompose.ui.BackDispatcher import moe.tlaster.precompose.ui.BackDispatcherOwner import moe.tlaster.precompose.ui.LocalBackDispatcherOwner +@ExperimentalComposeApi fun PreComposeWindow( title: String, hideTitleBar: Boolean = false,