Thread
#compose
    Tiago Nunes

    Tiago Nunes

    1 year ago
    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:
    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)?
    Adam Powell

    Adam Powell

    1 year ago
    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
    Tiago Nunes

    Tiago Nunes

    1 year ago
    First option probably isn't what I want, I get image from firebase SDK, not from URL
    Adam Powell

    Adam Powell

    1 year ago
    Or perhaps
    produceState
    as a shortcut to not need to define/remember your own
    mutableStateOf
    to hold the result for your composition
    z

    Zun

    1 year ago
    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
    Tiago Nunes

    Tiago Nunes

    1 year ago
    Hm I haven't tried it, no. I'll give that some thought soon. For now, using produceState is working great, thanks very much 🙂