Dagger Hilt: How to pass an interface as a parameter to another class using dagger hilt in android studio?

Viewed 35

I want to pass Callbacks which is an interface to this RepositoryImpl class like this:

class RepositoryImpl @Inject constructor(
   private val callbacks: Callbacks
) : Repository { }

This is the interface:

interface Callbacks {

   fun onSomethingHappened()
}

And I want to use this Callbacks interface in a fragment like this:

@AndroidEntryPoint
class MainFragment : Fragment(), Callbacks {

    override fun onSomethingHappened() {}
}

How should I write the Hilt Module to be able to provide the Callbacks interface to the RepositoryImpl class?

This is what my current project looks like without the Callbacks interface:

AppMain.kt:

@HiltAndroidApp
class AppMain : Application()

MainActivity.kt

@AndroidEntryPoint
class MainActivity : AppCompatActivity(){}

MainFragment.kt

@AndroidEntryPoint
class MainFragment : Fragment() {

    @Inject
    lateinit var repository: Repository
}

RepositoryImpl.kt

class RepositoryImpl @Inject constructor() : Repository { }

Repository.kt

interface Repository { }

HiltModule.kt

@Module
@InstallIn(SingletonComponent::class)
abstract class RepositoryModule {

    @Binds
    abstract fun bindRepository(repositoryImpl: RepositoryImpl): Repository
}
0 Answers
Related