Skip to content

Commit

Permalink
Updated docs with the new custom component context API
Browse files Browse the repository at this point in the history
  • Loading branch information
arkivanov committed Mar 19, 2024
1 parent c6e9423 commit 64b7ad7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 17 deletions.
68 changes: 53 additions & 15 deletions docs/component/custom-component-context.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,67 @@
# Custom `ComponentContext`

If one is needing `ComponentContext` to have extra functionality that is not already provided. It is possible to create a custom `ComponentContext` that could be decorated with the desired functionality of your choice.
If you need `ComponentContext` to have extra functionality that is not already provided, it is possible to create a custom component context that could be decorated with the desired functionality of your choice. For instance, in some cases it might be useful to create a component context interface with additional properties required by most of the components.

## Create and implement custom ComponentContext

For example, to create your own custom `ComponentContext` one must first create an interface that extends `ComponentContext` and then provide its implementation.
=== "Before version 3.0.0-alpha09"

```kotlin
import com.arkivanov.decompose.ComponentContext
To define a custom component context, create an interface that extends the `ComponentContext` interface, then implement it by delegating to the existing `ComponentContext`.

```kotlin
import com.arkivanov.decompose.ComponentContext

interface AppComponentContext : ComponentContext {

val logger: Logger // Additional property
}

class DefaultAppComponentContext(
componentContext: ComponentContext,
override val logger: Logger,
) : AppComponentContext, ComponentContext by componentContext
```

interface AppComponentContext : ComponentContext {
=== "Since version 3.0.0-alpha09"

// Custom things here
}
To define a custom component context, create an interface that extends the `GenericComponentContext` interface, then implement it by delegating parts to the existing `ComponentContext`. Also, implement the `componentContextFactory` property to allow Decompose creating new instances of the custom component context type.

class DefaultAppComponentContext(
componentContext: ComponentContext,
// Additional dependencies here
) : AppComponentContext, ComponentContext by componentContext {
```kotlin
import com.arkivanov.decompose.ComponentContext
import com.arkivanov.decompose.ComponentContextFactory
import com.arkivanov.decompose.GenericComponentContext
import com.arkivanov.essenty.backhandler.BackHandlerOwner
import com.arkivanov.essenty.instancekeeper.InstanceKeeperOwner
import com.arkivanov.essenty.lifecycle.LifecycleOwner
import com.arkivanov.essenty.statekeeper.StateKeeperOwner

interface AppComponentContext : GenericComponentContext<AppComponentContext> {

val logger: Logger // Additional property
}

class DefaultAppComponentContext(
componentContext: ComponentContext,
override val logger: Logger,
) : AppComponentContext,
LifecycleOwner by componentContext,
StateKeeperOwner by componentContext,
InstanceKeeperOwner by componentContext,
BackHandlerOwner by componentContext {

override val componentContextFactory: ComponentContextFactory<AppComponentContext> =
ComponentContextFactory { lifecycle, stateKeeper, instanceKeeper, backHandler ->
val ctx = componentContext.componentContextFactory(lifecycle, stateKeeper, instanceKeeper, backHandler)
DefaultAppComponentContext(ctx, logger)
}
}
```

// Custom things implementation here
}
```
## Custom child ComponentContext (before v3.0.0-alpha09)

!!!info "Not required since version 3.0.0-alpha09"

## Custom child ComponentContext
This section is only relevant for Decompose versions before `3.0.0-alpha09`. Since that version, the custom component context can be created the usual way - using the `childContext` extension function.

The default [ComponentContext#childContext](child-components.md#adding-a-child-component-manually) extension function returns the default `ComponentContext`. In order to create custom child `ComponentContext`, a special extension function is required.

Expand Down
6 changes: 5 additions & 1 deletion docs/navigation/slot/component-context.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Child Slot with custom `ComponentContext`
# Child Slot with custom `ComponentContext` (before v3.0.0-alpha09)

!!!info "Not required since version 3.0.0-alpha09"

This section is only relevant for Decompose versions before `3.0.0-alpha09`. Since that version, Child Slot can be created the usual way - using the `childSlot` extension function.

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`.

Expand Down
6 changes: 5 additions & 1 deletion docs/navigation/stack/component-context.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
# Child Stack with custom `ComponentContext`
# Child Stack with custom `ComponentContext` (before v3.0.0-alpha09)

!!!info "Not required since version 3.0.0-alpha09"

This section is only relevant for Decompose versions before `3.0.0-alpha09`. Since that version, Child Stack can be created the usual way - using the `childStack` extension function.

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`.

Expand Down

0 comments on commit 64b7ad7

Please sign in to comment.