'How to use type parameters in dagger hilt module?

I have the following module:

@InstallIn(SingletonComponent::class)
@Module 
object WebServiceModule {
@Provides
fun provideUserApi(
    @ApplicationContext context: Context,
    settingsDataStore: DataStore<SettingsProto>,
    tokenDataStore: DataStore<TokenProto>
): WebApi<UserApi> {
    return WebApi(context, settingsDataStore, tokenDataStore, UserApi::class.java)
}

@Provides
fun provideWarehouseApi(
    @ApplicationContext context: Context,
    settingsDataStore: DataStore<SettingsProto>,
    tokenDataStore: DataStore<TokenProto>
): WebApi<WarehouseApi> {
    return WebApi(context, settingsDataStore, tokenDataStore, WarehouseApi::class.java)
}

@Provides
fun provideAccounntApi(
    @ApplicationContext context: Context,
    settingsDataStore: DataStore<SettingsProto>,
    tokenDataStore: DataStore<TokenProto>
): WebApi<AccountApi> {
    return WebApi(context, settingsDataStore, tokenDataStore, AccountApi::class.java)
} }

Do I need to add a separate provider function for each web service? I have 30 web services, it seems quite tedious and ugly to do so. I tried to change the module to the following:

@InstallIn(SingletonComponent::class)
@Module
object WebServiceModule {

    @Provides
    fun <T : Api> provideApi(
        @ApplicationContext context: Context,
        settingsDataStore: DataStore<SettingsProto>,
        tokenDataStore: DataStore<TokenProto>,
        c: Class<T>
    ): WebApi<T> {
        return WebApi(context, settingsDataStore, tokenDataStore, c)
    }
}

But I got this error:

error: @Provides methods may not have type parameters
    public final <T extends com.***.pda.data.api.base.Api>com.***.pda.data.api.base.WebApi<T> provideApi(@org.jetbrains.annotations.NotNull()

It looks as if dagger does not support generic types. How can I reduce the code in my module then?



Sources

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

Source: Stack Overflow

Solution Source