iroyo
01/04/2023, 4:16 PMonGloballyPositioned
on the Image
modifier to get the size and then set a global variable of an Interceptor.. What I have is something like:
Image(
painter = painter,
modifier = modifier.onGloballyPositioned {
coilInterceptor.config.params[PARAM_WIDTH] = it.size.width.toString()
},
coilInterceptor is a singleton Interceptor…. I’m trying to avoid this. I much rather prefer passing to the ImageRequest
directly the url with the size concatenatedZach Klippenstein (he/him) [MOD]
01/04/2023, 9:37 PMonSizeChanged
instead of onGloballyPositioned
– size has nothing to do with postioinAlbert Chang
01/05/2023, 2:39 AMsize
property in Coil's Interceptor.Chain interface so you shouldn't need to get the size here.iroyo
01/05/2023, 7:42 AMsize
method for the ImageRequest. It can receive a SizeResolver
interface but how do I get the width then? and more importantly how do I “append” this “info” (width) to the url? via an Interceptor or directly concatenating the result to the base url?iroyo
01/05/2023, 7:44 AMval painter = rememberAsyncImagePainter(
model = ImageRequest.Builder(LocalContext.current)
.size(SizeResolver {
// add to url?????
Size(??, ??)
})
.data(url)
.build(),
contentScale = contentScale,
imageLoader = context.imageLoader
)
I’m guessing this is what @Albert Chang is mentioningAlbert Chang
01/05/2023, 8:32 AMInterceptor
.Albert Chang
01/05/2023, 8:49 AMclass AddWidthParamInterceptor : Interceptor {
override suspend fun intercept(chain: Interceptor.Chain): ImageResult {
val request = chain.request
val width = chain.size.width
val newRequest = if (width is Dimension.Pixels) {
val newUri = Uri.parse(request.data as String)
.buildUpon()
.appendQueryParameter("width", width.px.toString())
.build()
request.newBuilder()
.data(newUri)
.build()
} else {
request
}
return chain.proceed(newRequest)
}
}
// Then add it to your image loader
ImageLoader.Builder(context)
.components {
add(AddWidthParamInterceptor())
}
.interceptorDispatcher(<http://Dispatchers.IO|Dispatchers.IO>)
.build()
iroyo
01/05/2023, 8:51 AM