mirror of
https://github.com/antebudimir/tempus.git
synced 2025-12-31 17:43:32 +00:00
Add proper offline/online request interceptors
This commit is contained in:
parent
ac584974c6
commit
55b4d97195
9 changed files with 53 additions and 21 deletions
|
|
@ -72,10 +72,12 @@ public class AlbumSongListClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private OkHttpClient getOkHttpClient() {
|
private OkHttpClient getOkHttpClient() {
|
||||||
|
CacheUtil cacheUtil = new CacheUtil(context);
|
||||||
|
|
||||||
return new OkHttpClient.Builder()
|
return new OkHttpClient.Builder()
|
||||||
.addInterceptor(getHttpLoggingInterceptor())
|
.addInterceptor(getHttpLoggingInterceptor())
|
||||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||||
.cache(getCache())
|
.cache(getCache())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -122,10 +122,12 @@ public class BrowsingClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private OkHttpClient getOkHttpClient() {
|
private OkHttpClient getOkHttpClient() {
|
||||||
|
CacheUtil cacheUtil = new CacheUtil(context);
|
||||||
|
|
||||||
return new OkHttpClient.Builder()
|
return new OkHttpClient.Builder()
|
||||||
.addInterceptor(getHttpLoggingInterceptor())
|
.addInterceptor(getHttpLoggingInterceptor())
|
||||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||||
.cache(getCache())
|
.cache(getCache())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -57,10 +57,12 @@ public class MediaAnnotationClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private OkHttpClient getOkHttpClient() {
|
private OkHttpClient getOkHttpClient() {
|
||||||
|
CacheUtil cacheUtil = new CacheUtil(context);
|
||||||
|
|
||||||
return new OkHttpClient.Builder()
|
return new OkHttpClient.Builder()
|
||||||
.addInterceptor(getHttpLoggingInterceptor())
|
.addInterceptor(getHttpLoggingInterceptor())
|
||||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||||
.cache(getCache())
|
.cache(getCache())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -48,10 +48,12 @@ public class MediaLibraryScanningClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private OkHttpClient getOkHttpClient() {
|
private OkHttpClient getOkHttpClient() {
|
||||||
|
CacheUtil cacheUtil = new CacheUtil(context);
|
||||||
|
|
||||||
return new OkHttpClient.Builder()
|
return new OkHttpClient.Builder()
|
||||||
.addInterceptor(getHttpLoggingInterceptor())
|
.addInterceptor(getHttpLoggingInterceptor())
|
||||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||||
.cache(getCache())
|
.cache(getCache())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,10 +47,12 @@ public class MediaRetrievalClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private OkHttpClient getOkHttpClient() {
|
private OkHttpClient getOkHttpClient() {
|
||||||
|
CacheUtil cacheUtil = new CacheUtil(context);
|
||||||
|
|
||||||
return new OkHttpClient.Builder()
|
return new OkHttpClient.Builder()
|
||||||
.addInterceptor(getHttpLoggingInterceptor())
|
.addInterceptor(getHttpLoggingInterceptor())
|
||||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||||
.cache(getCache())
|
.cache(getCache())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -64,10 +64,12 @@ public class PlaylistClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private OkHttpClient getOkHttpClient() {
|
private OkHttpClient getOkHttpClient() {
|
||||||
|
CacheUtil cacheUtil = new CacheUtil(context);
|
||||||
|
|
||||||
return new OkHttpClient.Builder()
|
return new OkHttpClient.Builder()
|
||||||
.addInterceptor(getHttpLoggingInterceptor())
|
.addInterceptor(getHttpLoggingInterceptor())
|
||||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||||
.cache(getCache())
|
.cache(getCache())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,10 +47,12 @@ public class SearchingClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private OkHttpClient getOkHttpClient() {
|
private OkHttpClient getOkHttpClient() {
|
||||||
|
CacheUtil cacheUtil = new CacheUtil(context);
|
||||||
|
|
||||||
return new OkHttpClient.Builder()
|
return new OkHttpClient.Builder()
|
||||||
.addInterceptor(getHttpLoggingInterceptor())
|
.addInterceptor(getHttpLoggingInterceptor())
|
||||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||||
.cache(getCache())
|
.cache(getCache())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -47,10 +47,12 @@ public class SystemClient {
|
||||||
}
|
}
|
||||||
|
|
||||||
private OkHttpClient getOkHttpClient() {
|
private OkHttpClient getOkHttpClient() {
|
||||||
|
CacheUtil cacheUtil = new CacheUtil(context);
|
||||||
|
|
||||||
return new OkHttpClient.Builder()
|
return new OkHttpClient.Builder()
|
||||||
.addInterceptor(getHttpLoggingInterceptor())
|
.addInterceptor(getHttpLoggingInterceptor())
|
||||||
.addInterceptor(CacheUtil.offlineInterceptor)
|
.addInterceptor(cacheUtil.offlineInterceptor)
|
||||||
.addNetworkInterceptor(CacheUtil.onlineInterceptor)
|
.addNetworkInterceptor(cacheUtil.onlineInterceptor)
|
||||||
.cache(getCache())
|
.cache(getCache())
|
||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,22 +1,32 @@
|
||||||
package com.cappielloantonio.play.subsonic.utils;
|
package com.cappielloantonio.play.subsonic.utils;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.net.ConnectivityManager;
|
||||||
|
import android.net.NetworkInfo;
|
||||||
|
|
||||||
import okhttp3.Interceptor;
|
import okhttp3.Interceptor;
|
||||||
import okhttp3.Request;
|
import okhttp3.Request;
|
||||||
|
|
||||||
public class CacheUtil {
|
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());
|
okhttp3.Response response = chain.proceed(chain.request());
|
||||||
int maxAge = 60;
|
int maxAge = 60; // 60 seconds
|
||||||
return response.newBuilder()
|
return response.newBuilder()
|
||||||
.header("Cache-Control", "public, max-age=" + maxAge)
|
.header("Cache-Control", "public, max-age=" + maxAge)
|
||||||
.removeHeader("Pragma")
|
.removeHeader("Pragma")
|
||||||
.build();
|
.build();
|
||||||
};
|
};
|
||||||
|
|
||||||
public static Interceptor offlineInterceptor = chain -> {
|
public Interceptor offlineInterceptor = chain -> {
|
||||||
Request request = chain.request();
|
Request request = chain.request();
|
||||||
if (!true) {
|
if (!isConnected()) {
|
||||||
int maxStale = 60 * 60 * 24 * 30;
|
int maxStale = 60 * 60 * 24 * 30; // 30 days (60 seconds * 60 minutes * 24 hours * 30 days)
|
||||||
request = request.newBuilder()
|
request = request.newBuilder()
|
||||||
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
|
.header("Cache-Control", "public, only-if-cached, max-stale=" + maxStale)
|
||||||
.removeHeader("Pragma")
|
.removeHeader("Pragma")
|
||||||
|
|
@ -24,4 +34,10 @@ public class CacheUtil {
|
||||||
}
|
}
|
||||||
return chain.proceed(request);
|
return chain.proceed(request);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
private boolean isConnected(){
|
||||||
|
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||||
|
NetworkInfo netInfo = connectivityManager.getActiveNetworkInfo();
|
||||||
|
return (netInfo != null && netInfo.isConnected());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue