Search code examples
androiddagger-hilt

How does Hilt inject ViewModel parameters


In the documentation on Hilt, it shows this example of injecting a viewmodel into an activity:

@HiltViewModel
class ExampleViewModel @Inject constructor(
  private val savedStateHandle: SavedStateHandle,
  private val repository: ExampleRepository
) : ViewModel() {
  ...
}
@AndroidEntryPoint
class ExampleActivity : AppCompatActivity() {
  private val exampleViewModel: ExampleViewModel by viewModels()
  ...
}

But what if ExampleRepository itself has a constructor that requires parameters? How would the code in the activity be different? How do you tell Hilt what parameters to pass to Repository?


Solution

  • there is multiple ways but I'll mention one I use for parameters that are from custom type like retrofit api service or OKHttp you need to provide it like below

    @Module
    @InstallIn(SingletonComponent::class)
    object NetworkModule {
        @Provides
        fun provideOkHttpClient(
            @ApplicationContext context: Context,
            networkManager: NetworkManager,
            authenticator: AuthInterceptor,
            preferenceHelper: PreferenceHelper
        ): OkHttpClient {
            val httpLogging = HttpLoggingInterceptor()
            httpLogging.level = HttpLoggingInterceptor.Level.BODY
            val httpClient = OkHttpClient.Builder()
                .addInterceptor(authenticator)
                .connectTimeout(5, TimeUnit.MINUTES)
                .callTimeout(5, TimeUnit.MINUTES)
                .readTimeout(2, TimeUnit.MINUTES)
                .writeTimeout(2, TimeUnit.MINUTES)
            if (BuildConfig.BUILD_TYPE == Constants.STAGING_RELEASE)
                httpClient.addInterceptor(httpLogging)
            httpClient.addInterceptor(ChuckInterceptor(context))
            val httpCacheDirectory = File(context.cacheDir, "responses")
            val cacheSize: Long = 10 * 1024 * 1024
            val cache = Cache(httpCacheDirectory, cacheSize)
            httpClient.cache(cache)
            return httpClient.build()
        }
    }
    in this way when a parameter of type OkHttpClient is needed, this function will return it