Skip to content

Commit

Permalink
Merge pull request #159 from Lavmee/refactor-activity
Browse files Browse the repository at this point in the history
Refactor Android PreComposeActivity to ComponentAcitivity extensions
  • Loading branch information
Tlaster authored Oct 16, 2023
2 parents 3ef948a + e224ee7 commit bc13c2f
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 44 deletions.
2 changes: 1 addition & 1 deletion docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ api("moe.tlaster:precompose:$precompose_version")
// api("moe.tlaster:precompose-koin:$precompose_version") // For Koin intergration
```
## Android
Change the Activity's parent class to `moe.tlaster.precompose.lifecycle.PreComposeActivity` and use `moe.tlaster.precompose.lifecycle.setContent` for setting compose content
Change the `androidx.activity.compose.setContent` to `moe.tlaster.precompose.lifecycle.setContent`

## Desktop (JVM)
Change the `Window` to `moe.tlaster.precompose.PreComposeWindow`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
package moe.tlaster.precompose.lifecycle

import android.os.Bundle
import android.view.ViewGroup
import androidx.activity.ComponentActivity
import androidx.activity.viewModels
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionContext
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.saveable.LocalSaveableStateRegistry
import androidx.compose.ui.platform.ComposeView
import androidx.fragment.app.FragmentActivity
import androidx.lifecycle.DefaultLifecycleObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.findViewTreeLifecycleOwner
import androidx.lifecycle.findViewTreeViewModelStoreOwner
import androidx.lifecycle.setViewTreeLifecycleOwner
Expand All @@ -24,38 +26,12 @@ import moe.tlaster.precompose.stateholder.LocalStateHolder
import moe.tlaster.precompose.stateholder.SavedStateHolder
import moe.tlaster.precompose.ui.LocalBackDispatcherOwner

open class PreComposeActivity : FragmentActivity() {
internal val viewModel by viewModels<PreComposeViewModel>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
onBackPressedDispatcher.addCallback(this, viewModel.backPressedCallback)
}

override fun onResume() {
super.onResume()
viewModel.lifecycleRegistry.currentState = Lifecycle.State.Active
}

override fun onPause() {
super.onPause()
viewModel.lifecycleRegistry.currentState = Lifecycle.State.InActive
}

override fun onDestroy() {
super.onDestroy()
if (!isChangingConfigurations) {
viewModel.lifecycleRegistry.currentState = Lifecycle.State.Destroyed
}
}
}

fun PreComposeActivity.setContent(
fun ComponentActivity.setContent(
parent: CompositionContext? = null,
content: @Composable () -> Unit,
) {
val existingComposeView = window.decorView
.findViewById<ViewGroup>(android.R.id.content)
.getChildAt(0) as? ComposeView
val existingComposeView =
window.decorView.findViewById<ViewGroup>(android.R.id.content).getChildAt(0) as? ComposeView

if (existingComposeView != null) {
with(existingComposeView) {
Expand All @@ -80,7 +56,7 @@ fun PreComposeActivity.setContent(
}
}

private fun PreComposeActivity.setOwners() {
private fun ComponentActivity.setOwners() {
val decorView = window.decorView
if (decorView.findViewTreeLifecycleOwner() == null) {
decorView.setViewTreeLifecycleOwner(this)
Expand All @@ -94,16 +70,48 @@ private fun PreComposeActivity.setOwners() {
}

@Composable
private fun PreComposeActivity.ContentInternal(content: @Composable () -> Unit) {
private fun ComponentActivity.ContentInternal(content: @Composable () -> Unit) {
ProvideAndroidCompositionLocals {
content.invoke()
}
}

@Composable
private fun PreComposeActivity.ProvideAndroidCompositionLocals(
private fun ComponentActivity.ProvideAndroidCompositionLocals(
content: @Composable () -> Unit,
) {
val viewModel by viewModels<PreComposeViewModel>()

DisposableEffect(lifecycle) {
val observer = object : DefaultLifecycleObserver {
override fun onCreate(owner: LifecycleOwner) {
super.onCreate(owner)
onBackPressedDispatcher.addCallback(owner, viewModel.backPressedCallback)
}

override fun onResume(owner: LifecycleOwner) {
super.onResume(owner)
viewModel.lifecycleRegistry.currentState = Lifecycle.State.Active
}

override fun onPause(owner: LifecycleOwner) {
super.onPause(owner)
viewModel.lifecycleRegistry.currentState = Lifecycle.State.InActive
}

override fun onDestroy(owner: LifecycleOwner) {
super.onDestroy(owner)
if (!isChangingConfigurations) {
viewModel.lifecycleRegistry.currentState = Lifecycle.State.Destroyed
}
}
}
lifecycle.addObserver(observer)
onDispose {
lifecycle.removeObserver(observer)
}
}

val state by viewModel.backDispatcher.canHandleBackPress.collectAsState(false)

val saveableStateRegistry = LocalSaveableStateRegistry.current
Expand All @@ -118,9 +126,9 @@ private fun PreComposeActivity.ProvideAndroidCompositionLocals(
viewModel.backPressedCallback.isEnabled = state
}
CompositionLocalProvider(
LocalLifecycleOwner provides this.viewModel,
LocalStateHolder provides this.viewModel.stateHolder,
LocalBackDispatcherOwner provides this.viewModel,
LocalLifecycleOwner provides viewModel,
LocalStateHolder provides viewModel.stateHolder,
LocalBackDispatcherOwner provides viewModel,
LocalSavedStateHolder provides savedStateHolder,
) {
content.invoke()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package moe.tlaster.precompose.molecule.sample

import android.os.Bundle
import moe.tlaster.precompose.lifecycle.PreComposeActivity
import androidx.fragment.app.FragmentActivity
import moe.tlaster.precompose.lifecycle.setContent

class MainActivity : PreComposeActivity() {
class MainActivity : FragmentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
package moe.tlaster.android

import android.os.Bundle
import androidx.compose.material.ExperimentalMaterialApi
import androidx.activity.ComponentActivity
import moe.tlaster.common.App
import moe.tlaster.precompose.lifecycle.PreComposeActivity
import moe.tlaster.precompose.lifecycle.setContent

class MainActivity : PreComposeActivity() {
@OptIn(ExperimentalMaterialApi::class)
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
Expand Down

0 comments on commit bc13c2f

Please sign in to comment.