'Hilt failed to provide enum values

I'm trying to inject a Deferred that contains enum value into ViewModel using Hilt.

Module:

@Module
@InstallIn(SingletonComponent::class)
object UserModule {
    @Provides
    @UserAuthorityAsync
    fun provideUserAuthorityAsync(
        authManager: AuthManager
    ): Deferred<Authority> = CoroutineScope(Dispatchers.IO).async {
        authManager.getAuthority()
    }
}

@Qualifier
@Retention(AnnotationRetention.BINARY)
annotation class UserAuthorityAsync

VM:

@HiltViewModel
class UserEditViewModel @Inject constructor(
    @UserAuthorityAsync val authority: Deferred<Authority>
) {}

Authority is an enum class that contains user access levels

This produces the following error:

/home/user/AndroidStudioProjects/app/app/build/generated/hilt/component_sources/debug/com/example/app/MainApplication_HiltComponents.java:157: error: [Dagger/MissingBinding] @com.example.app.di.UserAuthorityAsync kotlinx.coroutines.Deferred<? extends com.example.app.service.data.model.user.Authority> cannot be provided without an @Provides-annotated method.
  public abstract static class SingletonC implements FragmentGetContextFix.FragmentGetContextFixEntryPoint,
                         ^
      @com.example.app.di.UserAuthorityAsync kotlinx.coroutines.Deferred<? extends com.example.app.service.data.model.user.Authority> is injected at
          com.example.app.ui.screen.admin.user.UserEditViewModel(…, authority, …)
      com.example.app.ui.screen.admin.user.UserEditViewModel is injected at
          com.example.app.ui.screen.admin.user.UserEditViewModel_HiltModules.BindsModule.binds(arg0)
      @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at
          dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.example.app.MainApplication_HiltComponents.SingletonC → com.example.app.MainApplication_HiltComponents.ActivityRetainedC → com.example.app.MainApplication_HiltComponents.ViewModelC]

Seems like Hilt does not like enums since other types like String work fine. I've tried not using qualifier or annotation class but it does not work. Please help.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source