Рабочие счетчики и переработка чата
This commit is contained in:
@@ -22,12 +22,30 @@ data class ReadMessagesRequest(
|
||||
val lastReadSequenceId: Int
|
||||
)
|
||||
|
||||
data class MessagesReadEvent(
|
||||
@com.google.gson.annotations.SerializedName("chatId", alternate = ["ChatId"]) val chatId: String? = null,
|
||||
@com.google.gson.annotations.SerializedName("userId", alternate = ["UserId"]) val userId: String? = null,
|
||||
@com.google.gson.annotations.SerializedName("lastReadSequenceId", alternate = ["LastReadSequenceId"]) val lastReadSequenceId: Int? = null
|
||||
) {
|
||||
val effectiveChatId: String get() = chatId ?: ""
|
||||
val effectiveUserId: String get() = userId ?: ""
|
||||
val effectiveLastReadSequenceId: Int get() = lastReadSequenceId ?: 0
|
||||
}
|
||||
|
||||
data class ReactionEvent(
|
||||
@com.google.gson.annotations.SerializedName("messageId", alternate = ["MessageId"]) val messageId: String? = null,
|
||||
@com.google.gson.annotations.SerializedName("chatId", alternate = ["ChatId"]) val chatId: String? = null,
|
||||
@com.google.gson.annotations.SerializedName("userId", alternate = ["UserId"]) val userId: String? = null,
|
||||
@com.google.gson.annotations.SerializedName("username", alternate = ["Username", "UserName"]) val username: String? = null,
|
||||
@com.google.gson.annotations.SerializedName("emoji", alternate = ["Emoji"]) val emoji: String? = null
|
||||
)
|
||||
|
||||
enum class ConnectionStatus { CONNECTED, CONNECTING, DISCONNECTED }
|
||||
|
||||
@Singleton
|
||||
class ChatHubClient @Inject constructor() {
|
||||
private var hubConnection: HubConnection? = null
|
||||
private val _events = MutableSharedFlow<ChatEvent>(extraBufferCapacity = 64)
|
||||
private val _events = MutableSharedFlow<ChatEvent>(extraBufferCapacity = 1024)
|
||||
val events: SharedFlow<ChatEvent> = _events.asSharedFlow()
|
||||
|
||||
private val _status = MutableStateFlow(ConnectionStatus.DISCONNECTED)
|
||||
@@ -77,13 +95,17 @@ class ChatHubClient @Inject constructor() {
|
||||
_events.tryEmit(ChatEvent.NewMessage(message))
|
||||
}, MessageDto::class.java)
|
||||
|
||||
conn.on("messages_read", { chatId: String, userId: String, lastReadSequenceId: Int ->
|
||||
_events.tryEmit(ChatEvent.MessagesRead(chatId, userId, lastReadSequenceId))
|
||||
}, String::class.java, String::class.java, Int::class.java)
|
||||
conn.on("messages_read", { data: MessagesReadEvent ->
|
||||
_events.tryEmit(ChatEvent.MessagesRead(
|
||||
data.effectiveChatId,
|
||||
data.effectiveUserId,
|
||||
data.effectiveLastReadSequenceId
|
||||
))
|
||||
}, MessagesReadEvent::class.java)
|
||||
|
||||
conn.on("user_typing", { chatId: String, userId: String ->
|
||||
_events.tryEmit(ChatEvent.UserTyping(chatId, userId))
|
||||
}, String::class.java, String::class.java)
|
||||
conn.on("user_typing", { data: ReactionEvent ->
|
||||
_events.tryEmit(ChatEvent.UserTyping(data.chatId ?: "", data.userId ?: ""))
|
||||
}, ReactionEvent::class.java)
|
||||
|
||||
conn.on("user_online", { userId: String ->
|
||||
_events.tryEmit(ChatEvent.UserOnline(userId))
|
||||
@@ -93,14 +115,23 @@ class ChatHubClient @Inject constructor() {
|
||||
_events.tryEmit(ChatEvent.NewChat(chat))
|
||||
}, ChatDto::class.java)
|
||||
|
||||
conn.on("reaction_added", { messageId: String, chatId: String, userId: String, username: String, emoji: String ->
|
||||
_events.tryEmit(ChatEvent.ReactionUpdated(messageId, chatId, userId, emoji))
|
||||
}, String::class.java, String::class.java, String::class.java, String::class.java, String::class.java)
|
||||
conn.on("reaction_added", { data: ReactionEvent ->
|
||||
_events.tryEmit(ChatEvent.ReactionUpdated(
|
||||
data.messageId ?: "",
|
||||
data.chatId ?: "",
|
||||
data.userId ?: "",
|
||||
data.emoji ?: ""
|
||||
))
|
||||
}, ReactionEvent::class.java)
|
||||
|
||||
conn.on("reaction_removed", { messageId: String, chatId: String, userId: String, emoji: String ->
|
||||
// Using ReactionUpdated with empty emoji to signal removal or just a specific removal event
|
||||
_events.tryEmit(ChatEvent.ReactionUpdated(messageId, chatId, userId, ""))
|
||||
}, String::class.java, String::class.java, String::class.java, String::class.java)
|
||||
conn.on("reaction_removed", { data: ReactionEvent ->
|
||||
_events.tryEmit(ChatEvent.ReactionUpdated(
|
||||
data.messageId ?: "",
|
||||
data.chatId ?: "",
|
||||
data.userId ?: "",
|
||||
"" // empty emoji signals removal
|
||||
))
|
||||
}, ReactionEvent::class.java)
|
||||
|
||||
// WebRTC Signaling Handlers
|
||||
conn.on("call_incoming", { chatId: String, from: String, offer: String, callType: String ->
|
||||
@@ -128,7 +159,26 @@ class ChatHubClient @Inject constructor() {
|
||||
|
||||
fun readMessages(request: ReadMessagesRequest) {
|
||||
if (hubConnection?.connectionState == HubConnectionState.CONNECTED) {
|
||||
hubConnection?.invoke("read_messages", request)
|
||||
hubConnection?.send("read_messages", request)
|
||||
Log.d("ChatHubClient", "Sent read_messages for chat: ${request.chatId}")
|
||||
}
|
||||
}
|
||||
|
||||
fun joinChat(chatId: String) {
|
||||
scope.launch {
|
||||
// Wait for connection to be established if it's currently connecting
|
||||
var attempts = 0
|
||||
while (hubConnection?.connectionState != HubConnectionState.CONNECTED && attempts < 10) {
|
||||
delay(500)
|
||||
attempts++
|
||||
}
|
||||
|
||||
if (hubConnection?.connectionState == HubConnectionState.CONNECTED) {
|
||||
hubConnection?.send("join_chat", chatId)
|
||||
Log.d("ChatHubClient", "Joined chat room: $chatId")
|
||||
} else {
|
||||
Log.e("ChatHubClient", "Failed to join chat room $chatId: Not connected")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user