-
|
For example: I have ProfileViewModel @AssistedInject
class ProfileViewModel(
@Assisted private val userId: Long,
private val analytics: Lazy<ProfileAnalytics>,
) : ViewModel() {
init {
analytics.value.sendAppear()
}
@AssistedFactory
@ManualViewModelAssistedFactoryKey(
value = Factory::class,
)
@ContributesIntoMap(
scope = AppScope::class,
)
interface Factory : ManualViewModelAssistedFactory {
fun create(@Assisted userId: Long): ProfileViewModel
}
}Also I have ProfileAnalytics class to send events into analytics. For this class I want to pass userId via constructor with others injects classes. class ProfileAnalytics(
private val userId: Long, <--- This field must be provided from ProfileViewModel
private val analytics: Lazy<Analytics>, <--- other injected class
) {
fun sendAppear() {
analytics.value.sendEvent(
name = "profile_appear",
properties = mapOf(
"user_id" to userId,
),
)
}
}Is it possible to do with metro? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
|
If you want it to participate in your DI graph you need to put that binding on your DI graph rather than have it be an assisted parameter. A common way people handle this scenario is they make a graph extension specific to that scope (for example a ProfileGraph) that takes that ID in as a @DependencyGraph
interface AppGraph {
// ...
val profileGraphFactory: ProfileGraph.Factory
}
@GraphExtension
interface ProfileGraph {
@GraphExtension.Factory
interface Factory {
fun create(@Provides userId: String): ProfileGraph
}
}Then everything in the scope of the |
Beta Was this translation helpful? Give feedback.
If you want it to participate in your DI graph you need to put that binding on your DI graph rather than have it be an assisted parameter. A common way people handle this scenario is they make a graph extension specific to that scope (for example a ProfileGraph) that takes that ID in as a
@Providesinput in its factory.Then everything in the scope of the
ProfileGraphwould have access to thatuserIdbinding in injection.