Start migrating Anvil KSP to Metro

This commit is contained in:
Jorge Martín
2025-08-20 15:29:50 +02:00
parent 2eacce47c0
commit 92a4b8b66b
703 changed files with 3523 additions and 2820 deletions

View File

@@ -1,3 +1,5 @@
import extension.setupAnvil
/*
* Copyright 2023, 2024 New Vector Ltd.
*
@@ -13,10 +15,12 @@ android {
namespace = "io.element.android.libraries.architecture"
}
setupAnvil()
dependencies {
api(projects.libraries.di)
api(projects.libraries.core)
api(libs.dagger)
api(libs.metro.runtime)
api(libs.appyx.core)
api(libs.androidx.lifecycle.runtime)
api(libs.molecule.runtime)

View File

@@ -11,6 +11,11 @@ import android.content.Context
import com.bumble.appyx.core.modality.BuildContext
import com.bumble.appyx.core.node.Node
import com.bumble.appyx.core.plugin.Plugin
import dev.zacsweers.metro.BindingContainer
import dev.zacsweers.metro.ContributesTo
import dev.zacsweers.metro.Multibinds
import io.element.android.libraries.di.AppScope
import kotlin.reflect.KClass
inline fun <reified N : Node> Node.createNode(
buildContext: BuildContext,
@@ -32,11 +37,11 @@ inline fun <reified N : Node> NodeFactoriesBindings.createNode(
buildContext: BuildContext,
plugins: List<Plugin> = emptyList()
): N {
val nodeClass = N::class.java
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.
val nodeFactory = nodeFactoryMap[nodeClass] ?: error("Cannot find NodeFactory for ${nodeClass.name}.")
val nodeFactory = nodeFactoryMap[nodeClass] ?: error("Cannot find NodeFactory for ${nodeClass.java.name}.")
@Suppress("UNCHECKED_CAST")
val castedNodeFactory = nodeFactory as? AssistedNodeFactory<N>
@@ -44,6 +49,8 @@ inline fun <reified N : Node> NodeFactoriesBindings.createNode(
return node as N
}
//@BindingContainer
interface NodeFactoriesBindings {
fun nodeFactories(): Map<Class<out Node>, AssistedNodeFactory<*>>
@Multibinds
fun nodeFactories(): Map<KClass<out Node>, AssistedNodeFactory<*>>
}

View File

@@ -8,10 +8,10 @@
package io.element.android.libraries.architecture
import com.bumble.appyx.core.node.Node
import dagger.MapKey
import dev.zacsweers.metro.MapKey
import kotlin.reflect.KClass
@Retention(AnnotationRetention.RUNTIME)
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
@Target(AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.FUNCTION, AnnotationTarget.CLASS)
@MapKey
annotation class NodeKey(val value: KClass<out Node>)