how to add progress dialog when i read all byte in multiple file for kotlin or java

146 Views Asked by At

I have created a multiple file download folder from firebase storage, then I want to read all bytes for all the files that I downloaded earlier and write them on a third party fingerprint device. but I have a problem with the progressdialog always closing before all files are written on my fingerprint device so the Android UI freezes. The following is an example of the code I used before. (I downloaded the file based on my arraylist value.)

val storageReference =
        FirebaseStorage.getInstance().reference.child("finger-blok/fingertemplate${FileBin.name!!.replace(" ", "")}.bin")
    val rootPath = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        File(
            Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
                .toString() + "/" + "finger-blok"
        )
    } else {
        File(
            Environment.getExternalStorageDirectory()
                .toString() + "/${Environment.DIRECTORY_DOWNLOADS}/" + "finger-blok"
        )
    }

    if (!rootPath.exists()) {
        rootPath.mkdirs()
    }
    val localFile = File(rootPath, "fingertemplate${FileBin.name!!.replace(" ", "")}.bin")
    val task = storageReference.getFile(localFile)

    task.addOnCompleteListener {
        Log.e("localfile is create", "${it.result.bytesTransferred}, fingerid : ${FileBin.fingerid}, ${localFile.path}")
        progessdialog2 = ProgressDialog(context, R.style.AppCompatAlertDialogStyle).apply {
            this.setCancelable(false)
            this.setMessage("Write Data ...")
            progessdialog.dismiss()
            this.show()
        }
        when{
            !progessdialog.isShowing->{
                if (progessdialog2.isShowing){
                    val filebyteserver: Task<ByteArray> = Files.readAllBytes(localFile.toPath()) as Task<ByteArray>
                    filebyteserver.addOnCompleteListener { byte->
                        statuswr = writeTemplate(FileBin.fingerid!!.toInt(), byte.result)
                        Log.e("status_write_template", "Status Write template ${FileBin.name} : $statuswr")
                        if (statuswr){
                            localFile.delete()
                        }
                        Log.e("BYTEARRAY", byte.result.contentToString()+", ${FileBin.name!!.count()}")
                        //ControlBluetooth.getInstance()!!.showToast("Tulis Fingerprint $statuswr")
                        val toast = Toast.makeText(
                            context,
                            "Tulis Fingerprint $statuswr",
                            Toast.LENGTH_SHORT
                        )
                        toast.show()
                    }
                }
                progessdialog2.dismiss() 
            }
        }

    }.addOnProgressListener {
        var persen = it.bytesTransferred/it.totalByteCount * 100
        progessdialog.setMessage("Update Data $persen")
    }.addOnFailureListener {
        Log.e("ErrorDownload File", "${it.message}")
    }
0

There are 0 best solutions below