mirror of
https://github.com/CappielloAntonio/tempo.git
synced 2026-02-01 07:03:35 +00:00
Add proper offline/online request interceptors
This commit is contained in:
@@ -72,10 +72,12 @@ public class AlbumSongListClient {
|
||||
}
|
||||
|
||||
private OkHttpClient getOkHttpClient() {
|
||||
CacheUtil cacheUtil = new CacheUtil(context);
|
||||
|
||||
return new OkHttpClient.Builder()
|
||||
.addInterceptor(getHttpLoggingInterceptor())
|
||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
||||
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||
.cache(getCache())
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -122,10 +122,12 @@ public class BrowsingClient {
|
||||
}
|
||||
|
||||
private OkHttpClient getOkHttpClient() {
|
||||
CacheUtil cacheUtil = new CacheUtil(context);
|
||||
|
||||
return new OkHttpClient.Builder()
|
||||
.addInterceptor(getHttpLoggingInterceptor())
|
||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
||||
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||
.cache(getCache())
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -57,10 +57,12 @@ public class MediaAnnotationClient {
|
||||
}
|
||||
|
||||
private OkHttpClient getOkHttpClient() {
|
||||
CacheUtil cacheUtil = new CacheUtil(context);
|
||||
|
||||
return new OkHttpClient.Builder()
|
||||
.addInterceptor(getHttpLoggingInterceptor())
|
||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
||||
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||
.cache(getCache())
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -48,10 +48,12 @@ public class MediaLibraryScanningClient {
|
||||
}
|
||||
|
||||
private OkHttpClient getOkHttpClient() {
|
||||
CacheUtil cacheUtil = new CacheUtil(context);
|
||||
|
||||
return new OkHttpClient.Builder()
|
||||
.addInterceptor(getHttpLoggingInterceptor())
|
||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
||||
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||
.cache(getCache())
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -47,10 +47,12 @@ public class MediaRetrievalClient {
|
||||
}
|
||||
|
||||
private OkHttpClient getOkHttpClient() {
|
||||
CacheUtil cacheUtil = new CacheUtil(context);
|
||||
|
||||
return new OkHttpClient.Builder()
|
||||
.addInterceptor(getHttpLoggingInterceptor())
|
||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
||||
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||
.cache(getCache())
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -64,10 +64,12 @@ public class PlaylistClient {
|
||||
}
|
||||
|
||||
private OkHttpClient getOkHttpClient() {
|
||||
CacheUtil cacheUtil = new CacheUtil(context);
|
||||
|
||||
return new OkHttpClient.Builder()
|
||||
.addInterceptor(getHttpLoggingInterceptor())
|
||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
||||
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||
.cache(getCache())
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -47,10 +47,12 @@ public class SearchingClient {
|
||||
}
|
||||
|
||||
private OkHttpClient getOkHttpClient() {
|
||||
CacheUtil cacheUtil = new CacheUtil(context);
|
||||
|
||||
return new OkHttpClient.Builder()
|
||||
.addInterceptor(getHttpLoggingInterceptor())
|
||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
||||
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||
.cache(getCache())
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -47,10 +47,12 @@ public class SystemClient {
|
||||
}
|
||||
|
||||
private OkHttpClient getOkHttpClient() {
|
||||
CacheUtil cacheUtil = new CacheUtil(context);
|
||||
|
||||
return new OkHttpClient.Builder()
|
||||
.addInterceptor(getHttpLoggingInterceptor())
|
||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
||||
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||
.cache(getCache())
|
||||
.build();
|
||||
}
|
||||
|
||||
@@ -1,22 +1,32 @@
|
||||
package com.cappielloantonio.play.subsonic.utils;
|
||||
|
||||
import android.content.Context;
|
||||
import android.net.ConnectivityManager;
|
||||
import android.net.NetworkInfo;
|
||||
|
||||
import okhttp3.Interceptor;
|
||||
import okhttp3.Request;
|
||||
|
||||
public class CacheUtil {
|
||||
public static Interceptor onlineInterceptor = chain -> {
|
||||
private final Context context;
|
||||
|
||||
public CacheUtil(Context context) {
|
||||
this.context = context;
|
||||
}
|
||||
|
||||
public Interceptor onlineInterceptor = chain -> {
|
||||
okhttp3.Response response = chain.proceed(chain.request());
|
||||
int maxAge = 60;
|
||||
int maxAge = 60; // 60 seconds
|
||||
return response.newBuilder()
|
||||
.header("Cache-Control", "public, max-age=" + maxAge)
|
||||
.removeHeader("Pragma")
|
||||
.build();
|
||||
};
|
||||
|
||||
public static Interceptor offlineInterceptor = chain -> {
|
||||
public Interceptor offlineInterceptor = chain -> {
|
||||
Request request = chain.request();
|
||||
if (!true) {
|
||||
int maxStale = 60 * 60 * 24 * 30;
|
||||
if (!isConnected()) {
|
||||
int maxStale = 60 * 60 * 24 * 30; // 30 days (60 seconds * 60 minutes * 24 hours * 30 days)
|
||||
request = request.newBuilder()
|
||||
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
|
||||
.removeHeader("Pragma")
|
||||
@@ -24,4 +34,10 @@ public class CacheUtil {
|
||||
}
|
||||
return chain.proceed(request);
|
||||
};
|
||||
|
||||
private boolean isConnected(){
|
||||
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
|
||||
return (netInfo != null && netInfo.isConnected());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user