-
Notifications
You must be signed in to change notification settings - Fork 9
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
Wrap ComponentContext with RouterContext #59
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
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
14 changes: 14 additions & 0 deletions
14
...mpose-router/src/androidMain/kotlin/io/github/xxfast/decompose/router/RouterContextExt.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,14 @@ | ||
package io.github.xxfast.decompose.router | ||
|
||
import androidx.activity.ComponentActivity | ||
import androidx.activity.OnBackPressedDispatcher | ||
import androidx.fragment.app.Fragment | ||
import com.arkivanov.decompose.defaultComponentContext | ||
|
||
fun ComponentActivity.defaultRouterContext(): RouterContext = | ||
RouterContext(defaultComponentContext()) | ||
|
||
fun Fragment.defaultRouterContext( | ||
onBackPressedDispatcher: OnBackPressedDispatcher?, | ||
): RouterContext = | ||
RouterContext(defaultComponentContext(onBackPressedDispatcher)) |
45 changes: 0 additions & 45 deletions
45
decompose-router/src/commonMain/kotlin/io/github/xxfast/decompose/Navigator.kt
This file was deleted.
Oops, something went wrong.
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
43 changes: 43 additions & 0 deletions
43
decompose-router/src/commonMain/kotlin/io/github/xxfast/decompose/router/RouterContext.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 io.github.xxfast.decompose.router | ||
|
||
import androidx.compose.runtime.ProvidableCompositionLocal | ||
import androidx.compose.runtime.staticCompositionLocalOf | ||
import com.arkivanov.decompose.ComponentContext | ||
import com.arkivanov.decompose.DefaultComponentContext | ||
import com.arkivanov.essenty.backhandler.BackHandler | ||
import com.arkivanov.essenty.instancekeeper.InstanceKeeper | ||
import com.arkivanov.essenty.lifecycle.Lifecycle | ||
import com.arkivanov.essenty.statekeeper.StateKeeper | ||
|
||
class RouterContext internal constructor( | ||
private val delegate: ComponentContext, | ||
) : ComponentContext by delegate { | ||
|
||
constructor( | ||
lifecycle: Lifecycle, | ||
stateKeeper: StateKeeper? = null, | ||
instanceKeeper: InstanceKeeper? = null, | ||
backHandler: BackHandler? = null, | ||
) : this(DefaultComponentContext(lifecycle, stateKeeper, instanceKeeper, backHandler)) | ||
|
||
internal val storage: MutableMap<Any, Any> = HashMap() | ||
} | ||
|
||
internal inline fun <reified T : Any> RouterContext.getOrCreate(key: Any, factory: () -> T) : T { | ||
var instance: T? = storage[key] as T? | ||
if (instance == null) { | ||
instance = factory() | ||
storage[key] = instance | ||
} | ||
|
||
return instance | ||
} | ||
|
||
/*** | ||
* Compositional local for [RouterContext]. | ||
* | ||
* Based on [Arkadii](https://github.com/arkivanov)'s [article](https://proandroiddev.com/a-comprehensive-hundred-line-navigation-for-jetpack-desktop-compose-5b723c4f256e) | ||
* Original [source](https://github.com/arkivanov/ComposeNavigatorExample/blob/d786d92632fe22e4d7874645ba2071fb813f9ace/navigator/src/commonMain/kotlin/com/arkivanov/composenavigatorexample/navigator/Navigator.kt) | ||
*/ | ||
val LocalRouterContext: ProvidableCompositionLocal<RouterContext> = | ||
staticCompositionLocalOf { error("Root RouterContext was not provided") } |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
@arkivanov Do you think we need to key this remember with our
key
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.
Yeah, you can use keyStr, the same as for getOrCreate