Skip to content

Commit

Permalink
update doc
Browse files Browse the repository at this point in the history
  • Loading branch information
Tlaster committed Oct 16, 2023
1 parent 4bd924a commit 3caa2db
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 64 deletions.
Original file line number Diff line number Diff line change
@@ -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<ViewGroup>(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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down

0 comments on commit 3caa2db

Please sign in to comment.