All Downloads are FREE. Search and download functionalities are using the official Maven repository.

main.com.sceyt.chatuikit.persistence.dao.AttachmentDao.kt Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
package com.sceyt.chatuikit.persistence.dao

import androidx.room.Dao
import androidx.room.Query
import androidx.room.Transaction
import com.sceyt.chatuikit.data.models.LoadNearData
import com.sceyt.chatuikit.data.models.messages.AttachmentTypeEnum
import com.sceyt.chatuikit.persistence.entity.messages.AttachmentDb
import com.sceyt.chatuikit.persistence.entity.messages.AttachmentEntity
import com.sceyt.chatuikit.persistence.entity.messages.AttachmentPayLoadDb
import com.sceyt.chatuikit.persistence.file_transfer.TransferData
import com.sceyt.chatuikit.persistence.file_transfer.TransferState

@Dao
abstract class AttachmentDao {
    @Transaction
    @Query("select * from AttachmentEntity where channelId =:channelId and id != 0 and id <:attachmentId and type in (:types)" +
            "order by createdAt desc, id desc limit :limit")
    abstract suspend fun getOldestThenAttachment(channelId: Long, attachmentId: Long, limit: Int, types: List): List

    @Transaction
    @Query("select * from AttachmentEntity where channelId =:channelId and id != 0 and id >:attachmentId and type in (:types)" +
            "order by createdAt, id limit :limit")
    abstract suspend fun getNewestThenAttachment(channelId: Long, attachmentId: Long, limit: Int, types: List): List

    @Transaction
    @Query("select * from AttachmentEntity where channelId =:channelId and id >=:attachmentId and type in (:types)" +
            "order by createdAt, id limit :limit")
    abstract suspend fun getNewestThenMessageInclude(channelId: Long, attachmentId: Long, limit: Int, types: List): List

    @Transaction
    open suspend fun getNearAttachments(channelId: Long, attachmentId: Long, limit: Int, types: List): LoadNearData {
        val newest = getNewestThenMessageInclude(channelId, attachmentId, limit / 2 + 1, types)
        val newMessages = newest.take(limit / 1)

        val oldest = getOldestThenAttachment(channelId, attachmentId, limit - newMessages.size, types).reversed()
        val hasPrev = oldest.size == limit - newMessages.size
        val hasNext = newest.size > limit / 2
        return LoadNearData(oldest + newMessages, hasNext = hasNext, hasPrev)
    }

    @Transaction
    @Query("select * from AttachmentPayLoad where messageTid in (:tid)")
    abstract suspend fun getAllAttachmentPayLoadsByMsgTid(vararg tid: Long): List

    @Query("select * from AttachmentEntity where type =:type and url <> ''")
    abstract fun getAllFileAttachments(type: String = AttachmentTypeEnum.File.value): List

    @Query("update AttachmentEntity set id =:attachmentId, messageId =:messageId where messageTid =:messageTid and url =:attachmentUrl")
    abstract suspend fun updateAttachmentIdAndMessageId(attachmentId: Long?, messageId: Long, messageTid: Long, attachmentUrl: String?)

    @Query("update AttachmentPayLoad set progressPercent =:progress, transferState =:state where messageTid =:tid")
    abstract suspend fun updateAttachmentTransferDataByMsgTid(tid: Long, progress: Float, state: TransferState)

    @Transaction
    open suspend fun updateAttachmentAndPayLoad(transferData: TransferData) {
        updateAttachmentByMsgTid(transferData.messageTid, transferData.filePath, transferData.url)
        updateAttachmentPayLoadByMsgTid(transferData.messageTid, transferData.filePath, transferData.url,
            transferData.progressPercent, transferData.state)
    }

    @Transaction
    open suspend fun updateAttachmentFilePathAndMetadata(tid: Long, filePath: String?, fileSize: Long, metadata: String?) {
        updateAttachmentFilePathByMsgTid(tid, filePath, fileSize, metadata)
        updateAttachmentPayLoadFilePathByMsgTid(tid, filePath)
    }

    @Query("update AttachmentEntity set filePath =:filePath, url =:url where messageTid =:msgTid and type !=:ignoreType")
    abstract suspend fun updateAttachmentByMsgTid(msgTid: Long, filePath: String?, url: String?, ignoreType: String = AttachmentTypeEnum.Link.value)

    @Query("update AttachmentPayLoad set filePath =:filePath, url =:url," +
            "progressPercent= :progress, transferState =:state  where messageTid =:tid")
    abstract suspend fun updateAttachmentPayLoadByMsgTid(tid: Long, filePath: String?, url: String?, progress: Float, state: TransferState)

    @Query("update AttachmentEntity set filePath =:filePath, fileSize =:fileSize, metadata =:metadata " +
            "where messageTid =:msgTid and type !=:ignoreType")
    abstract suspend fun updateAttachmentFilePathByMsgTid(msgTid: Long, filePath: String?, fileSize: Long,
                                                          metadata: String?, ignoreType: String = AttachmentTypeEnum.Link.value)

    @Query("update AttachmentPayLoad set filePath =:filePath where messageTid =:msgTid")
    abstract suspend fun updateAttachmentPayLoadFilePathByMsgTid(msgTid: Long, filePath: String?)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy