Skip to content

Commit

Permalink
Move setting headers recipe to network.md.
Browse files Browse the repository at this point in the history
  • Loading branch information
colinrtwhite committed Nov 6, 2024
1 parent d019ce1 commit 35c45b6
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 69 deletions.
45 changes: 45 additions & 0 deletions coil-network-core/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,48 @@ OkHttpNetworkFetcherFactory(

!!! Note
You need to enable `coreLibraryDesugaring` to support Android API level 25 or below. Follow the docs [here](https://developer.android.com/studio/write/java8-support#library-desugaring) to enable it.

#### Headers

Headers can be added to your image requests in one of two ways. You can set headers for a single request:

```kotlin
val headers = NetworkHeaders.Builder()
.set("Cache-Control", "no-cache")
.build()
val request = ImageRequest.Builder(context)
.data("https://example.com/image.jpg")
.httpHeaders(headers)
.target(imageView)
.build()
imageLoader.execute(request)
```

Or you can create an OkHttp [`Interceptor`](https://square.github.io/okhttp/interceptors/) that sets headers for every request executed by your `ImageLoader`:

```kotlin
class RequestHeaderInterceptor(
private val name: String,
private val value: String,
) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val headers = NetworkHeaders.Builder()
.set("Cache-Control", "no-cache")
.build()
val request = chain.request().newBuilder()
.httpHeaders(headers)
.build()
return chain.proceed(request)
}
}

val imageLoader = ImageLoader.Builder(context)
.okHttpClient {
OkHttpClient.Builder()
// This header will be added to every image request.
.addNetworkInterceptor(RequestHeaderInterceptor("Cache-Control", "no-cache"))
.build()
}
.build()
```
69 changes: 0 additions & 69 deletions docs/recipes.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,75 +25,6 @@ imageView.load("https://example.com/image.jpg") {
}
```

## Using a custom OkHttpClient

If you use `io.coil-kt.coil3:coil-network-okhttp` You can specify a custom `OkHttpClient` when creating your `ImageLoader`:

```kotlin
val imageLoader = ImageLoader.Builder(context)
// Create the OkHttpClient inside a lambda so it will be initialized lazily on a background thread.
.components {
add(
OkHttpNetworkFetcherFactory(
callFactory = {
OkHttpClient.Builder()
.addInterceptor(CustomInterceptor())
.build()
}
)
)
}
.build()
```

!!! Note
If you already have a built `OkHttpClient`, use [`newBuilder()`](https://square.github.io/okhttp/5.x/okhttp/okhttp3/-ok-http-client/#customize-your-client-with-newbuilder) to build a new client that shares resources with the original.

#### Headers

Headers can be added to your image requests in one of two ways. You can set headers for a single request:

```kotlin
val headers = NetworkHeaders.Builder()
.set("Cache-Control", "no-cache")
.build()
val request = ImageRequest.Builder(context)
.data("https://example.com/image.jpg")
.httpHeaders(headers)
.target(imageView)
.build()
imageLoader.execute(request)
```

Or you can create an OkHttp [`Interceptor`](https://square.github.io/okhttp/interceptors/) that sets headers for every request executed by your `ImageLoader`:

```kotlin
class RequestHeaderInterceptor(
private val name: String,
private val value: String,
) : Interceptor {

override fun intercept(chain: Interceptor.Chain): Response {
val headers = NetworkHeaders.Builder()
.set("Cache-Control", "no-cache")
.build()
val request = chain.request().newBuilder()
.httpHeaders(headers)
.build()
return chain.proceed(request)
}
}

val imageLoader = ImageLoader.Builder(context)
.okHttpClient {
OkHttpClient.Builder()
// This header will be added to every image request.
.addNetworkInterceptor(RequestHeaderInterceptor("Cache-Control", "no-cache"))
.build()
}
.build()
```

## Using a Memory Cache Key as a Placeholder

Using a previous request's `MemoryCache.Key` as a placeholder for a subsequent request can be useful if the two images are the same, though loaded at different sizes. For instance, if the first request loads the image at 100x100 and the second request loads the image at 500x500, we can use the first image as a synchronous placeholder for the second request.
Expand Down

0 comments on commit 35c45b6

Please sign in to comment.