private val fetch = Fetch.getInstance(FetchConfiguration.Builder(ContextUtils.getApplicationContext())
.setLogger(FetchLogger(true,“动画下载”))
.enableLogging(true)
.setDownloadConcurrentLimit(5)
.setAutoRetryMaxAttempts(3)
.setHttpDownloader(OkHttpDownloader(Downloader.FileDownloaderType.PARALLEL))
.setProgressReportingInterval(1000)
.build())
/**
- 下载监听
*/
class DownloadListener(private val groupId : Int,private val num:Int,private val callback 😦(Boolean) -> Unit) ?) : AbstractFetchListener(){
private var downloaded: Int = 0 //已下载的数量
override fun onCompleted(download: Download) {
if (download.group != groupId) {
return
}
//资源打包成zip文件放到了服务器上,所以下载下来要解压缩
LottieAnimationUtils.unZip(download.file, parentPath)
downloadCount(true)
}
override fun onError(download: Download, error: Error, throwable: Throwable?) {
if (download.group != groupId) {
return
}
downloadCount(false)
}
private val downloadCount: (Boolean) -> Unit = {
downloaded++
if (downloaded == num) {
callback?.invoke(it)
fetch.removeListener(this)
}
}
}
fun downloadList(pathList: List, priority: Priority = Priority.NORMAL, callback: ((Boolean) -> Unit)? = null) {
val groupId = Random.nextInt()
pathList.filter {
//本地文件不存在的才下载
!File(parentPath + File.separator + it).exists()
}.flatMap { path ->
arrayListOf(Request(“ F i l e D o w n l o a d C o n s t a n t . B a s e C o n f i g . G I F T P A T H / {FileDownloadConstant.BaseConfig.GIFT_PATH}/ F