Jetpack Compose progress indicator

84 Views Asked by At

My code downloads several images from the cloud to the cache at the same time without showing them on the screen, the code works fine

val loaderlistener = object : progressRespondBody.progressListener {
                    
                    override fun Update(byteread: Long, contentLength: Long, done: Boolean) {
                        100 * byteread / contentLength != (100).toLong()

                        Log.i("progressListener_image", "done: ${100 * byteread / contentLength}");
                    }
                }

                val mediaPathList: ArrayList<String> = ArrayList()
                
                val imageloader = ImageLoader.Builder(context)
                    .memoryCachePolicy(CachePolicy.DISABLED)
                    .crossfade(true)
                    .allowHardware(false)
                    .okHttpClient {
                        OkHttpClient().newBuilder()
                            .addInterceptor(Interceptor { chain ->
                                val orginalres = chain.proceed(chain.request());
                                orginalres.newBuilder()
                                    .body(progressRespondBody(orginalres.body!!,loaderlistener))
                                    .build()
                            })
                            .build()
                    }
                    .diskCache {
                        if (save_local_external) {
                            path =              
                   context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!.path.toPath()
                            DiskCache.Builder()
                                .maxSizePercent(0.4)
                                .directory(path)
                                .build()
                        } else {
                            DiskCache.Builder()
                                .maxSizePercent(0.1)
                                .directory(path)
                                .build()
                        }
                    }
                     .build()

                  imagelist?.forEach { imageUrl ->
                    val request = ImageRequest.Builder(context)
                        .data(imageUrl)
                        .crossfade(true)
                        .build()
                     imageloader.enqueue(request)

                    imagelist.take(3).forEachIndexed { index, _ ->
                    val mediaPath =  
                   imageloader.diskCache?.openSnapshot(MemoryCache.Key(imagelist[index]).key)?.data.toString()
                        mediaPathList.add(mediaPath)
                    }                    
                }             
                

I want the user to show the download progress and after downloading the download window is closed when I do this, the download process repeats endlessly and throws an error How do I get this?

val progressState = remember { mutableStateOf(0f) }
                val loaderlistener = remember {
                    object : progressRespondBody.progressListener {
                        override fun Update(byteread: Long, contentLength: Long, done: Boolean) {
                            if (contentLength != (100).toLong()) {
                                val newProgress = 100 * byteread / contentLength
                                progressState.value = newProgress.toFloat()

                            }
                            
                        }
                    }
                }
val mediaPathList: ArrayList<String> = ArrayList()

                val imageloader = ImageLoader.Builder(context)
                    .memoryCachePolicy(CachePolicy.DISABLED)
                    .crossfade(true)
                    .allowHardware(false)
                    .okHttpClient {
                        OkHttpClient().newBuilder()
                            .addInterceptor(Interceptor { chain ->
                                val orginalres = chain.proceed(chain.request());
                                orginalres.newBuilder()
                                    .body(progressRespondBody(orginalres.body!!,loaderlistener))
                                    .build()
                            })
                            .build()
                    }
                    .diskCache {
                        if (save_local_external) {
                            path =
                                context.getExternalFilesDir(Environment.DIRECTORY_PICTURES)!!.path.toPath()
                            DiskCache.Builder()
                                .maxSizePercent(0.4)
                                .directory(path)
                                .build()
                        } else {
                            DiskCache.Builder()
                                .maxSizePercent(0.1)
                                .directory(path)
                                .build()
                        }
                    }
                    .build()
imagelist?.forEach { imageUrl ->
                    val request = ImageRequest.Builder(context)
                        .data(imageUrl)
                        .crossfade(true)
                        .build()
                    imageloader.enqueue(request)

                    imagelist.take(3).forEachIndexed { index, _ ->
                        val mediaPath = imageloader.diskCache?.openSnapshot(MemoryCache.Key(imagelist[index]).key)?.data.toString()
                        mediaPathList.add(mediaPath)
                    }
                    if (clear){
                        imageloader.diskCache?.clear()
                    }
                }
LinearProgressIndicator(
                   progress = progressState.value,
                   modifier = Modifier
                      .fillMaxWidth()
                      .height(16.dp)
             )
0

There are 0 best solutions below