I am quite new with android and have issue that I ...
# android
j
I am quite new with android and have issue that I do not know how to solve. I use Assisted injection with dagger hilt and I have ViewModelFactoryProvider that looks like this
Copy code
@EntryPoint
@InstallIn(ActivityComponent::class)
interface ViewModelFactoryProvider {
    fun communityFeedViewModelFactory(): CommunityFeedViewModel.Factory
    fun postTileViewModelFactory(): PostTileViewModel.Factory
    fun reactionViewModelFactory(): ReactionsViewModel.Factory
    fun commentViewModelFactory(): CommentViewModel.Factory
}
Here is for example how i use it in ReactionsViewModel
Copy code
class ReactionsViewModel
@AssistedInject
constructor(
    @Assisted private var reactions: PostReactions,
    @Assisted private var postId: String,
    private var reactionsRepository: ReactionsRepository,
) : ViewModel() {

    @AssistedFactory
    interface Factory {
        fun create(
            @Assisted reactions: PostReactions,
            @Assisted postId: String,
        ): ReactionsViewModel
    }

    @Suppress("UNCHECKED_CAST")
    companion object {
        fun provideFactory(
            assistedFactory: Factory,
            reactions: PostReactions,
            postId: String
        ): ViewModelProvider.Factory = object : ViewModelProvider.Factory {
            override fun <T : ViewModel> create(modelClass: Class<T>): T {
                return assistedFactory.create(reactions, postId) as T
            }
        }
    }
}

@Composable
fun reactionsViewModel(
    reactions: PostReactions,
    id: String
): ReactionsViewModel {
    val factory = EntryPointAccessors.fromActivity(
        LocalContext.current as Activity,
        ViewModelFactoryProvider::class.java
    ).reactionViewModelFactory()
    return viewModel(
        factory = ReactionsViewModel.provideFactory(
            factory,
            reactions,
            postId = id,
        ), key = id
    )
}
And here is how I provide reactions respository:
Copy code
@Module
@InstallIn(ActivityComponent::class)
object ReactionsModule {
    @Provides
    fun provideReactionApi(retrofit: Retrofit):
            ReactionsApi = retrofit.create(ReactionsApi::class.java)

    @Provides
    fun providesRRepository(ReactionsApi: ReactionsApi):
            ReactionsRepository =
        ReactionsRepositoryImpl(ReactionsApi)
}
Problem here is that I need to use ActivityComponent in module where I provide repository. If i try to access this reactions repository in any other viewmodel it crash because it needs to be ViewModelComponent. Is there any clever solution how to fix it?
­čžÁ 2
­čśÂ 2