File tree Expand file tree Collapse file tree
androidMain/kotlin/com/multiplatform/lifecyle
commonMain/kotlin/com.multiplatform.lifecycle
iosMain/kotlin/com.multiplatform.lifecyle Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -6,9 +6,10 @@ import com.multiplatform.lifecycle.LifecycleEvent
66import com.multiplatform.lifecycle.LifecycleTracker
77
88/* *
9- * Created By Kevin Zou On 2024/2/22
9+ * An observer for Android lifecycle events.
1010 */
11- class AndroidLifecycleEventObserver (private val lifecycleTracker : LifecycleTracker ) : DefaultLifecycleObserver {
11+ class AndroidLifecycleEventObserver (private val lifecycleTracker : LifecycleTracker ) :
12+ DefaultLifecycleObserver {
1213 override fun onCreate (owner : LifecycleOwner ) {
1314 super .onCreate(owner)
1415 lifecycleTracker.handleLifecycleEvent(LifecycleEvent .OnCreateEvent )
Original file line number Diff line number Diff line change 11package com.multiplatform.lifecycle
22
33/* *
4- * Created By Kevin Zou On 2024/2/26
4+ * A class that has an Android-like lifecycle.
55 */
66abstract class Lifecycle {
77 /* *
@@ -14,28 +14,18 @@ abstract class Lifecycle {
1414 *
1515 * @param observer The observer to notify.
1616 */
17- abstract fun addObserver (observer : LifecycleObserver )
17+ abstract fun addObserver (observer : LifecycleObserver )
1818
1919 /* *
2020 * Removes the given observer from the observers list.
21- *
22- * If this method is called while a state change is being dispatched,
23- *
24- * * If the given observer has not yet received that event, it will not receive it.
25- * * If the given observer has more than 1 method that observes the currently dispatched
26- * event and at least one of them received the event, all of them will receive the event and
27- * the removal will happen afterwards.
28- *
29- *
3021 * @param observer The observer to be removed.
3122 */
32- abstract fun removeObserver (observer : LifecycleObserver )
23+ abstract fun removeObserver (observer : LifecycleObserver )
3324
3425 /* *
3526 * Returns the current state of the Lifecycle.
3627 *
3728 * @return The current state of the Lifecycle.
3829 */
39- abstract val currentState: State
40-
41- }
30+ abstract val currentState: State
31+ }
Original file line number Diff line number Diff line change 11package com.multiplatform.lifecycle
22
33/* *
4- * Created By Kevin Zou On 2024/2/22
4+ * Lifecycle events. These events can be used by developers to listen to the lifecycle of the application.
55 */
66sealed class LifecycleEvent {
77 data object OnCreateEvent : LifecycleEvent ()
Original file line number Diff line number Diff line change 11package com.multiplatform.lifecycle
22
33/* *
4- * Created By Kevin Zou On 2024/2/22
4+ * A lifecycle tracker that can be used to track the lifecycle of a component.
55 */
66interface LifecycleObserver {
77 fun onEvent (event : LifecycleEvent ) {
Original file line number Diff line number Diff line change 11package com.multiplatform.lifecycle
22
3- /* *
4- * Created By Kevin Zou On 2024/2/26
5- */
6-
73/* *
84 * Lifecycle states. You can consider the states as the nodes in a graph and
95 * [LifecycleEvent]s as the edges between these nodes.
@@ -59,7 +55,9 @@ enum class State {
5955 * Resumed state for a LifecycleOwner. For an [android.app.Activity], this state
6056 * is reached after [onResume][android.app.Activity.onResume] is called.
6157 */
62- RESUMED ;
58+ RESUMED ,
59+
60+ ;
6361
6462 /* *
6563 * Compares if this State is greater or equal to the given `state`.
@@ -70,4 +68,4 @@ enum class State {
7068 fun isAtLeast (state : State ): Boolean {
7169 return compareTo(state) >= 0
7270 }
73- }
71+ }
Original file line number Diff line number Diff line change 11package com.multiplatform.lifecycle
22
33/* *
4- * Created By Kevin Zou On 2024/2/22
4+ * A lifecycle tracker that can be used to track the lifecycle of the application.
55 */
6- open class LifecycleTracker : Lifecycle () {
6+ open class LifecycleTracker : Lifecycle () {
77 /* *
88 * Current state
99 */
@@ -34,11 +34,7 @@ open class LifecycleTracker: Lifecycle() {
3434
3535 override var currentState: State
3636 get() = state
37- /* *
38- * Moves the Lifecycle to the given state and dispatches necessary events to the observers.
39- *
40- * @param state new state
41- */
37+
4238 set(state) {
4339 moveToState(state)
4440 }
Original file line number Diff line number Diff line change @@ -2,16 +2,14 @@ package com.multiplatform.lifecycle
22
33import androidx.compose.runtime.staticCompositionLocalOf
44
5- /* *
6- * Created By Kevin Zou On 2024/2/27
7- */
85/* *
96 * The CompositionLocal containing the current [LifecycleTracker].
107 */
11- val LocalLifecycleTracker = staticCompositionLocalOf<LifecycleTracker > {
12- noLocalProvidedFor(" LocalLifecycleTracker" )
13- }
8+ val LocalLifecycleTracker =
9+ staticCompositionLocalOf<LifecycleTracker > {
10+ noLocalProvidedFor(" LocalLifecycleTracker" )
11+ }
1412
1513private fun noLocalProvidedFor (name : String ): Nothing {
1614 error(" CompositionLocal $name not present" )
17- }
15+ }
Original file line number Diff line number Diff line change @@ -14,7 +14,7 @@ import platform.UIKit.UIApplicationWillResignActiveNotification
1414import platform.UIKit.UIApplicationWillTerminateNotification
1515
1616/* *
17- * Created By Kevin Zou On 2024/2/22
17+ * A delegate for handling iOS lifecycle events in a Compose UI View Controller.
1818 */
1919class LifecycleComposeUIVCDelegate (private val lifecycleTracker : LifecycleTracker ) :
2020 ComposeUIViewControllerDelegate {
Original file line number Diff line number Diff line change @@ -8,7 +8,7 @@ import platform.Foundation.NSNotification
88import platform.darwin.NSObject
99
1010/* *
11- * Created By Kevin Zou On 2024/2/26
11+ * A listener for iOS lifecycle events.
1212 */
1313@Suppress(" unused" )
1414@OptIn(BetaInteropApi ::class )
You can’t perform that action at this time.
0 commit comments