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

main.com.sceyt.chatuikit.persistence.converters.MessageConverter.kt Maven / Gradle / Ivy

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

import androidx.room.TypeConverter
import com.google.gson.Gson
import com.google.gson.reflect.TypeToken
import com.sceyt.chat.models.message.BodyAttribute
import com.sceyt.chat.models.message.DeliveryStatus
import com.sceyt.chat.models.message.MarkerTotal
import com.sceyt.chat.models.message.MessageState
import com.sceyt.chatuikit.persistence.extensions.toEnum
import com.sceyt.chatuikit.persistence.file_transfer.TransferState

class MessageConverter {
    @TypeConverter
    fun deliveryStatusToTnt(value: DeliveryStatus) = value.ordinal

    @TypeConverter
    fun intToDeliveryStatus(value: Int) = value.toEnum()

    @TypeConverter
    fun messageStateToTnt(value: MessageState) = value.ordinal

    @TypeConverter
    fun intToMessageState(value: Int) = value.toEnum()

    @TypeConverter
    fun transferStateEnumToTnt(value: TransferState?) = value?.ordinal

    @TypeConverter
    fun intToTransferStateTypeEnum(value: Int?) = value?.toEnum()

    @TypeConverter
    fun stringToMarkerTotal(json: String?): List? {
        json ?: return null
        val type = object : TypeToken>() {}.type
        return try {
            Gson().fromJson(json, type)
        } catch (e: Exception) {
            null
        }
    }

    @TypeConverter
    fun markerCountToString(obj: List?): String? {
        if (obj == null)
            return null

        val gson = Gson()
        val type = object : TypeToken>() {}.type
        return gson.toJson(obj, type)
    }

    @TypeConverter
    fun stringToBodyAttribute(json: String?): List? {
        json ?: return null
        val type = object : TypeToken>() {}.type
        return try {
            Gson().fromJson(json, type)
        } catch (e: Exception) {
            null
        }
    }

    @TypeConverter
    fun bodyAttributeToString(obj: List?): String? {
        if (obj == null)
            return null

        val gson = Gson()
        val type = object : TypeToken>() {}.type
        return gson.toJson(obj, type)
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy