Hey everyone, I have a RecyclerView with items; ea...
# compose
t
Hey everyone, I have a RecyclerView with items; each item has an imagePath; in onBindViewHolder, a new coroutine is launched, to check if the image is already stored locally. If it is, it sets the image. If it isn't, it downloads and sets the image. This is the code inside onBindViewHolder:
Copy code
lifecycleScope.launch {
    val bitmap = withContext(<http://Dispatchers.IO|Dispatchers.IO>) {
        getLocalImageAsBitmap(imageName) ?: run {
            val file = requireContext().cacheDir.resolve("places").apply { mkdirs() }.resolve(imageName)
            try {
                repository.downloadFile(imageName, file)
                getLocalImageAsBitmap(imageName)
            } catch (e: Exception) {
                e.printStackTrace()
                null
            }
        } ?: return@withContext null
    }
    binding.imageView.setImageBitmap(bitmap)
}
How can I migrate this to Compose (LazyColumn)?
a
Easiest way would be to use the coil or glide extensions in the accompanist library, which will do all of this for you
Second easiest way is to use a
LaunchedEffect
in the item with
imageName
as a key param
t
First option probably isn't what I want, I get image from firebase SDK, not from URL
a
Or perhaps
produceState
as a shortcut to not need to define/remember your own
mutableStateOf
to hold the result for your composition
z
I don’t feel like having so much business code in your adapter makes sense. Have you already tried customizing your image loader to accept firebase links? The whole process of loading and cache can still be handled by the image loader instead of manually doing all that
t
Hm I haven't tried it, no. I'll give that some thought soon. For now, using produceState is working great, thanks very much 🙂