0

I new to Jetpack compose and i'am creating a view to download a file from my api, i have a call back to give me the % or the download and i would like it display it on a progress bar. The problem is that everytime i update the progressbar, it seems to update all my view causing multiples call to my api. Here is my code :

@Composable
fun DownloadView() {
val isDownloading = remember { mutableStateOf(false) }

var downloadProgress by remember { mutableStateOf(0.1f) }
val animatedProgress = animateFloatAsState(
    targetValue = downloadProgress,
    animationSpec = ProgressIndicatorDefaults.ProgressAnimationSpec
).value

println("Recompose view") //this is called 100+ times

Button(
    modifier = Modifier
        .fillMaxWidth()
        .padding(horizontal = 8.dp),
    shape = RoundedCornerShape(10.dp),
    colors = ButtonDefaults.buttonColors(backgroundColor = colorResource(id = R.color.blue)),
    onClick = {
        println("click download")
        if (!isDownloading.value) {
            isDownloading.value = true
            Api.download() //stating the download from my api
            downloadProgress = Api.callback() //call back of the download progress 
            //when api is done i also get a call back the i set isDownloading.value = false
            }
        }
    },) {

    var text = "Download"
    if (isDownloading.value) {
        text = "Downloading"
    }
    Text(text = text, color = Color.White)
    if (isDownloading.value) {
        CircularProgressIndicator(
            animatedProgress,
            color = colorResource(id = R.color.orange),
            modifier = Modifier.size(28.dp).padding(start = 4.dp)
        )
    }
}

}

This is working, i got my button with the progress, it change the button text when i'am downloading and i update the progress bar. The problem is that my whole view is updated everytime the value of the progress is update. Is there a way to avoid this or did i missunderstand something ? Thanks for the help.

XCarb
  • 735
  • 10
  • 31

0 Answers0