-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
166 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
84 changes: 84 additions & 0 deletions
84
precompose/src/androidMain/kotlin/moe/tlaster/precompose/PreComposeApp.android.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
package moe.tlaster.precompose | ||
|
||
import androidx.compose.runtime.Composable | ||
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.Modifier | ||
import androidx.lifecycle.DefaultLifecycleObserver | ||
import moe.tlaster.precompose.lifecycle.Lifecycle | ||
import moe.tlaster.precompose.lifecycle.LocalLifecycleOwner | ||
import moe.tlaster.precompose.lifecycle.PreComposeViewModel | ||
import moe.tlaster.precompose.stateholder.LocalSavedStateHolder | ||
import moe.tlaster.precompose.stateholder.LocalStateHolder | ||
import moe.tlaster.precompose.stateholder.SavedStateHolder | ||
import moe.tlaster.precompose.ui.LocalBackDispatcherOwner | ||
|
||
@Composable | ||
actual fun PreComposeApp( | ||
modifier: Modifier, | ||
content: @Composable () -> Unit, | ||
) { | ||
val viewModel = androidx.lifecycle.viewmodel.compose.viewModel<PreComposeViewModel>() | ||
|
||
val lifecycle = androidx.compose.ui.platform.LocalLifecycleOwner.current.lifecycle | ||
val onBackPressedDispatcher = checkNotNull(androidx.activity.compose.LocalOnBackPressedDispatcherOwner.current) { | ||
"No OnBackPressedDispatcherOwner was provided via LocalOnBackPressedDispatcherOwner" | ||
}.onBackPressedDispatcher | ||
|
||
DisposableEffect(lifecycle) { | ||
val observer = object : DefaultLifecycleObserver { | ||
override fun onCreate(owner: androidx.lifecycle.LifecycleOwner) { | ||
super.onCreate(owner) | ||
onBackPressedDispatcher.addCallback(owner, viewModel.backPressedCallback) | ||
} | ||
|
||
override fun onResume(owner: androidx.lifecycle.LifecycleOwner) { | ||
super.onResume(owner) | ||
viewModel.lifecycleRegistry.currentState = Lifecycle.State.Active | ||
} | ||
|
||
override fun onPause(owner: androidx.lifecycle.LifecycleOwner) { | ||
super.onPause(owner) | ||
viewModel.lifecycleRegistry.currentState = Lifecycle.State.InActive | ||
} | ||
|
||
// override fun onDestroy(owner: androidx.lifecycle.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 | ||
val savedStateHolder = remember(saveableStateRegistry) { | ||
SavedStateHolder( | ||
"root", | ||
saveableStateRegistry, | ||
) | ||
} | ||
|
||
LaunchedEffect(state) { | ||
viewModel.backPressedCallback.isEnabled = state | ||
} | ||
CompositionLocalProvider( | ||
LocalLifecycleOwner provides viewModel, | ||
LocalStateHolder provides viewModel.stateHolder, | ||
LocalBackDispatcherOwner provides viewModel, | ||
LocalSavedStateHolder provides savedStateHolder, | ||
) { | ||
content.invoke() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
precompose/src/commonMain/kotlin/moe/tlaster/precompose/PreComposeApp.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package moe.tlaster.precompose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.ui.Modifier | ||
|
||
@Composable | ||
expect fun PreComposeApp( | ||
modifier: Modifier = Modifier, | ||
content: @Composable () -> Unit = {}, | ||
) |
43 changes: 43 additions & 0 deletions
43
precompose/src/nonAndroidMain/kotlin/moe/tlaster/precompose/PreComposeApp.nonAndroid.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package moe.tlaster.precompose | ||
|
||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.CompositionLocalProvider | ||
import androidx.compose.runtime.remember | ||
import androidx.compose.ui.Modifier | ||
import moe.tlaster.precompose.lifecycle.LifecycleOwner | ||
import moe.tlaster.precompose.lifecycle.LifecycleRegistry | ||
import moe.tlaster.precompose.lifecycle.LocalLifecycleOwner | ||
import moe.tlaster.precompose.stateholder.LocalStateHolder | ||
import moe.tlaster.precompose.stateholder.StateHolder | ||
import moe.tlaster.precompose.ui.BackDispatcher | ||
import moe.tlaster.precompose.ui.BackDispatcherOwner | ||
import moe.tlaster.precompose.ui.LocalBackDispatcherOwner | ||
|
||
@Composable | ||
actual fun PreComposeApp( | ||
modifier: Modifier, | ||
content: @Composable () -> Unit, | ||
) { | ||
val holder = remember { | ||
PreComposeWindowHolder() | ||
} | ||
CompositionLocalProvider( | ||
LocalLifecycleOwner provides holder, | ||
LocalStateHolder provides holder.stateHolder, | ||
LocalBackDispatcherOwner provides holder, | ||
) { | ||
content.invoke() | ||
} | ||
} | ||
|
||
class PreComposeWindowHolder : LifecycleOwner, BackDispatcherOwner { | ||
override val lifecycle by lazy { | ||
LifecycleRegistry() | ||
} | ||
val stateHolder by lazy { | ||
StateHolder() | ||
} | ||
override val backDispatcher by lazy { | ||
BackDispatcher() | ||
} | ||
} |