Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifecycle KMP experimental support #423

Merged
merged 7 commits into from
May 21, 2024
Merged

Lifecycle KMP experimental support #423

merged 7 commits into from
May 21, 2024

Conversation

DevSrSouza
Copy link
Collaborator

@DevSrSouza DevSrSouza commented May 21, 2024

Usage:

@Composable
fun MainView() {
    ProvideNavigatorLifecycleKMPSupport {
        Navigator(...)
    }
}

In the Screen, just use plain Lifecycle ViewModel Compose viewModel callback or any third party DI that already supports lifecycle kmp.

[versions]
androidxKmp-lifecycle = "2.8.0-rc03"
voyager = "next version"

[libraries]
voyager-lifecycleKmp = { module = "cafe.adriel.voyager:voyager-navigator", version.ref = "voyager" }
androidxKmp-lifecycle-viewmodelCompose = { module = "org.jetbrains.androidx.lifecycle:lifecycle-viewmodel-compose", version.ref = "androidxKmp-lifecycle" }

This PR also updates Compose KMP to 1.6.10

Comment on lines +7 to +8
public val LocalNavigatorScreenLifecycleProvider: ProvidableCompositionLocal<NavigatorScreenLifecycleProvider?> =
staticCompositionLocalOf { null }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any particular reason for making it nullable and initialising it as null as well?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have moved the default implementation to the Navigator module, when this is null, it will use the default implementation. The reason is for removing the android lifecycle API from the core module and move to the navigator, also avoid cycle dependencies

setupModuleForComposeMultiplatform(fullyMultiplatform = true)

android {
namespace = "cafe.adriel.voyager.kodein"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe?

Suggested change
namespace = "cafe.adriel.voyager.kodein"
namespace = "cafe.adriel.voyager.lifecycle.kmp"

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! forget to replace

Comment on lines +19 to +20
compileOnly(compose.runtime)
compileOnly(compose.runtimeSaveable)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this might lead to a warning like

A compileOnly dependency is used in the Kotlin/Native target 'ios....':

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a old approach that I don't know if is still required. In the past, if you not add compileOnly to Compose KMP libraries, then, android only project would fail to use the library because it would not find the jetbrains android dependencies. I think this is fixed, but I not 100% sure without testing, for now, I will still keep this approach.

For the warning, we have a flag
https://github.com/adrielcafe/voyager/blob/main/gradle.properties#L12-L19

Comment on lines 159 to 160
// when the Application goes to background, perform save
performSave(outState)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

any special reason why not saving it for onPause lifecycle as well?

@DevSrSouza DevSrSouza merged commit 309e6c7 into main May 21, 2024
1 check passed
@DevSrSouza DevSrSouza deleted the viewmodel-kmp branch May 21, 2024 10:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants