Skip to content
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

Describe in the documentation how one can extend ComponentContext #50

Closed
arkivanov opened this issue Jan 24, 2021 · 3 comments
Closed
Labels
good first issue Good for newcomers help wanted Extra attention is needed

Comments

@arkivanov
Copy link
Contributor

arkivanov commented Jan 24, 2021

Users are asking questions about how one can extend ComponentContext with additional app-wide things. I came up with a solution. Would be nice do add this as a suggestion to the docs. If somebody thinks this can be improved, please let me know.

interface AppComponentContext : ComponentContext {

    // Custom things here
}

class DefaultAppComponentContext(
    componentContext: ComponentContext,
    // Additional dependencies here
) : AppComponentContext, ComponentContext by componentContext {

    // Custom things implementation here
}

fun <C : Parcelable, T : Any> AppComponentContext.appRouter(
    initialConfiguration: () -> C,
    initialBackStack: () -> List<C> = ::emptyList,
    configurationClass: KClass<out C>,
    key: String = "DefaultRouter",
    handleBackButton: Boolean = false,
    componentFactory: (configuration: C, AppComponentContext) -> T
): Router<C, T> =
    router(
        initialConfiguration = initialConfiguration,
        initialBackStack = initialBackStack,
        configurationClass = configurationClass,
        key = key,
        handleBackButton = handleBackButton
    ) { configuration, componentContext ->
        componentFactory(
            configuration,
            DefaultAppComponentContext(
                componentContext = componentContext
            )
        )
    }

class MyComponent(componentContext: AppComponentContext): AppComponentContext by componentContext {
    
    // Use AppComponentContext here
}
@plusmobileapps
Copy link
Contributor

I think I understand your solution to creating a custom component context and put up a PR to add that. I'm not sure I fully understand the need for having a custom component context, but let me know if any changes are needed to make the new documentation more clear 😄

@arkivanov
Copy link
Contributor Author

I also don't fully understand why this might be used. But it was asked multiple times, hence the solution. Probably some global things that are needed in (almost) every component. So you don't need to explicitly pass it through all components.

@arkivanov
Copy link
Contributor Author

Closed via #52

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants