Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Proposal: Switch to just speak Call.Factory #8

Merged
merged 9 commits into from
Aug 19, 2019
Prev Previous commit
Next Next commit
Simplify sample
  • Loading branch information
ZacSweers committed Aug 19, 2019
commit 32d56dd4b79dbd70428d235784b61b2ad097a480
25 changes: 9 additions & 16 deletions coil-sample/src/main/java/coil/sample/Application.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,10 @@ import coil.Coil
import coil.ImageLoader
import coil.ImageLoaderBuilder.Companion.applyCoilOptimizations
import coil.util.CoilLogger
import okhttp3.Call
import okhttp3.OkHttpClient

class Application : MultiDexApplication() {

/**
* Clever technique with Call.Factory. By lazily constructing our OkHttpClient instance,
* we can defer its instantiation until the first network request and off the main thread.
*/
private val okHttpClient by lazy {
OkHttpClient.Builder()
.applyCoilOptimizations(this@Application)
.apply {
// The Unsplash API requires TLS 1.2, which isn't enabled by default before Lollipop.
if (SDK_INT < LOLLIPOP) forceTls12()
}
.build()
}

override fun onCreate() {
super.onCreate()
CoilLogger.setEnabled(true)
Expand All @@ -39,7 +24,15 @@ class Application : MultiDexApplication() {
availableMemoryPercentage(0.5)
bitmapPoolPercentage(0.5)
crossfade(true)
callFactory(Call.Factory { request -> okHttpClient.newCall(request) })
callFactory(
OkHttpClient.Builder()
.applyCoilOptimizations(this@Application)
.apply {
// The Unsplash API requires TLS 1.2, which isn't enabled by default before Lollipop.
if (SDK_INT < LOLLIPOP) forceTls12()
}
.build()
)
}
}
}