Skip to content

Commit

Permalink
Merge pull request #611 from arkivanov/imports-docs-navigation
Browse files Browse the repository at this point in the history
Added imports to code snippets in docs/navigation
  • Loading branch information
arkivanov authored Jan 18, 2024
2 parents ad5129f + d9c33eb commit d94479a
Show file tree
Hide file tree
Showing 7 changed files with 366 additions and 144 deletions.
2 changes: 1 addition & 1 deletion docs/extensions/compose.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ fun main() {

!!! note

You can find the `runOnUiThread` method [here]([Utils.kt](https://github.com/arkivanov/Decompose/blob/master/sample/app-desktop/src/jvmMain/kotlin/com/arkivanov/sample/app/Utils.kt)).
You can find the `runOnUiThread` method [here](https://github.com/arkivanov/Decompose/blob/master/sample/app-desktop/src/jvmMain/kotlin/com/arkivanov/sample/app/Utils.kt).

## Navigating between Composable components

Expand Down
23 changes: 22 additions & 1 deletion docs/navigation/pages/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ There are three steps to initialize `Child Pages`:
Here is a very basic example of a pager-like navigation:

```kotlin title="PageComponent"
import com.arkivanov.decompose.ComponentContext

interface PageComponent {
val data: String
}
Expand All @@ -55,6 +57,16 @@ class DefaultPageComponent(
=== "Before v2.2.0-alpha01"

```kotlin title="PagesComponent"
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.router.pages.ChildPages
import com.arkivanov.decompose.router.pages.Pages
import com.arkivanov.decompose.router.pages.PagesNavigation
import com.arkivanov.decompose.router.pages.childPages
import com.arkivanov.decompose.router.pages.select
import com.arkivanov.decompose.value.Value
import com.arkivanov.essenty.parcelable.Parcelable
import com.arkivanov.essenty.parcelable.Parcelize

interface PagesComponent {
val pages: Value<ChildPages<*, PageComponent>>

Expand Down Expand Up @@ -89,12 +101,21 @@ class DefaultPageComponent(

@Parcelize // kotlin-parcelize plugin must be applied if you are targetting Android
private data class Config(val data: String) : Parcelable
}
}
```

=== "Since v2.2.0-alpha01"

```kotlin title="PagesComponent"
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.router.pages.ChildPages
import com.arkivanov.decompose.router.pages.Pages
import com.arkivanov.decompose.router.pages.PagesNavigation
import com.arkivanov.decompose.router.pages.childPages
import com.arkivanov.decompose.router.pages.select
import com.arkivanov.decompose.value.Value
import kotlinx.serialization.Serializable

interface PagesComponent {
val pages: Value<ChildPages<*, PageComponent>>

Expand Down
115 changes: 88 additions & 27 deletions docs/navigation/slot/component-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,93 @@

Custom `ComponentContext` allows passing extra data and functionality to every child component. See [Custom ComponentContext](../../component/custom-component-context.md) page for more information about creating custom `AppComponentContext`.

In order to pass custom component context (like `AppComponentContext`) to child slot components, make an extension function on your `AppComponentContext` interface. This custom extension function will initialize the `Child Slot` and provide every child an `AppComponentContext`.
In order to pass the custom `ComponentContext` (like `AppComponentContext`) to child slot components, make an extension function on your `AppComponentContext` interface. This custom extension function will initialize the `Child Slot` and provide every child an `AppComponentContext`.

```kotlin
inline fun <reified C : Any, T : Any> AppComponentContext.appChildSlot(
source: SlotNavigationSource<C>,
noinline initialConfiguration: () -> C? = { null },
key: String = "DefaultSlot",
handleBackButton: Boolean = false,
persistent: Boolean = false,
noinline childFactory: (configuration: C, AppComponentContext) -> T
): Value<ChildSlot<C, T>> =
childSlot(
source = source,
key = key,
handleBackButton = handleBackButton,
initialConfiguration = initialConfiguration,
persistent = persistent
) { configuration, componentContext ->
childFactory(
configuration,
DefaultAppComponentContext(
componentContext = componentContext,
// Additional dependencies here
=== "Before v2.2.0-alpha01"

```kotlin
import com.arkivanov.decompose.router.slot.ChildSlot
import com.arkivanov.decompose.router.slot.SlotNavigationSource
import com.arkivanov.decompose.router.slot.childSlot
import com.arkivanov.decompose.value.Value
import com.arkivanov.essenty.parcelable.Parcelable

inline fun <reified C : Parcelable, T : Any> AppComponentContext.appChildSlot(
source: SlotNavigationSource<C>,
noinline initialConfiguration: () -> C? = { null },
key: String = "DefaultSlot",
handleBackButton: Boolean = false,
persistent: Boolean = false,
noinline childFactory: (configuration: C, AppComponentContext) -> T
): Value<ChildSlot<C, T>> =
childSlot(
source = source,
key = key,
handleBackButton = handleBackButton,
initialConfiguration = initialConfiguration,
persistent = persistent
) { configuration, componentContext ->
childFactory(
configuration,
DefaultAppComponentContext(
componentContext = componentContext,
// Additional dependencies here
)
)
)
}
```
}
```

=== "Since v2.2.0-alpha01"

```kotlin
import com.arkivanov.decompose.router.slot.ChildSlot
import com.arkivanov.decompose.router.slot.SlotNavigationSource
import com.arkivanov.decompose.router.slot.childSlot
import com.arkivanov.decompose.value.Value
import kotlinx.serialization.KSerializer

interface AppComponentContext : ComponentContext

class DefaultAppComponentContext(componentContext: ComponentContext) : AppComponentContext

inline fun <reified C : Any, T : Any> AppComponentContext.appChildSlot(
source: SlotNavigationSource<C>,
serializer: KSerializer<C>?,
noinline initialConfiguration: () -> C? = { null },
key: String = "DefaultSlot",
handleBackButton: Boolean = false,
noinline childFactory: (configuration: C, AppComponentContext) -> T
): Value<ChildSlot<C, T>> =
childSlot(
source = source,
serializer = serializer,
key = key,
handleBackButton = handleBackButton,
initialConfiguration = initialConfiguration,
) { configuration, componentContext ->
childFactory(
configuration,
DefaultAppComponentContext(
componentContext = componentContext,
// Additional dependencies here
)
)
}
```

Finally, in your components you can use the new extension function that will utilize the custom `AppComponentContext`.

=== "Before v3.0.0-alpha01"
=== "Before v2.2.0-alpha01"

```kotlin
import com.arkivanov.decompose.router.slot.ChildSlot
import com.arkivanov.decompose.router.slot.SlotNavigation
import com.arkivanov.decompose.router.slot.activate
import com.arkivanov.decompose.router.slot.dismiss
import com.arkivanov.decompose.value.Value
import com.arkivanov.essenty.parcelable.Parcelable
import com.arkivanov.essenty.parcelable.Parcelize

interface RootComponent {
val dialog: Value<ChildSlot<*, DialogComponent>>
}
Expand Down Expand Up @@ -67,9 +121,16 @@ Finally, in your components you can use the new extension function that will uti
}
```

=== "Since v3.0.0-alpha01"
=== "Since v2.2.0-alpha01"

```kotlin
import com.arkivanov.decompose.router.slot.ChildSlot
import com.arkivanov.decompose.router.slot.SlotNavigation
import com.arkivanov.decompose.router.slot.activate
import com.arkivanov.decompose.router.slot.dismiss
import com.arkivanov.decompose.value.Value
import kotlinx.serialization.Serializable

interface RootComponent {
val dialog: Value<ChildSlot<*, DialogComponent>>
}
Expand Down
31 changes: 28 additions & 3 deletions docs/navigation/slot/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ There are three steps to initialize the `Child Slot`:
Here is a very basic example of a child slot:

```kotlin title="Dialog component"
import com.arkivanov.decompose.ComponentContext

interface DialogComponent {

fun onDismissClicked()
Expand All @@ -48,6 +50,16 @@ class DefaultDialogComponent(
=== "Before v2.2.0-alpha01"

```kotlin title="Root component"
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.router.slot.ChildSlot
import com.arkivanov.decompose.router.slot.SlotNavigation
import com.arkivanov.decompose.router.slot.activate
import com.arkivanov.decompose.router.slot.childSlot
import com.arkivanov.decompose.router.slot.dismiss
import com.arkivanov.decompose.value.Value
import com.arkivanov.essenty.parcelable.Parcelable
import com.arkivanov.essenty.parcelable.Parcelize

interface RootComponent {

val dialog: Value<ChildSlot<*, DialogComponent>>
Expand Down Expand Up @@ -76,7 +88,7 @@ class DefaultDialogComponent(
dialogNavigation.activate(DialogConfig(message = message))
}

@Parcelize // kotlin-parcelize plugin must be applied if you are targetting Android
@Parcelize // kotlin-parcelize plugin must be applied if you are targeting Android
private data class DialogConfig(
val message: String,
) : Parcelable
Expand All @@ -86,6 +98,15 @@ class DefaultDialogComponent(
=== "Since v2.2.0-alpha01"

```kotlin title="Root component"
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.router.slot.ChildSlot
import com.arkivanov.decompose.router.slot.SlotNavigation
import com.arkivanov.decompose.router.slot.activate
import com.arkivanov.decompose.router.slot.childSlot
import com.arkivanov.decompose.router.slot.dismiss
import com.arkivanov.decompose.value.Value
import kotlinx.serialization.Serializable

interface RootComponent {

val dialog: Value<ChildSlot<*, DialogComponent>>
Expand Down Expand Up @@ -126,12 +147,16 @@ class DefaultDialogComponent(
When multiple `Child Slots` are used in one component, each such `Child Slot` must have a unique key associated. The keys are required to be unique only within the parent (hosting) component, so it is ok for different components to have `Child Slots` with same keys. An exception will be thrown if multiple `Child Slots` with the same key are detected in a component.

```kotlin title="Two Child Slots in one component"
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.router.slot.SlotNavigation
import com.arkivanov.decompose.router.slot.childSlot

class Root(
componentContext: ComponentContext
) : ComponentContext by componentContext {

private val topNavigation = SlotNavigation<TopConfig>()

private val topSlot =
childSlot<TopConfig, TopChild>(
source = topNavigation,
Expand All @@ -140,7 +165,7 @@ class Root(
)

private val bottomNavigation = SlotNavigation<BottomConfig>()

private val bottomSlot =
childSlot<BottomConfig, BottomChild>(
source = bottomNavigation,
Expand Down
Loading

0 comments on commit d94479a

Please sign in to comment.