优化合并的进度展示。
This commit is contained in:
parent
c04dae1fe3
commit
6ffb08dc4f
@ -15,6 +15,7 @@ import kotlinx.coroutines.delay
|
|||||||
import kotlinx.coroutines.flow.Flow
|
import kotlinx.coroutines.flow.Flow
|
||||||
import kotlinx.coroutines.flow.flow
|
import kotlinx.coroutines.flow.flow
|
||||||
import kotlinx.coroutines.flow.flowOn
|
import kotlinx.coroutines.flow.flowOn
|
||||||
|
import kotlinx.coroutines.launch
|
||||||
import kotlinx.coroutines.withContext
|
import kotlinx.coroutines.withContext
|
||||||
import java.io.File
|
import java.io.File
|
||||||
import java.io.FileOutputStream
|
import java.io.FileOutputStream
|
||||||
@ -173,16 +174,34 @@ object PdfUtils {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
val merger = PDFMergerUtility()
|
val merger = PDFMergerUtility()
|
||||||
|
val total = 100
|
||||||
|
|
||||||
|
// 加载文件 0~30%
|
||||||
inputFiles.forEachIndexed { index, file ->
|
inputFiles.forEachIndexed { index, file ->
|
||||||
merger.addSource(file)
|
merger.addSource(file)
|
||||||
onProgress?.invoke(index + 1, inputFiles.size)
|
val progress = ((index + 1).toFloat() / inputFiles.size * 30).toInt()
|
||||||
delay(100)
|
onProgress?.invoke(progress, total)
|
||||||
|
delay(50)
|
||||||
}
|
}
|
||||||
|
|
||||||
merger.destinationFileName = outputFile.absolutePath
|
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())
|
merger.mergeDocuments(MemoryUsageSetting.setupTempFileOnly())
|
||||||
|
|
||||||
|
fakeJob.cancel()
|
||||||
|
onProgress?.invoke(100, total)
|
||||||
|
|
||||||
outputFile
|
outputFile
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
e.printStackTrace()
|
e.printStackTrace()
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user