Skip to content

Commit

Permalink
Enable network cache
Browse files Browse the repository at this point in the history
  • Loading branch information
vitalyster committed Dec 6, 2024
1 parent 77d665b commit fb951d3
Showing 1 changed file with 9 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/main/java/com/juick/App.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,11 @@ import java.util.concurrent.TimeUnit
* Created by gerc on 14.02.2016.
*/
class App : Application() {

private val cacheSize = (50 * 1024 * 1024).toLong()

val api: Api by lazy {
val networkCache = Cache(applicationContext.cacheDir, cacheSize)
val client = OkHttpClient.Builder()
.addInterceptor { chain: Interceptor.Chain ->
val original = chain.request()
Expand All @@ -73,10 +77,14 @@ class App : Application() {
}
}
}
if (BuildConfig.DEBUG && response.networkResponse == null) {
Log.d("App", "Cached response: ${request.url}")
}
response
}
.addInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))
.addNetworkInterceptor(HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.HEADERS))
.readTimeout(30, TimeUnit.SECONDS)
.cache(networkCache)
.build()
val retrofit = Retrofit.Builder()
.baseUrl(BuildConfig.API_ENDPOINT)
Expand Down

0 comments on commit fb951d3

Please sign in to comment.