Inheritance in custom ComponentContext since 3.0.0-alpha09 and childFactory #679
-
With the changes of The scenario In addition I have an interface AppComponentContext : GenericComponentContext<AppComponentContext> {
// additional context data
}
interface AccessComponentContext : AppComponentContext {
val accessLevel: AccessLevel
} Now, when I want to create a class UserAccessComponent(
componentContext: AccessComponentContext,
// ...
) {
//...
private val stack = childStack(
source = navigation,
serializer = Configuration.serializer(),
initialStack = { listOf(Configuration.Home) },
handleBackButton = true,
childFactory = ::createChild,
)
override val childStack: Value<ChildStack<*, Child>> = stack
private fun createChild(
configuration: Configuration,
componentContext: AppComponentContext, // <-- before it was AccessComponentContext
): Child {
// all the child components use AccessComponentContext, so this had to be added:
require(componentContext is AccessComponentContext)
return when (configuration) {
// ...
}
} Is there a better way to handle this kind of abstraction without adding custom extension functions again? To solve this in a different way I was thinking of something like generics in the custom ComponentContext or not using inheritance at all in custom components contexts, but I am not sure if that will improve the overall code. This however would require different implementation in order to be able to switch between two or more contexts. |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Thanks for the feedback! Can you try something as follows? interface BaseComponentContext<out T : Any> : GenericComponentContext<T> {
// additional context data
}
interface AppComponentContext : BaseComponentContext<AppComponentContext>
interface AccessComponentContext : BaseComponentContext<AccessComponentContext> {
val accessLevel: AccessLevel
} |
Beta Was this translation helpful? Give feedback.
Yeah, in this case it requires some more code.