簡體   English   中英

如何使用 Assisted Inject 和 Inject with dagger hilt?

[英]How to use Assisted Inject and Inject with dagger hilt?

我有 ViewModel,我需要在運行時注入一些參數,但在這個 ViewModel 中我也需要存儲庫。 不確定匕首刀柄是否可以結合輔助注射和注射。 如果我現在嘗試使用 communityFeedRepository 它會給我錯誤:

com.test.context.community.repository.CommunityFeedRepository 在 com.test.context.community.CommunityFeedViewModel(..., communityFeedRepository) 處注入 com.test.context.community.CommunityFeedViewModel.Factory 在 com.test.context.di 請求。 ViewModelFactoryProvider.communityFeedViewModelFactory()

這是我的代碼:

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
    )
}

啊,我想我發現了錯誤,它工作正常。 如果有人遇到類似問題可能會有所幫助。 我有輔助模塊

@EntryPoint
@InstallIn(ActivityComponent::class)
interface ViewModelFactoryProvider {
    fun communityFeedViewModelFactory(): CommunityFeedViewModel.Factory
}

它安裝在 ActivityComponent 中。 我的 CommunityModule 安裝在 ViewModelComponent 中時遇到了問題。 我這樣做了,現在一切正常。

@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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM