-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Disallow destroying child ComponentContext
- Loading branch information
Showing
21 changed files
with
572 additions
and
212 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
decompose/src/commonMain/kotlin/com/arkivanov/decompose/Annotations.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 |
---|---|---|
@@ -1,9 +1,16 @@ | ||
package com.arkivanov.decompose | ||
|
||
/** | ||
* Marks internal declarations in Decompose. Internal declarations must not be used outside the library. | ||
* There are no backward compatibility guarantees between different versions of Decompose. | ||
*/ | ||
@RequiresOptIn(level = RequiresOptIn.Level.ERROR) | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class InternalDecomposeApi | ||
|
||
/** | ||
* Marks experimental API in Decompose. An experimental API can be changed or removed at any time. | ||
*/ | ||
@RequiresOptIn(level = RequiresOptIn.Level.WARNING) | ||
@Retention(AnnotationRetention.BINARY) | ||
annotation class ExperimentalDecomposeApi |
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
4 changes: 4 additions & 0 deletions
4
decompose/src/commonMain/kotlin/com/arkivanov/decompose/Utils.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 |
---|---|---|
@@ -1,3 +1,7 @@ | ||
package com.arkivanov.decompose | ||
|
||
import com.arkivanov.essenty.lifecycle.Lifecycle | ||
|
||
internal expect fun Any.ensureNeverFrozen() | ||
|
||
internal val Lifecycle.isDestroyed: Boolean get() = state == Lifecycle.State.DESTROYED |
26 changes: 20 additions & 6 deletions
26
decompose/src/commonMain/kotlin/com/arkivanov/decompose/backhandler/ChildBackHandler.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 |
---|---|---|
@@ -1,27 +1,41 @@ | ||
package com.arkivanov.decompose.backhandler | ||
|
||
import com.arkivanov.decompose.isDestroyed | ||
import com.arkivanov.essenty.backhandler.BackHandler | ||
import com.arkivanov.essenty.lifecycle.Lifecycle | ||
import com.arkivanov.essenty.lifecycle.doOnDestroy | ||
import com.arkivanov.essenty.lifecycle.subscribe | ||
|
||
internal interface ChildBackHandler : BackHandler { | ||
|
||
var isEnabled: Boolean | ||
|
||
fun start() | ||
|
||
fun stop() | ||
} | ||
|
||
internal fun BackHandler.child(lifecycle: Lifecycle?): BackHandler { | ||
val handler = DefaultChildBackHandler(parent = this) | ||
internal fun BackHandler.child(lifecycle: Lifecycle? = null): BackHandler { | ||
val handler = childBackHandler(isEnabled = false) | ||
|
||
if (lifecycle == null) { | ||
handler.isEnabled = true | ||
handler.start() | ||
} else if (lifecycle.state != Lifecycle.State.DESTROYED) { | ||
} else if (!lifecycle.isDestroyed) { | ||
handler.isEnabled = lifecycle.state >= Lifecycle.State.STARTED | ||
handler.start() | ||
lifecycle.doOnDestroy(handler::stop) | ||
|
||
lifecycle.subscribe( | ||
onStart = { handler.isEnabled = true }, | ||
onStop = { handler.isEnabled = false }, | ||
onDestroy = handler::stop, | ||
) | ||
} | ||
|
||
return handler | ||
} | ||
|
||
internal fun BackHandler.child(): ChildBackHandler = DefaultChildBackHandler(parent = this) | ||
internal fun BackHandler.childBackHandler(isEnabled: Boolean = true): ChildBackHandler = | ||
DefaultChildBackHandler( | ||
parent = this, | ||
isEnabled = isEnabled, | ||
) |
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
18 changes: 11 additions & 7 deletions
18
...mpose/src/commonMain/kotlin/com/arkivanov/decompose/instancekeeper/ChildInstanceKeeper.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
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
12 changes: 9 additions & 3 deletions
12
decompose/src/commonMain/kotlin/com/arkivanov/decompose/statekeeper/ChildStateKeeper.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 |
---|---|---|
@@ -1,17 +1,23 @@ | ||
package com.arkivanov.decompose.statekeeper | ||
|
||
import com.arkivanov.decompose.isDestroyed | ||
import com.arkivanov.essenty.lifecycle.Lifecycle | ||
import com.arkivanov.essenty.lifecycle.doOnDestroy | ||
import com.arkivanov.essenty.statekeeper.StateKeeper | ||
import com.arkivanov.essenty.statekeeper.StateKeeperDispatcher | ||
import com.arkivanov.essenty.statekeeper.consume | ||
|
||
internal fun StateKeeper.child(key: String, lifecycle: Lifecycle?): StateKeeper { | ||
internal fun StateKeeper.child(key: String, lifecycle: Lifecycle? = null): StateKeeper { | ||
check(!isRegistered(key = key)) { "The key \"$key\" is already in use." } | ||
|
||
val stateKeeper = StateKeeperDispatcher(consume(key)) | ||
register(key, stateKeeper::save) | ||
lifecycle?.doOnDestroy { unregister(key) } | ||
|
||
if (lifecycle == null) { | ||
register(key, stateKeeper::save) | ||
} else if (!lifecycle.isDestroyed) { | ||
register(key, stateKeeper::save) | ||
lifecycle.doOnDestroy { unregister(key) } | ||
} | ||
|
||
return stateKeeper | ||
} |
Oops, something went wrong.