riku
2021-02-25 e102578ebfc95c27aeb13dce13fb82af53a2bead
app/src/main/java/cn/flightfeather/thirdapp/repository/dao/MediaFileDao.kt
@@ -1,9 +1,14 @@
package cn.flightfeather.thirdapp.repository.dao
import cn.flightfeather.thirdapp.bean.entity.MediaFileCache
import cn.flightfeather.thirdapp.bean.entity.Mediafile
import cn.flightfeather.thirdapp.common.database.DbFactory
import cn.flightfeather.thirdapp.common.database.FlatSaveInterface
import cn.flightfeather.thirdapp.common.database.flatMapDbSaveResult
import com.ping.greendao.gen.MediaFileCacheDao
import com.ping.greendao.gen.MediafileDao
import io.reactivex.Observable
import org.greenrobot.greendao.AbstractDao
/**
 * @author riku
@@ -70,4 +75,47 @@
            true
        }
    }
    /**
     * 获取缓存的文件
     */
    fun getFileCache(url: String?): Observable<String> {
        return DbFactory.getGreenDaoObservable().map {
            val resultList = it.mediaFileCacheDao.queryBuilder()
                    .where(MediaFileCacheDao.Properties.Url.eq(url))
                    .list()
            if (resultList.isNotEmpty()) {
                resultList[0].path
            } else {
                ""
            }
        }
    }
    /**
     * 保存缓存的文件
     */
    fun saveFileCache(url: String, localPath: String, thumbnailPath: String? = null) {
        flatMapDbSaveResult(DbFactory.getInstance().mediaFileCacheDao,
                object : FlatSaveInterface<MediaFileCache, Long> {
                    override fun onQuery(dao: AbstractDao<MediaFileCache, Long>): List<MediaFileCache> {
                        return dao.queryBuilder()
                                .where(MediaFileCacheDao.Properties.Url.eq(url))
                                .list()
                    }
                    override fun onUpdate(list: List<MediaFileCache>) {
                        list[0].apply {
                            path = localPath
                        }
                    }
                    override fun onInsert(): List<MediaFileCache> {
                        return listOf(MediaFileCache().apply {
                            this.url = url
                            this.path = localPath
                        })
                    }
                })
    }
}