Other cleanup

This commit is contained in:
Benoit Marty 2025-09-05 14:58:31 +02:00 committed by Benoit Marty
parent ad8ad3d443
commit 8d533e8a20
11 changed files with 14 additions and 20 deletions

View file

@ -12,12 +12,11 @@ import android.content.ContextWrapper
import com.bumble.appyx.core.node.Node
import io.element.android.libraries.di.DependencyInjectionGraphOwner
inline fun <reified T : Any> Node.optionalBindings() = optionalBindings(T::class.java)
inline fun <reified T : Any> Node.bindings() = bindings(T::class.java)
inline fun <reified T : Any> Context.bindings() = bindings(T::class.java)
fun <T : Any> Context.bindings(klass: Class<T>): T {
// search dagger components in the context hierarchy
// search the components in the dependency injection graph
return generateSequence(this) { (it as? ContextWrapper)?.baseContext }
.plus(applicationContext)
.filterIsInstance<DependencyInjectionGraphOwner>()
@ -28,16 +27,13 @@ fun <T : Any> Context.bindings(klass: Class<T>): T {
?: error("Unable to find bindings for ${klass.name}")
}
fun <T : Any> Node.optionalBindings(klass: Class<T>): T? {
// search dagger components in node hierarchy
fun <T : Any> Node.bindings(klass: Class<T>): T {
// search the components in the node hierarchy
return generateSequence(this, Node::parent)
.filterIsInstance<DependencyInjectionGraphOwner>()
.map { it.graph }
.flatMap { it as? Collection<*> ?: listOf(it) }
.filterIsInstance(klass)
.firstOrNull()
}
fun <T : Any> Node.bindings(klass: Class<T>): T {
return optionalBindings(klass) ?: error("Unable to find bindings for ${klass.name}")
?: error("Unable to find bindings for ${klass.name}")
}

View file

@ -37,7 +37,7 @@ inline fun <reified N : Node> NodeFactoriesBindings.createNode(
val nodeClass = N::class
val nodeFactoryMap = nodeFactories()
// Note to developers: If you got the error below, make sure to build again after
// clearing the cache (sometimes several times) to let Dagger generate the NodeFactory.
// clearing the cache (sometimes several times) to let codegen generate the NodeFactory.
val nodeFactory = nodeFactoryMap[nodeClass] ?: error("Cannot find NodeFactory for ${nodeClass.java.name}.")
@Suppress("UNCHECKED_CAST")