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

main.com.sceyt.chatuikit.data.managers.message.MessageEventManager.kt Maven / Gradle / Ivy

There is a newer version: 1.7.2
Show newest version
package com.sceyt.chatuikit.data.managers.message

import com.sceyt.chat.ChatClient
import com.sceyt.chat.models.channel.Channel
import com.sceyt.chat.models.message.Message
import com.sceyt.chat.models.message.Reaction
import com.sceyt.chat.sceyt_listeners.MessageListener
import com.sceyt.chatuikit.data.managers.message.handler.MessageEventHandlerImpl
import com.sceyt.chatuikit.data.managers.message.handler.MessageEventHandler
import com.sceyt.chatuikit.data.managers.message.event.ReactionUpdateEventData
import com.sceyt.chatuikit.data.managers.message.event.ReactionUpdateEventEnum
import com.sceyt.chatuikit.data.models.channels.SceytChannel
import com.sceyt.chatuikit.data.models.messages.SceytMessage
import com.sceyt.chatuikit.data.models.messages.SceytReaction
import com.sceyt.chatuikit.extensions.TAG
import com.sceyt.chatuikit.persistence.mappers.toSceytReaction
import com.sceyt.chatuikit.persistence.mappers.toSceytUiChannel
import com.sceyt.chatuikit.persistence.mappers.toSceytUiMessage
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.withContext

object MessageEventManager : MessageEventHandler.AllEventManagers {
    private var eventManager = MessageEventHandlerImpl(this)

    private val onMessageFlow_: MutableSharedFlow> = MutableSharedFlow(
        extraBufferCapacity = 5,
        onBufferOverflow = BufferOverflow.DROP_OLDEST)
    val onMessageFlow = onMessageFlow_.asSharedFlow()

    private val onDirectMessageFlow_: MutableSharedFlow = MutableSharedFlow(
        extraBufferCapacity = 5,
        onBufferOverflow = BufferOverflow.DROP_OLDEST)
    val onDirectMessageFlow = onDirectMessageFlow_.asSharedFlow()

    private val onMessageReactionUpdatedFlow_: MutableSharedFlow = MutableSharedFlow(
        extraBufferCapacity = 5,
        onBufferOverflow = BufferOverflow.DROP_OLDEST)
    val onMessageReactionUpdatedFlow = onMessageReactionUpdatedFlow_.asSharedFlow()


    private val onMessageEditedOrDeletedFlow_: MutableSharedFlow = MutableSharedFlow(
        extraBufferCapacity = 5,
        onBufferOverflow = BufferOverflow.DROP_OLDEST)
    val onMessageEditedOrDeletedFlow = onMessageEditedOrDeletedFlow_.asSharedFlow()


    private val onOutGoingMessageFlow_: MutableSharedFlow = MutableSharedFlow(
        extraBufferCapacity = 50,
        onBufferOverflow = BufferOverflow.DROP_OLDEST)
    val onOutgoingMessageFlow = onOutGoingMessageFlow_.asSharedFlow()


    init {
        ChatClient.getClient().addMessageListener(TAG, object : MessageListener {

            override fun onMessage(channel: Channel, message: Message) {
                eventManager.onMessage(channel.toSceytUiChannel(), message.toSceytUiMessage())
            }

            override fun onDirectMessage(message: Message) {
                eventManager.onDirectMessage(message.toSceytUiMessage())
            }

            override fun onMessageDeleted(message: Message?) {
                message ?: return
                eventManager.onMessageDeleted(message.toSceytUiMessage())
            }

            override fun onMessageEdited(message: Message?) {
                message ?: return
                eventManager.onMessageEdited(message.toSceytUiMessage())
            }

            override fun onReactionAdded(message: Message?, reaction: Reaction?) {
                if (message == null || reaction == null) return
                eventManager.onReactionAdded(message.toSceytUiMessage(), reaction.toSceytReaction())
            }

            override fun onReactionDeleted(message: Message?, reaction: Reaction?) {
                if (message == null || reaction == null) return
                eventManager.onReactionDeleted(message.toSceytUiMessage(), reaction.toSceytReaction())
            }

            override fun onMessageComposing(p0: String?, p1: String?) {
            }

            override fun onMessagePaused(p0: String?, p1: String?) {
            }
        })
    }


    override fun onMessage(channel: SceytChannel, message: SceytMessage) {
        onMessageFlow_.tryEmit(Pair(channel, message))
    }

    override fun onDirectMessage(message: SceytMessage) {
        onDirectMessageFlow_.tryEmit(message)
    }

    override fun onMessageDeleted(message: SceytMessage) {
        onMessageEditedOrDeletedFlow_.tryEmit(message)
    }

    override fun onMessageEdited(message: SceytMessage) {
        onMessageEditedOrDeletedFlow_.tryEmit(message)
    }

    override fun onReactionAdded(message: SceytMessage, reaction: SceytReaction) {
        onMessageReactionUpdatedFlow_.tryEmit(ReactionUpdateEventData(message, reaction, ReactionUpdateEventEnum.Add))
    }

    override fun onReactionDeleted(message: SceytMessage, reaction: SceytReaction) {
        onMessageReactionUpdatedFlow_.tryEmit(ReactionUpdateEventData(message, reaction, ReactionUpdateEventEnum.Remove))
    }

    fun setCustomListener(listener: MessageEventHandlerImpl) {
        eventManager = listener
        eventManager.setDefaultListeners(this)
    }

    suspend fun emitOutgoingMessage(sceytMessage: SceytMessage) {
        withContext(Dispatchers.Main) {
            onOutGoingMessageFlow_.emit(sceytMessage)
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy