viewModelScope.launch { _stateFlow.update { it.copy(nowCopying = true) } withContext(singleThreadContext) { val fileSize = sourceUsbFile.length // singleThread val outputStream = BufferedOutputStream(targetFile.outputStream()) // IO val input = UsbFileStreamFactory.createBufferedInputStream(sourceUsbFile, fs) // singleThread val buffer = ByteArray(fs.chunkSize) var fullSize = 0L var resultSize = input.read(buffer) // singleThread var percent = 0 var newPercent = -1 while (resultSize != -1) { outputStream.write(buffer) // IO fullSize += resultSize newPercent = ((fullSize.toFloat() / fileSize.toFloat()) * 100).toInt() resultSize = input.read(buffer) // singleThread //println("Was read $fullSize") yield() if (newPercent != percent) { percent = newPercent deviceState = deviceState.copy(bytesWasReadPercent = percent) // TODO report progress } } outputStream.close() // IO } println("$sourceUsbFile copied") _stateFlow.update { it.copy(nowCopying = false) } }