初始代码提交
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
package com.sw.inbound.utils
|
||||
|
||||
import com.sw.inbound.objbox.FoodCollectionBean
|
||||
import java.util.concurrent.atomic.AtomicInteger
|
||||
import kotlin.collections.chunked
|
||||
import kotlin.collections.count
|
||||
|
||||
class ImageUploader(
|
||||
private val totalList: List<FoodCollectionBean>,
|
||||
private val uploadImage: suspend (List<FoodCollectionBean>)-> Boolean,
|
||||
private val onProgress: (Int, List<FoodCollectionBean>) -> Unit,
|
||||
private val onError:(List<FoodCollectionBean>) -> Unit,
|
||||
private val onComplete:() -> Unit
|
||||
) {
|
||||
companion object {
|
||||
private const val BATCH_SIZE = 5
|
||||
}
|
||||
private val uploadedCount = AtomicInteger(0)
|
||||
|
||||
suspend fun processUploads() {
|
||||
val batches = totalList.chunked(BATCH_SIZE)
|
||||
|
||||
for (batch in batches) {
|
||||
val success = uploadImage(batch)
|
||||
if (!success) {
|
||||
//println("上传失败,终止流程")
|
||||
onError(batch)
|
||||
return
|
||||
}
|
||||
val fileCount = batch.count { it.imageFile!=null }
|
||||
uploadedCount.addAndGet(fileCount)
|
||||
onProgress(uploadedCount.get(), batch)
|
||||
//println("已上传 ${uploadedCount.get()}/$totalImages")
|
||||
}
|
||||
|
||||
onComplete()
|
||||
//println("流程完成,总计上传 ${uploadedCount.get()} 张图片")
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user