This is the module Class
@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Provides
fun getAppPreferences(
@ApplicationContext context: ApplicationContext,
@Named("appName") appName: String
):SharedPreferences {
return (context as Context).getSharedPreferences(appName, Context.MODE_PRIVATE)
}
@Provides
@Named("URL")
fun getBaseUrl(): String {
return AppConstants.BASE_URL
}
@Singleton
@Provides
fun getApiService(
client: OkHttpClient, @Named("URL") baseURL: String,
factory: GsonConverterFactory
): ApiInterface {
return Retrofit.Builder()
.baseUrl(baseURL)
.addConverterFactory(factory)
.client(client)
.build()
.create(ApiInterface::class.java)
}
@Singleton
@Provides
fun getClient(sharedPreferences: SharedPreferences): OkHttpClient {
val interceptor = HttpLoggingInterceptor()
interceptor.level = HttpLoggingInterceptor.Level.BODY
return OkHttpClient().newBuilder()
.addInterceptor(interceptor)
.addInterceptor { chain ->
val requestBuilder = chain.request().newBuilder()
val token = ProjectUtil.getApiTokenBearer(sharedPreferences)
token?.apply {
requestBuilder.addHeader("Authorization", token)
}
chain.proceed(requestBuilder.build())
}
.connectTimeout(100, TimeUnit.SECONDS)
.writeTimeout(100, TimeUnit.SECONDS)
.readTimeout(100, TimeUnit.SECONDS)
.build()
}
@Singleton
@Provides
fun getConvertorFactory(): GsonConverterFactory {
val gson = GsonBuilder()
.setLenient()
.create()
return GsonConverterFactory.create(gson)
}
@Provides
@Named("appName")
fun getAppName() = "CarApplication"
}
This is my viewmodel constructor
@HiltViewModel
class HomeFragmentViewModel @Inject constructor(
val sharedPreferences: SharedPreferences,
private val apiService: ApiInterface
) : ViewModel() {
private lateinit var createEquriumDialog: Dialog
// val progress_dialog = MyOwnProgressDialog(context)
var showAquariumRv: MutableLiveData<Boolean> = MutableLiveData()
var equariumList: ListUserAquariumResponse? = null
val goToActivityIntent: MutableLiveData<Intent> = MutableLiveData()
val progress: MutableLiveData<Boolean> = MutableLiveData()
val showMessage: MutableLiveData<String> = MutableLiveData()
}
i get this error when i run the application which i think donot Inject the application context required in the Module any help would be apreciated
C:\XYZ\XYZ\AndroidStudioProjects\CarAndroid\app\build\generated\source\kapt\debug\com\sw\car\baseclasses\CarApp_HiltComponents.java:128: error: [Dagger/MissingBinding] @dagger.hilt.android.qualifiers.ApplicationContext dagger.hilt.android.qualifiers.ApplicationContext cannot be provided without an @Provides-annotated method. public abstract static class SingletonC implements DaluaApp_GeneratedInjector, ^ @dagger.hilt.android.qualifiers.ApplicationContext dagger.hilt.android.qualifiers.ApplicationContext is injected at com.sw.car.di.modules.AppModule.getAppPreferences(context, �) android.content.SharedPreferences is injected at com.sw.car.ui.home.fragments.home.HomeFragmentViewModel(sharedPreferences, �) com.sw.car.ui.home.fragments.home.HomeFragmentViewModel is injected at com.sw.car.ui.home.fragments.home.HomeFragmentViewModel_HiltModules.BindsModule.binds(vm) @dagger.hilt.android.internal.lifecycle.HiltViewModelMap java.util.Map<java.lang.String,javax.inject.Provider<androidx.lifecycle.ViewModel>> is requested at dagger.hilt.android.internal.lifecycle.HiltViewModelFactory.ViewModelFactoriesEntryPoint.getHiltViewModelMap() [com.devstudio.dalua.baseclasses.CarApp_HiltComponents.SingletonC ? com.devstudio.dalua.baseclasses.CarApp_HiltComponents.ActivityRetainedC ? com.devstudio.dalua.baseclasses.CarApp_HiltComponents.ViewModelC]