API interface dependency injection with hilt

Viewed 256

I am newbie at dagger hilt.

I have created implementation, where I inject my constructor such as :

class ListItemsPagingSourceFactory @Inject constructor(
    private val api: Api,
    private val pageErrorsQueue: ListItemsPagingErrorsQueue
) {

}

interface Api {

    fun getList(
        count: Int,
        offset: Int
    ) : ListItemResponse

I have an error with my api interface I only want to implement my methods to be used later. However error occurs like:

Error: [Hilt]
public abstract interface Api {
                ^

When I added @InstallIn @EntryPoint

To Api inteface:

error: [Hilt]
  @InstallIn, 'value' class is invalid or missing: @dagger.hilt.InstallIn({})
  [Hilt] Processing did not complete. See error above for details.

EDIT: I have very similar implementation, but with retrofit where I use (@Get,@Query etc) I would like to implement my own "API" that is just method which get data locally from service without launching database. This is code from retrofitApi

interface RetrofitApi {
    @GET("data/mine")
    suspend fun getDataItems(
        @Query("count") count: Int,
        @Query("size") size: Int
    ): DataItemsResponse

How to change it as just interface or abstract class, which has got only methods without launching DB => to not have Dagger screams in the log.

EDIT2:

This configuration doesn't work as well.

@InstallIn
@Module(includes = [AppModule.Api::class])
class AppModule {
    // interface with @Binds
    @Module
    interface Api {
        @Binds
        fun getList(
            count: Int,
            offset: Int
        ) : ListItemResponse
    }
}

error:

@Binds methods must have exactly one parameter, whose type is assignable to the return type
1 Answers

Since API is an abstract interface, you wouldn't add those annotations there. You'd add them to the concrete implementation. Whatever class has those Dagger needs to be able to instantiate, and it can't instantiate an abstract class.

Related