Search code examples
androiddagger-2dagger-hilt

How can i reinitialise the Singleton Component in Hilt dagger?


I have a retrofit module in my project, Before login i want to use retrofit without headers, But after login i want to use retrofit with headers using Hilt Dagger. How can i do this?

@Module
@InstallIn(SingletonComponent::class)
object RetrofitDi {


@Provides
fun getBasePath(): String {
    return "http://abcd.com/"
}

@Provides
fun providesLoggingInterceptor(): HttpLoggingInterceptor {

    return HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY)
}

@Provides
fun providesOkHttpClients(@ApplicationContext context: Context, sharedPreference: SharedPreference, httpLoggingInterceptor: HttpLoggingInterceptor): OkHttpClient {
    val okhttpClient = OkHttpClient.Builder()
    okhttpClient.addInterceptor(httpLoggingInterceptor)
    okhttpClient.callTimeout(60, TimeUnit.SECONDS)
    okhttpClient.connectTimeout(60, TimeUnit.SECONDS)
    okhttpClient.writeTimeout(60, TimeUnit.SECONDS)
    okhttpClient.readTimeout(60, TimeUnit.SECONDS)
    val token = sharedPreference.getStringData(SharedPreference.AUTH_KEY)
    val user_id = sharedPreference.getStringData(SharedPreference.USER_ID)
    if (BuildConfig.DEBUG) {
        val interceptor = HttpLoggingInterceptor()
        interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS)
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY)
        okhttpClient.addInterceptor(interceptor)
        okhttpClient.addInterceptor(Interceptor { chain ->
            val response = chain.proceed(chain.request())
            if (!response.isSuccessful) {
                when (response.code) {
                    CommonUtils.ALREADY_LOGGED_IN -> {
                        
                        sharedPreference.setBoolean(SharedPreference.IS_LOGGED_IN, false)
                        sharedPreference.clear()
                        context.getCacheDir().delete()
                        val intent = Intent(context, LoginActivity::class.java)
                        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
                        context.startActivity(intent)
                    }
                }
            }
            response
        })
    }

    okhttpClient.addInterceptor(Interceptor { chain: Interceptor.Chain ->
        val builder1 = chain.request().newBuilder()
        var request: Request? = null
        if (!token.isEmpty()) {
            builder1.addHeader("auth_key", "" + token)
        }
        if (!user_id.isEmpty()) {
            builder1.addHeader("user_id", "" + user_id)
        }
        request = builder1.build()
        chain.proceed(request)
    })
    return okhttpClient.build()
}

@Provides
fun providesGSONConvertorFactory(): Converter.Factory {

    return GsonConverterFactory.create()
}

@Provides
fun providesRetrofit(baseUrl: String, convertor: Converter.Factory, okHttpClient: OkHttpClient): Retrofit {
    return Retrofit.Builder().baseUrl(baseUrl).addConverterFactory(convertor).client(okHttpClient).build()
}


@Provides
fun providesApiService(retrofit: Retrofit): ApiService {
    return retrofit.create(ApiService::class.java)
}
}

Singleton instance created once in hilt-dagger but token and user_id will be available after login. After login i will need new okhttpclient. I did it without DI. But don't know how to deal with hilt-dagger.


Solution

  • Better way to do this is to have your API service methods annotated with @Headers("Token-required") for the APIs which requires token. Then in your interceptor method check for this header as:

    if (request.header("Token-required") != null) {
            request = request.newBuilder()
                .addHeader("token", "your token value")
                .build()
    }