Skip to content

Instantly share code, notes, and snippets.

@colinrtwhite
Created August 15, 2020 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save colinrtwhite/90267704091467451e46b21b95154299 to your computer and use it in GitHub Desktop.
Save colinrtwhite/90267704091467451e46b21b95154299 to your computer and use it in GitHub Desktop.
MeasuredMapper -> Interceptor
class HttpUriInterceptor : Interceptor {
override suspend fun intercept(chain: Interceptor.Chain): ImageResult {
val data = chain.request.data
val request = if (data is Uri && handles(data)) {
chain.request.newBuilder().data(map(data, chain.size)).build()
} else {
chain.request
}
return chain.proceed(request)
}
private fun handles(data: Uri) = data.scheme == "http" || data.scheme == "https"
private fun map(data: Uri, size: Size): HttpUrl {
return data.toString()
.toHttpUrl()
.newBuilder()
.addQueryParameter("size", size.toString())
.build()
}
}
class HttpUriMeasuredMapper : MeasuredMapper<Uri, HttpUrl> {
override fun handles(data: Uri) = data.scheme == "http" || data.scheme == "https"
override fun map(data: Uri, size: Size): HttpUrl {
return data.toString()
.toHttpUrl()
.newBuilder()
.addQueryParameter("size", size.toString())
.build()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment