package cn.flightfeather.thirdapp.repository
|
|
import cn.flightfeather.thirdapp.common.net.ResultCallBack
|
import cn.flightfeather.thirdapp.common.net.ResultObserver
|
import cn.flightfeather.thirdapp.common.net.RetrofitFactory
|
import cn.flightfeather.thirdapp.repository.dao.MediaFileDao
|
import retrofit2.Response
|
import java.io.File
|
|
/**
|
* @author riku
|
* Date: 2020/4/18
|
*/
|
class MediaFileRepository {
|
private val mediaFileDao = MediaFileDao()
|
|
fun getFileCache(url: String?, resultCallBack: ResultCallBack<String>) {
|
//url 不是网络路径时,返回其自身
|
if (url?.contains("http") != true) {
|
resultCallBack.onSuccess(url)
|
return
|
}
|
|
val dbService = mediaFileDao.getFileCache(url).map {
|
Response.success(it)
|
}
|
|
RetrofitFactory.executeResult(dbService, object : ResultObserver<String>() {
|
override fun onSuccess(result: String?) {
|
if (result.isNullOrBlank()) {
|
resultCallBack.onFailure()
|
} else {
|
val file = File(result)
|
if (file.exists()) {
|
resultCallBack.onSuccess(result)
|
} else {
|
resultCallBack.onFailure()
|
}
|
}
|
}
|
|
override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
|
resultCallBack.onFailure()
|
}
|
})
|
}
|
|
fun saveFileCache(url: String, localPath: String) {
|
mediaFileDao.saveFileCache(url, localPath)
|
}
|
}
|