lelestacia
lelestacia

Reputation: 519

Kotlin Context Receiver with Dagger Hilt

So, I've been researching about Context Receiver in Kotlin. And I've been trying to test it in my test project. The way that I'm trying to reach it is by applying Context Receiver with Dagger Hilt.

I have an Interface implementation like this:

context (LoggerApi)
internal class AnimeRepositoryImpl @Inject constructor(
    private val remoteDataSource: AnimeRemoteDataSourceApi,
    private val localDataSource: AnimeLocalDataSourceApi,
) : AnimeRepository

And module like this:

@Provides
@Singleton
fun provideAnimeRepository(
   remoteDataSource: AnimeRemoteDataSourceApi,
   localDataSource: AnimeLocalDataSourceApi,
   loggerApi: LoggerApi
): AnimeRepository =
   with(loggerApi) {
      AnimeRepositoryImpl(
         remoteDataSource = remoteDataSource,
         localDataSource = localDataSource
      )
   }

And the code didn't compiled with an error from Hilt:

required: LoggerApi,AnimeRemoteDataSourceApi,AnimeLocalDataSourceApi
found:    AnimeRemoteDataSourceApi,AnimeLocalDataSourceApi
reason: actual and formal argument lists differ in length

And when i open the factory generated by Hilt. It looks like this:

public static AnimeRepositoryImpl newInstance(AnimeRemoteDataSourceApi remoteDataSource,
   AnimeLocalDataSourceApi localDataSource) {
      return new AnimeRepositoryImpl(remoteDataSource, localDataSource);
}

As we can see, the LoggerApi didn't get generated. But if i put the LoggerApi as constructor, it build just fine. What is happening in here? I also tried to put the Context Receiver on the Interface but this still happens. Does this means Hilt doesn't support Context Receiver as injection or something else?

Upvotes: 1

Views: 84

Answers (0)

Related Questions