I'm trying to use Dagger Hilt with Retrofit and Corroutines to make a simple API consumption project. However, whenever I try to run the App, "DataQuoteApi cannot be provided without an @Provides-annotated method." And honestly I don't know how to solve this kind of problem, as I can't put a @Provides in the interface or something like that
My interface:
interface DataQuoteApi {
@GET("/quotes")
suspend fun getAllQuotes(): List<QuoteModel>
}
My repository:
class QuoteRepository @Inject constructor(
private var api:DataQuoteApi
) {
suspend fun getQuotes():List<QuoteModel>?{
return api.getAllQuotes()
}
}
My module:
@Module
@InstallIn(SingletonComponent::class)
object NetworkModule {
@Singleton
@Provides
fun provideRetrofit(): Retrofit {
return Retrofit
.Builder()
.baseUrl("https://thesimpsonsquoteapi.glitch.me")
.addConverterFactory(GsonConverterFactory.create()).build()
}
@Singleton
@Provides
fun provideGetQuotes(apiQuote : DataQuoteApi): QuoteRepository {
return QuoteRepository(apiQuote)
}
}
Meu ViewModel:
@HiltViewModel
class QuoteViewModel @Inject constructor(
private var repository: QuoteRepository
):ViewModel() {
var isLoading = MutableLiveData<Boolean>()
var quote = MutableLiveData<QuoteModel>()
fun getQuotes(){
viewModelScope.launch {
isLoading.postValue(true)
var response = repository.getQuotes()
if(!response.isNullOrEmpty()){
quote.postValue(response[0])
isLoading.postValue(false)
}
}
}
fun onCreate() {
viewModelScope.launch {
isLoading.postValue(true)
var allQuotes = repository.getQuotes()
if (!allQuotes.isNullOrEmpty()) {
quote.postValue(allQuotes[0])
isLoading.postValue(false)
}
}
}
}
Does anyone know what I'm doing wrong?