Dmitry Sergienko
Dmitry Sergienko

Reputation: 115

For my interceptor I need use ifecycleOwner, I use the interceptor in Hilt module. But I dont know how get ifecycleOwner in Hilt Module?

@Module @InstallIn(SingletonComponent::class)

object AppEntryModule {

private val client = OkHttpClient.Builder().apply {
    addInterceptor(MyInterceptor(  ?????   ))
}.build()

@Provides
@Singleton
fun provideRetrofit(): Retrofit =
    Retrofit.Builder()
        .baseUrl(MOCK_URL)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())
        .build()

}

class MyInterceptor @Inject constructor( private val viewLifecycleOwner: LifecycleOwner ) : Interceptor { {}

Upvotes: 0

Views: 59

Answers (1)

Dmitry Sergienko
Dmitry Sergienko

Reputation: 115

I found solution:

object AppEntryModule {

@Provides
@Singleton
fun provideRetrofit(@ApplicationContext appContext: Context): Retrofit {
    val client = OkHttpClient.Builder().apply {
        addInterceptor(MyInterceptor(MyPreferences(appContext)))
    }.build()
     return  Retrofit.Builder()
        .baseUrl(MOCK_URL)
        .client(client)
        .addConverterFactory(GsonConverterFactory.create())
        .build()
}

Upvotes: 0

Related Questions