Skip to content

Instantly share code, notes, and snippets.

@julianfalcionelli
Created November 28, 2017 18:35
Show Gist options
  • Save julianfalcionelli/5732b658334bbd9170b6dbf3d5ff296e to your computer and use it in GitHub Desktop.
Save julianfalcionelli/5732b658334bbd9170b6dbf3d5ff296e to your computer and use it in GitHub Desktop.
Retrofit Manager + Offline Mode + Cache Mode Full example
public class RetrofitManager {
public static final String TAG = "RetrofitManager";
public static final String BASE_URL = "https://lateralview.co";
public static final String HEADER_CACHE_CONTROL = "Cache-Control";
public static final String HEADER_PRAGMA = "Pragma";
private Context mContext;
private Retrofit mRetrofit, mCachedRetrofit;
private Cache mCache;
private OkHttpClient mOkHttpClient, mCachedOkHttpClient;
public RetrofitManager(Context context) {
mContext = context;
}
public Retrofit getRetrofit() {
if (mRetrofit == null) {
// Add all interceptors you want (headers, URL, logging)
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
.addInterceptor(provideOfflineCacheInterceptor())
.addNetworkInterceptor(provideCacheInterceptor())
.cache(provideCache());
mOkHttpClient = httpClient.build();
mRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(new Gson()))
// Add your adapter factory to handler Errors
.client(mOkHttpClient)
.build();
}
return mRetrofit;
}
public Retrofit getCachedRetrofit() {
if (mCachedRetrofit == null) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder()
// Add all interceptors you want (headers, URL, logging)
.addInterceptor(provideForcedOfflineCacheInterceptor())
.cache(provideCache());
mCachedOkHttpClient = httpClient.build();
mCachedRetrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create(new Gson()))
.client(mCachedOkHttpClient)
.build();
}
return mCachedRetrofit;
}
private Cache provideCache() {
if (mCache == null) {
try {
mCache = new Cache(new File(mContext.getCacheDir(), "http-cache"),
10 * 1024 * 1024); // 10 MB
} catch (Exception e) {
Log.e(TAG, "Could not create Cache!");
}
}
return mCache;
}
private Interceptor provideCacheInterceptor() {
return chain -> {
Response response = chain.proceed(chain.request());
CacheControl cacheControl;
if (isConnected()) {
cacheControl = new CacheControl.Builder()
.maxAge(0, TimeUnit.SECONDS)
.build();
} else {
cacheControl = new CacheControl.Builder()
.maxStale(7, TimeUnit.DAYS)
.build();
}
return response.newBuilder()
.removeHeader(HEADER_PRAGMA)
.removeHeader(HEADER_CACHE_CONTROL)
.header(HEADER_CACHE_CONTROL, cacheControl.toString())
.build();
};
}
private Interceptor provideOfflineCacheInterceptor() {
return chain -> {
Request request = chain.request();
if (!isConnected()) {
CacheControl cacheControl = new CacheControl.Builder()
.maxStale(7, TimeUnit.DAYS)
.build();
request = request.newBuilder()
.removeHeader(HEADER_PRAGMA)
.removeHeader(HEADER_CACHE_CONTROL)
.cacheControl(cacheControl)
.build();
}
return chain.proceed(request);
};
}
private Interceptor provideForcedOfflineCacheInterceptor() {
return chain -> {
Request request = chain.request();
CacheControl cacheControl = new CacheControl.Builder()
.maxStale(7, TimeUnit.DAYS)
.build();
request = request.newBuilder()
.removeHeader(HEADER_PRAGMA)
.removeHeader(HEADER_CACHE_CONTROL)
.cacheControl(cacheControl)
.build();
return chain.proceed(request);
};
}
public void clean() {
if (mOkHttpClient != null) {
// Cancel Pending Request
mOkHttpClient.dispatcher().cancelAll();
}
if (mCachedOkHttpClient != null) {
// Cancel Pending Cached Request
mCachedOkHttpClient.dispatcher().cancelAll();
}
mRetrofit = null;
mCachedRetrofit = null;
if (mCache != null) {
try {
mCache.evictAll();
} catch (IOException e) {
Log.e(TAG, "Error cleaning http cache");
}
}
mCache = null;
}
private boolean isConnected() {
try {
android.net.ConnectivityManager e = (android.net.ConnectivityManager) mContext.getSystemService(
Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = e.getActiveNetworkInfo();
return activeNetwork != null && activeNetwork.isConnectedOrConnecting();
} catch (Exception e) {
Log.w(TAG, e.toString());
}
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment