riku
2022-02-18 d59d55575d913646b7a90fca651904ab889c6723
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package cn.flightfeather.thirdappmodule.repository
 
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.common.net.ResultObserver
import cn.flightfeather.thirdappmodule.common.net.RetrofitFactory
import cn.flightfeather.thirdappmodule.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)
    }
}