From 64b7ad76ad56a8d60e482543b769ca6b7996c84b Mon Sep 17 00:00:00 2001 From: Arkadii Ivanov Date: Thu, 14 Mar 2024 21:06:04 +0000 Subject: [PATCH] Updated docs with the new custom component context API --- docs/component/custom-component-context.md | 68 +++++++++++++++++----- docs/navigation/slot/component-context.md | 6 +- docs/navigation/stack/component-context.md | 6 +- 3 files changed, 63 insertions(+), 17 deletions(-) diff --git a/docs/component/custom-component-context.md b/docs/component/custom-component-context.md index 0fbab6eb6..59216dc02 100644 --- a/docs/component/custom-component-context.md +++ b/docs/component/custom-component-context.md @@ -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 { + + 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 = + 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. diff --git a/docs/navigation/slot/component-context.md b/docs/navigation/slot/component-context.md index 4abe9f264..adf400b60 100644 --- a/docs/navigation/slot/component-context.md +++ b/docs/navigation/slot/component-context.md @@ -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`. diff --git a/docs/navigation/stack/component-context.md b/docs/navigation/stack/component-context.md index b3e08237e..099abccb7 100644 --- a/docs/navigation/stack/component-context.md +++ b/docs/navigation/stack/component-context.md @@ -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`.