Skip to content

Instantly share code, notes, and snippets.

@iamnaran
Last active September 4, 2021 08:54
Show Gist options
  • Save iamnaran/d0fcd2d7b756485d6a4b2beb0716e1fd to your computer and use it in GitHub Desktop.
Save iamnaran/d0fcd2d7b756485d6a4b2beb0716e1fd to your computer and use it in GitHub Desktop.
NetModule injected at SingletonComponent - Hilt Retrofit
/*
* Created by NaRan on 8/18/21, 2:39 PM
* Last modified 8/18/21, 2:39 PM
* Copyright (c) 2021 .
* All rights reserved.
*
*/
import android.content.Context;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.concurrent.TimeUnit;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import dagger.hilt.InstallIn;
import dagger.hilt.android.qualifiers.ApplicationContext;
import dagger.hilt.components.SingletonComponent;
import hu.akarnokd.rxjava3.retrofit.RxJava3CallAdapterFactory;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
import retrofit2.converter.protobuf.ProtoConverterFactory;
import retrofit2.converter.scalars.ScalarsConverterFactory;
@InstallIn(SingletonComponent.class)
@Module
public class NetModule {
@Provides
@Singleton
public Gson provideGson() {
return new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create();
}
@Provides
@Singleton
public OkHttpClient provideOkHttpClient(@ApplicationContext Context context, HttpLoggingInterceptor httpLoggingInterceptor, SupportInterceptor supportInterceptor, UnAuthorizedInterceptor unAuthorizedInterceptor, HostSelectionInterceptor hostSelectionInterceptor) {
long cacheSize = 5 * 1024 * 1024;
Cache mCache = new Cache(context.getCacheDir(), cacheSize);
if (BuildConfig.DEBUG) {
return new OkHttpClient().newBuilder()
.cache(mCache)
.retryOnConnectionFailure(true)
.connectTimeout(200, TimeUnit.SECONDS)
.readTimeout(200, TimeUnit.SECONDS)
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(hostSelectionInterceptor)
.followRedirects(true)
.followSslRedirects(true)
.addInterceptor(supportInterceptor)
.addInterceptor(unAuthorizedInterceptor)
.build();
} else {
return new OkHttpClient().newBuilder()
.connectTimeout(200, TimeUnit.SECONDS)
.readTimeout(200, TimeUnit.SECONDS)
.retryOnConnectionFailure(true)
.followRedirects(true)
.followSslRedirects(true)
.addInterceptor(supportInterceptor)
.addInterceptor(hostSelectionInterceptor)
.addInterceptor(unAuthorizedInterceptor)
.build();
}
}
@Provides
@Singleton
public Retrofit provideRetrofitClient(ScalarsConverterFactory scalarsConverterFactory, ProtoConverterFactory protoConverterFactory, RxJava3CallAdapterFactory rxJava3CallAdapterFactory, OkHttpClient okHttpClient) {
// todo retrofit prod & dev
return new Retrofit.Builder()
.client(okHttpClient)
.baseUrl(BuildConfig.PRODUCTION_BASE_URL)
.addConverterFactory(scalarsConverterFactory)
.addConverterFactory(protoConverterFactory)
.addCallAdapterFactory(rxJava3CallAdapterFactory)
.build();
}
@Provides
@Singleton
public HttpLoggingInterceptor provideHttpLogInterceptor() {
return new HttpLoggingInterceptor()
.setLevel(HttpLoggingInterceptor.Level.HEADERS)
.setLevel(HttpLoggingInterceptor.Level.BODY);
}
@Provides
@Singleton
public HostSelectionInterceptor provideHostSelectionInterceptor() {
return new HostSelectionInterceptor();
}
@Provides
@Singleton
public SupportInterceptor provideSupportInterceptor() {
return new SupportInterceptor();
}
@Provides
@Singleton
public UnAuthorizedInterceptor provideUnAuthorizedInterceptor() {
return new UnAuthorizedInterceptor();
}
@Provides
@Singleton
public GsonConverterFactory providesGsonConverterFactory() {
return GsonConverterFactory.create();
}
@Provides
@Singleton
public RxJava3CallAdapterFactory providesRxJavaCallAdapterFactory() {
return RxJava3CallAdapterFactory.createAsync();
}
@Provides
@Singleton
ApiHelper provideApiHelper(ApiHelperImpl apiHelper) {
return apiHelper;
}
@Provides
@Singleton
public AuthApiService provideApiService(Retrofit retrofit) {
return retrofit.create(AuthApiService.class);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment