[英]How to use Assisted Inject and Inject with dagger hilt?
I have ViewModel where I need in runtime inject some parameters but also in this ViewModel i need repository.我有 ViewModel,我需要在运行时注入一些参数,但在这个 ViewModel 中我也需要存储库。 Not sure is it possible with dagger hilt to combine Assisted Inject and Inject.
不确定匕首刀柄是否可以结合辅助注射和注射。 If I try now to use communityFeedRepository it gives me error:
如果我现在尝试使用 communityFeedRepository 它会给我错误:
com.test.context.community.repository.CommunityFeedRepository is injected at com.test.context.community.CommunityFeedViewModel(…, communityFeedRepository) com.test.context.community.CommunityFeedViewModel.Factory is requested at com.test.context.di.ViewModelFactoryProvider.communityFeedViewModelFactory()
com.test.context.community.repository.CommunityFeedRepository 在 com.test.context.community.CommunityFeedViewModel(..., communityFeedRepository) 处注入 com.test.context.community.CommunityFeedViewModel.Factory 在 com.test.context.di 请求。 ViewModelFactoryProvider.communityFeedViewModelFactory()
Here is my code:这是我的代码:
class CommunityFeedViewModel
@AssistedInject
constructor(
@Assisted("preloadedTiles") private val preloadedTiles: List<Tile>,
@Assisted("path") private val path: String,
@Assisted("token") private val token: String?,
// private var communityFeedRepository: CommunityFeedRepository
) : ViewModel() {
var tiles: List<Tile> = preloadedTiles
private val vmPath = path
var vmToken: String? = token
private val tileState = MutableStateFlow(tiles)
val til = tileState.asStateFlow()
private suspend fun loadTiles() {
/*communityFeedRepository.getCommunityFeedTiles(vmPath, vmToken).onSuccess {
tileState.value = it.tiles
vmToken = it.token
}*/
}
init {
viewModelScope.launch {
if (tiles.isEmpty()) {
loadTiles()
}
}
}
@AssistedFactory
interface Factory {
fun create(
@Assisted("preloadedTiles") preloadedTiles: List<Tile>,
@Assisted("path") path: String,
@Assisted("token") token: String?
): CommunityFeedViewModel
}
@Suppress("UNCHECKED_CAST")
companion object {
fun provideFactory(
assistedFactory: Factory,
preloadedTiles: List<Tile>,
path: String,
token: String?
): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
return assistedFactory.create(preloadedTiles, path, token) as T
}
}
}
}
@Composable
fun communityFeedViewModel(
preloadedTiles: List<Tile>,
path: String,
token: String?
): CommunityFeedViewModel {
val factory = EntryPointAccessors.fromActivity(
LocalContext.current as Activity,
ViewModelFactoryProvider::class.java
).communityFeedViewModelFactory()
return viewModel(
factory = CommunityFeedViewModel.provideFactory(
factory,
preloadedTiles = preloadedTiles,
path = path,
token = token
), key = path
)
}
Ah I think I found error and it works fine.啊,我想我发现了错误,它工作正常。 If somebody run into similar issues might be helpful.
如果有人遇到类似问题可能会有所帮助。 I have AssistedModule
我有辅助模块
@EntryPoint
@InstallIn(ActivityComponent::class)
interface ViewModelFactoryProvider {
fun communityFeedViewModelFactory(): CommunityFeedViewModel.Factory
}
and it is installed in ActivityComponent.它安装在 ActivityComponent 中。 I had issues where my CommunityModule had install in ViewModelComponent.
我的 CommunityModule 安装在 ViewModelComponent 中时遇到了问题。 I make it like this and now all works fine.
我这样做了,现在一切正常。
@Module
@InstallIn(ActivityComponent::class)
object CommunityFeedModule {
@Provides
fun provideCommunityFeedApi(retrofit: Retrofit): CommunityFeedApi = retrofit.create(CommunityFeedApi::class.java)
@Provides
fun provideCommunityFeedRepository(communityFeedApi: CommunityFeedApi): CommunityFeedRepository =
CommunityFeedRepositoryImpl(communityFeedApi)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.