-
Notifications
You must be signed in to change notification settings - Fork 74
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
Compose as LifecycleOwner
#1198
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
f8fdda2
to
8aa573d
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome, thanks!
I agree with the states that we need to set on different events, but we need to set them carefully to avoid bugs. See my comments, I suggest to create a common class that routes events the right way.
} | ||
|
||
companion object { | ||
fun logWarning(message: String) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fun logWarning(message: String) { | |
private fun logWarning(message: String) { |
fail("Invalid '$action' for 'State.Disposed'") | ||
} | ||
Action.APPLICATION_DID_ENTER_BACKGROUND, Action.APPLICATION_WILL_ENTER_FOREGROUND -> { | ||
// no-op |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we fail as well? These actions shouldn't be fired because we unsubscribe from them:
fun dispose() {
handleAction(Action.DISPOSE)
applicationStateListener.dispose()
}
import androidx.lifecycle.LifecycleRegistry | ||
import kotlin.test.fail | ||
|
||
internal class ViewControllerBasedLifecycleOwner : LifecycleOwner { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should write a unit test for this class, as here can be lot of bugs regarding non-symmetric events.
if (isApplicationForeground) { | ||
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE) | ||
} | ||
|
||
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP) | ||
|
||
Created(isApplicationForeground = isApplicationForeground, lifecycle = lifecycle) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if (isApplicationForeground) { | |
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE) | |
} | |
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_STOP) | |
Created(isApplicationForeground = isApplicationForeground, lifecycle = lifecycle) | |
this |
It doesn't sound right to emit Pause/Stop from Created state. If we in this state, it means we didn't receive VIEW_WILL_APPEAR
, and so - didn't emit Start/Resume before.
import androidx.lifecycle.Lifecycle | ||
import androidx.lifecycle.LifecycleOwner | ||
import androidx.lifecycle.LifecycleRegistry | ||
import kotlin.test.fail |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why is the test library in the dependencies?
We should remove it, and use error()
instead
} | ||
|
||
init { | ||
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What if the class itself is created after view is visible? We need to send ON_START or ON_RESUME
compose/ui/ui/src/desktopMain/kotlin/androidx/compose/ui/scene/ComposeContainer.desktop.kt
Show resolved
Hide resolved
@@ -227,9 +255,13 @@ internal class ComposeContainer( | |||
// Re-checking the actual size if it wasn't available during init. | |||
onChangeWindowSize() | |||
onChangeWindowPosition() | |||
|
|||
lifecycle.handleLifecycleEvent(Lifecycle.Event.ON_START) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We set started
state on 2 different states:
- when it is iconified (minimized)
- when it is added to window hierarchy.
So, we have 2 sources of truth and races between them.
To avoid that, we should either choose one source of truth, or combine isIconified
and isAddedToHirarchy
states.
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_PAUSE) | ||
}) | ||
|
||
lifecycleOwner.handleLifecycleEvent(Lifecycle.Event.ON_CREATE) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's not call ON_CREATE
and ON_START
in the same method init
, and just call ON_START
straight away in the end of init
, if JS doesn't have meaning for CREATED
state.
@@ -170,6 +170,8 @@ if(AndroidXComposePlugin.isMultiplatformEnabled(project)) { | |||
api project(":compose:ui:ui-unit") | |||
api project(":compose:ui:ui-util") | |||
implementation(project(":annotation:annotation")) | |||
implementation(project(":lifecycle:lifecycle-common")) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because we add a new dependency, this change requires 2 approves, please ask someone else to review this commit (about adding dependency).
It looks fine for me, because we either will release a stable version of lifecycle
, or we just depend on stable API of it right now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eymar, no need to review the whole PR, only this commit
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having these dependencies added, we can't avoid publishing lifecycle libraries (there was an idea that maybe we don't need to publish lifecyclce right now - #1204 (comment)). User projects won't work with this ui library if we don't publish lifecycle.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
there was an idea
If you talk about my comment:
If we don't need it urgently
I meant that "if don't need it right now". But we definitely need it for the 1.6.10 release
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
even dev builds won't work untill lifecycle is published if this PR is merged before that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed. Then let's wait until we publish
We agreed to push it as is, and fix comments separately |
I will create separate tasks for each platform later. |
8aa573d
to
926ec3f
Compare
After lifecycle modules commonization, `LifecycleOwner` is available in the common source set. Move it via `expect`/`actual` to keep binary compatibility on Android, which is based on filename - `AndroidCompositionLocals_androidKt`. Test: N/A Change-Id: I4b0ed6087e494a0b30cb6a890496fa17ebfda19c
Initial implementation of iOS bindings to Lifecycle. <img width="836" alt="ios_lifecycle" src="https://github.com/JetBrains/compose-multiplatform-core/assets/4167681/ec73ed26-5435-43e7-a4cf-5d3b9fe91693">
926ec3f
to
4c4b1ac
Compare
It cannot be handled anyway because ON_STARTED/ON_RESUME will be called immediately after and web target is single-threaded Fixes comment from #1198 ## Testing Test: added ComposeWindowLifecycleTest
## Proposed Changes Fixed iOS-related comments from #1198 ## Testing Test: added ViewControllerBasedLifecycleOwnerTest
Proposed Changes
LifecycleOwner
for Desktop, iOS and WebTesting
Test: Observe lifecycle events in mpp
Fixed Issues
Fixes JetBrains/compose-multiplatform#2915