When I use interface like below in viewmodel
class MainViewModel @ViewModelInject constructor(
private val trafficImagesRepository: TrafficImageRepository, <----------------- Not working
@Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel()
I am getting an error like below
cannot be provided without an @Provides-annotated method.
public abstract static class SingletonC implements MainApplication_GeneratedInjector,
My interface is like below
interface TrafficImageRepository {
suspend fun getTrafficImages() : NetworkResponse<TrafficData, ErrorTrafficImages>
}
And Repository class is like below
class DefaultTrafficImagesRepository @Inject constructor(private val trafficImageService: TrafficImageService) : TrafficImageRepository {
override suspend fun getTrafficImages(): NetworkResponse<TrafficData, ErrorTrafficImages> {
lateinit var response: NetworkResponse<TrafficData, ErrorTrafficImages>
withContext(IO) {
val currentTimestamp = Constants.getCurrentTime()
response = trafficImageService.getTrafficImages(currentTimestamp)
}
return response
}
}
But when I use DefaultTrafficImagesRepository class directly instead of Interface my app is able to build without any error.
class MainViewModel @ViewModelInject constructor(
private val trafficImagesRepository: DefaultTrafficImagesRepository , <----------------- Working Fine
@Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel()
You must to bind interface
@Module
@InstallIn(ViewModelComponent::class)
abstract class RepositoryModule {
@Binds
abstract fun bindRepository(impl: DefaultTrafficImagesRepository): TrafficImageRepository
}
and use viewmodel inject in this way
@HiltViewModel
class MainViewModel @Inject constructor(
private val trafficImagesRepository: TrafficImageRepository,
@Assisted private val savedStateHandle: SavedStateHandle
) : ViewModel()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With