s3rius
03/19/2024, 8:19 AMchildContext()
function to shadow ComponentContext.childContext().
interface AppContext : ComponentContext {
val parentContext: AppContext?
}
private class DefaultAppContext(override val parentContext: AppContext?, baseContext: ComponentContext) :
AppContext,
ComponentContext by baseContext
fun AppContext.childContext(key: String) = DefaultAppContext(this, childContext(key)) // <- this actually resolves fine
All-in-all this actually works quite nicely; most details are abstracted away and I can easily provide some functionality to all components. With the proper TestContext testing is no problem too.
With the new factory, I think it would change into this:
private class DefaultAppContext(
override val parentContext: AppContext?,
override val lifecycle: Lifecycle,
override val stateKeeper: StateKeeper
override val instanceKeeper: InstanceKeeper
override val backHandler: BackHandler
) : AppContext, ComponentContext {
override val componentContextFactory = AppComponentContextFactory(this)
}
class AppComponentContextFactory(val self: AppContext) : ComponentContextFactory<AppContext> {
operator fun invoke(
lifecycle: Lifecycle,
stateKeeper: StateKeeper,
instanceKeeper: InstanceKeeper,
backHandler: BackHandler,
) = DefaultAppContext(self, lifecycle, StateKeeper, instanceKeeper, backHandler)
}
Basically the same; probably a little nicer due to the removal of the childContext override.
If you have an idea for a more elegant solution, I'd love to hear it!