diff --git a/app/src/main/java/com/all/pdfreader/pro/app/util/PdfUtils.kt b/app/src/main/java/com/all/pdfreader/pro/app/util/PdfUtils.kt index 1c37798..688860a 100644 --- a/app/src/main/java/com/all/pdfreader/pro/app/util/PdfUtils.kt +++ b/app/src/main/java/com/all/pdfreader/pro/app/util/PdfUtils.kt @@ -15,6 +15,7 @@ import kotlinx.coroutines.delay import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.flow import kotlinx.coroutines.flow.flowOn +import kotlinx.coroutines.launch import kotlinx.coroutines.withContext import java.io.File import java.io.FileOutputStream @@ -173,16 +174,34 @@ object PdfUtils { try { val merger = PDFMergerUtility() + val total = 100 + + // 加载文件 0~30% inputFiles.forEachIndexed { index, file -> merger.addSource(file) - onProgress?.invoke(index + 1, inputFiles.size) - delay(100) + val progress = ((index + 1).toFloat() / inputFiles.size * 30).toInt() + onProgress?.invoke(progress, total) + delay(50) } merger.destinationFileName = outputFile.absolutePath + // 伪进度(30%~95%) + var fakeProgress = 30 + val fakeJob = launch { + while (fakeProgress < 95) { + fakeProgress += (1..3).random() // 每次前进一点点 + if (fakeProgress > 95) fakeProgress = 95 + onProgress?.invoke(fakeProgress, total) + delay((100L..300L).random()) // 模拟不均匀的进度 + } + } + merger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly()) + fakeJob.cancel() + onProgress?.invoke(100, total) + outputFile } catch (e: Exception) { e.printStackTrace()