Skip to content

Commit

Permalink
Update docs for childOverlay with custom component context
Browse files Browse the repository at this point in the history
  • Loading branch information
azlekov committed Oct 11, 2022
1 parent 9ad36e1 commit 6238749
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 60 deletions.
63 changes: 3 additions & 60 deletions docs/component/custom-component-context.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,64 +22,7 @@ class DefaultAppComponentContext(
}
```

## How to initialize Child Stack with custom ComponentContext
## Custom ComponentContext with navigation

In order to pass this `AppComponentContext` to child components, make an extension function on the `AppComponentContext` interface. This custom extension function will initialize the `Child Stack` and provide child `AppComponentContext`.

```kotlin
fun <C : Parcelable, T : Any> AppComponentContext.appChildStack(
source: StackNavigationSource<C>,
initialStack: () -> List<C>,
configurationClass: KClass<out C>,
key: String = "DefaultStack",
handleBackButton: Boolean = false,
childFactory: (configuration: C, AppComponentContext) -> T
): Value<ChildStack<C, T>> =
childStack(
source = source,
initialStack = initialStack,
configurationClass = configurationClass,
key = key,
handleBackButton = handleBackButton
) { configuration, componentContext ->
childFactory(
configuration,
DefaultAppComponentContext(
componentContext = componentContext
)
)
}

inline fun <reified C : Parcelable, T : Any> AppComponentContext.appChildStack(
source: StackNavigationSource<C>,
noinline initialStack: () -> List<C>,
key: String = "DefaultStack",
handleBackButton: Boolean = false,
noinline childFactory: (configuration: C, AppComponentContext) -> T
): Value<ChildStack<C, T>> =
appChildStack(
source = source,
initialStack = initialStack,
configurationClass = C::class,
key = key,
handleBackButton = handleBackButton,
childFactory = childFactory,
)
```

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

```kotlin
class MyComponent(componentContext: AppComponentContext) : AppComponentContext by componentContext {

private val navigation = StackNavigation<Configuration>()

private val childStack = appChildStack(
source = navigation,
initialStack = { listOf(Configuration.Home) },
childFactory = { configuration, appComponentContext ->
// return child components using the custom component context
}
)
}
```
- [With Child Stack](../navigation/stack/component-context.md)
- [With Child Overlay](../navigation/overlay/component-context.md)
69 changes: 69 additions & 0 deletions docs/navigation/overlay/component-context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
# How to initialize Child Overlay with custom ComponentContext

In order to pass this `AppComponentContext` to child overlay components, make an extension function on the `AppComponentContext` interface. This custom extension function will initialize the `Child Overlay` and provide child `AppComponentContext`.

```kotlin
fun <C : Parcelable, T : Any> AppComponentContext.appChildOverlay(
source: OverlayNavigationSource<C>,
initialConfiguration: () -> C?,
configurationClass: KClass<out C>,
key: String = "DefaultOverlay",
handleBackButton: Boolean = false,
persistent: Boolean = false,
childFactory: (configuration: C, AppComponentContext) -> T
): Value<ChildOverlay<C, T>> =
childOverlay(
source = source,
configurationClass = configurationClass,
key = key,
handleBackButton = handleBackButton,
initialConfiguration = initialConfiguration,
persistent = persistent
) { configuration, componentContext ->
childFactory(
configuration,
DefaultAppComponentContext(
componentContext = componentContext,
bundleId,
settings,
boarding
)
)
}

inline fun <reified C : Parcelable, T : Any> AppComponentContext.appChildStack(
source: OverlayNavigationSource<C>,
noinline initialConfiguration: () -> C?,
key: String = "DefaultOverlay",
handleBackButton: Boolean = false,
persistent: Boolean = false,
noinline childFactory: (configuration: C, AppComponentContext) -> T
): Value<ChildOverlay<C, T>> =
appChildOverlay(
source = source,
initialConfiguration = initialConfiguration,
configurationClass = C::class,
key = key,
handleBackButton = handleBackButton,
persistent = persistent,
childFactory = childFactory,
)
```

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

```kotlin
class MyComponent(componentContext: AppComponentContext) : AppComponentContext by componentContext {

private val navigation = OverlayNavigation<Configuration>()

private val childStack = appChildOverlay(
source = navigation,
persistent = false,
handleBackButton = true,
childFactory = { configuration, appComponentContext -> {
// return child components using the custom component context
}
)
}
```
61 changes: 61 additions & 0 deletions docs/navigation/stack/component-context.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# How to initialize Child Stack with custom ComponentContext

In order to pass this `AppComponentContext` to child components, make an extension function on the `AppComponentContext` interface. This custom extension function will initialize the `Child Stack` and provide child `AppComponentContext`.

```kotlin
fun <C : Parcelable, T : Any> AppComponentContext.appChildStack(
source: StackNavigationSource<C>,
initialStack: () -> List<C>,
configurationClass: KClass<out C>,
key: String = "DefaultStack",
handleBackButton: Boolean = false,
childFactory: (configuration: C, AppComponentContext) -> T
): Value<ChildStack<C, T>> =
childStack(
source = source,
initialStack = initialStack,
configurationClass = configurationClass,
key = key,
handleBackButton = handleBackButton
) { configuration, componentContext ->
childFactory(
configuration,
DefaultAppComponentContext(
componentContext = componentContext
)
)
}

inline fun <reified C : Parcelable, T : Any> AppComponentContext.appChildStack(
source: StackNavigationSource<C>,
noinline initialStack: () -> List<C>,
key: String = "DefaultStack",
handleBackButton: Boolean = false,
noinline childFactory: (configuration: C, AppComponentContext) -> T
): Value<ChildStack<C, T>> =
appChildStack(
source = source,
initialStack = initialStack,
configurationClass = C::class,
key = key,
handleBackButton = handleBackButton,
childFactory = childFactory,
)
```

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

```kotlin
class MyComponent(componentContext: AppComponentContext) : AppComponentContext by componentContext {

private val navigation = StackNavigation<Configuration>()

private val childStack = appChildStack(
source = navigation,
initialStack = { listOf(Configuration.Home) },
childFactory = { configuration, appComponentContext ->
// return child components using the custom component context
}
)
}
```
2 changes: 2 additions & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ nav:
- Navigation: navigation/stack/navigation.md
- Deep linking: navigation/stack/deeplinking.md
- Web browser history: navigation/stack/browser-history.md
- Custom ComponentContext: navigation/stack/component-context.md
- Child Overlay:
- Overview: navigation/overlay/overview.md
- Navigation: navigation/overlay/navigation.md
- Custom ComponentContext: navigation/overlay/component-context.md

- Extensions:
- Overview: extensions/overview.md
Expand Down

0 comments on commit 6238749

Please sign in to comment.