forked from melod1n/fast-messenger
Merge remote-tracking branch 'origin/dev' into dev
This commit is contained in:
@@ -35,13 +35,12 @@ class LongPollUpdatesParser(
|
|||||||
|
|
||||||
private val coroutineScope = CoroutineScope(coroutineContext)
|
private val coroutineScope = CoroutineScope(coroutineContext)
|
||||||
|
|
||||||
private val listenersMap: MutableMap<ApiEvent, MutableCollection<VkEventCallback<*>>> =
|
private val listenersMap: MutableMap<ApiEvent, MutableList<VkEventCallback<*>>> = mutableMapOf()
|
||||||
mutableMapOf()
|
|
||||||
|
|
||||||
fun parseNextUpdate(event: List<Any>) {
|
fun parseNextUpdate(event: List<Any>) {
|
||||||
val eventId = event.first().asInt()
|
val eventId = event.first().asInt()
|
||||||
|
|
||||||
when (val eventType = ApiEvent.parseOrNull(eventId)) {
|
when (val eventType = ApiEvent.parseOrNull(eventId)) {
|
||||||
null -> Log.d("LongPollUpdatesParser", "parseNextUpdate: unknownEvent: $event")
|
null -> Log.d("LongPollUpdatesParser", "parseNextUpdate: unknownEvent: $event")
|
||||||
|
|
||||||
ApiEvent.MESSAGE_SET_FLAGS -> parseMessageSetFlags(eventType, event)
|
ApiEvent.MESSAGE_SET_FLAGS -> parseMessageSetFlags(eventType, event)
|
||||||
@@ -59,14 +58,10 @@ class LongPollUpdatesParser(
|
|||||||
ApiEvent.VIDEO_UPLOADING,
|
ApiEvent.VIDEO_UPLOADING,
|
||||||
ApiEvent.FILE_UPLOADING -> parseInteraction(eventType, event)
|
ApiEvent.FILE_UPLOADING -> parseInteraction(eventType, event)
|
||||||
|
|
||||||
ApiEvent.UNREAD_COUNT_UPDATE -> onNewEvent(eventType, event)
|
ApiEvent.UNREAD_COUNT_UPDATE -> parseUnreadCounterUpdate(eventType, event)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun onNewEvent(eventType: ApiEvent, event: List<Any>) {
|
|
||||||
Log.d("LongPollUpdatesParser", "newEvent: $eventType: $event")
|
|
||||||
}
|
|
||||||
|
|
||||||
private fun parseInteraction(eventType: ApiEvent, event: List<Any>) {
|
private fun parseInteraction(eventType: ApiEvent, event: List<Any>) {
|
||||||
Log.d("LongPollUpdatesParser", "$eventType: $event")
|
Log.d("LongPollUpdatesParser", "$eventType: $event")
|
||||||
|
|
||||||
@@ -105,23 +100,50 @@ class LongPollUpdatesParser(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private fun parseUnreadCounterUpdate(eventType: ApiEvent, event: List<Any>) {
|
||||||
|
Log.d("LongPollUpdatesParser", "$eventType $event")
|
||||||
|
|
||||||
|
val unreadCount = event[1].asInt()
|
||||||
|
val unreadUnmutedCount = event[2].asInt()
|
||||||
|
val showOnlyMuted = event[3].asInt() == 1
|
||||||
|
val businessNotifyUnreadCount = event[4].asInt()
|
||||||
|
val archiveUnreadCount = event[7].asInt()
|
||||||
|
val archiveUnreadUnmutedCount = event[8].asInt()
|
||||||
|
val archiveMentionsCount = event[9].asInt()
|
||||||
|
|
||||||
|
listenersMap[ApiEvent.UNREAD_COUNT_UPDATE]?.let { listeners ->
|
||||||
|
listeners.forEach { vkEventCallback ->
|
||||||
|
(vkEventCallback as VkEventCallback<LongPollEvent.UnreadCounter>)
|
||||||
|
.onEvent(
|
||||||
|
LongPollEvent.UnreadCounter(
|
||||||
|
unread = unreadCount,
|
||||||
|
unreadUnmuted = unreadUnmutedCount,
|
||||||
|
showOnlyMuted = showOnlyMuted,
|
||||||
|
business = businessNotifyUnreadCount,
|
||||||
|
archive = archiveUnreadCount,
|
||||||
|
archiveUnmuted = archiveUnreadUnmutedCount,
|
||||||
|
archiveMentions = archiveMentionsCount
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private fun parseConversationPinStateChanged(eventType: ApiEvent, event: List<Any>) {
|
private fun parseConversationPinStateChanged(eventType: ApiEvent, event: List<Any>) {
|
||||||
Log.d("LongPollUpdatesParser", "$eventType: $event")
|
Log.d("LongPollUpdatesParser", "$eventType: $event")
|
||||||
|
|
||||||
val peerId = event[1].asInt()
|
val peerId = event[1].asInt()
|
||||||
val majorId = event[2].asInt()
|
val majorId = event[2].asInt()
|
||||||
|
|
||||||
coroutineScope.launch {
|
listenersMap[ApiEvent.PIN_UNPIN_CONVERSATION]?.let { listeners ->
|
||||||
listenersMap[ApiEvent.PIN_UNPIN_CONVERSATION]?.let { listeners ->
|
listeners.forEach { vkEventCallback ->
|
||||||
listeners.forEach { vkEventCallback ->
|
(vkEventCallback as VkEventCallback<LongPollEvent.VkConversationPinStateChangedEvent>)
|
||||||
(vkEventCallback as VkEventCallback<LongPollEvent.VkConversationPinStateChangedEvent>)
|
.onEvent(
|
||||||
.onEvent(
|
LongPollEvent.VkConversationPinStateChangedEvent(
|
||||||
LongPollEvent.VkConversationPinStateChangedEvent(
|
peerId = peerId,
|
||||||
peerId = peerId,
|
majorId = majorId
|
||||||
majorId = majorId
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -184,18 +206,16 @@ class LongPollUpdatesParser(
|
|||||||
val messageId = event[2].asInt()
|
val messageId = event[2].asInt()
|
||||||
val unreadCount = event[3].asInt()
|
val unreadCount = event[3].asInt()
|
||||||
|
|
||||||
coroutineScope.launch {
|
listenersMap[ApiEvent.MESSAGE_READ_INCOMING]?.let { listeners ->
|
||||||
listenersMap[ApiEvent.MESSAGE_READ_INCOMING]?.let { listeners ->
|
listeners.map { vkEventCallback ->
|
||||||
listeners.map { vkEventCallback ->
|
(vkEventCallback as VkEventCallback<LongPollEvent.VkMessageReadIncomingEvent>)
|
||||||
(vkEventCallback as VkEventCallback<LongPollEvent.VkMessageReadIncomingEvent>)
|
.onEvent(
|
||||||
.onEvent(
|
LongPollEvent.VkMessageReadIncomingEvent(
|
||||||
LongPollEvent.VkMessageReadIncomingEvent(
|
peerId = peerId,
|
||||||
peerId = peerId,
|
messageId = messageId,
|
||||||
messageId = messageId,
|
unreadCount = unreadCount
|
||||||
unreadCount = unreadCount
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -206,18 +226,16 @@ class LongPollUpdatesParser(
|
|||||||
val messageId = event[2].asInt()
|
val messageId = event[2].asInt()
|
||||||
val unreadCount = event[3].asInt()
|
val unreadCount = event[3].asInt()
|
||||||
|
|
||||||
coroutineScope.launch {
|
listenersMap[ApiEvent.MESSAGE_READ_OUTGOING]?.let { listeners ->
|
||||||
listenersMap[ApiEvent.MESSAGE_READ_OUTGOING]?.let { listeners ->
|
listeners.map { vkEventCallback ->
|
||||||
listeners.map { vkEventCallback ->
|
(vkEventCallback as VkEventCallback<LongPollEvent.VkMessageReadOutgoingEvent>)
|
||||||
(vkEventCallback as VkEventCallback<LongPollEvent.VkMessageReadOutgoingEvent>)
|
.onEvent(
|
||||||
.onEvent(
|
LongPollEvent.VkMessageReadOutgoingEvent(
|
||||||
LongPollEvent.VkMessageReadOutgoingEvent(
|
peerId = peerId,
|
||||||
peerId = peerId,
|
messageId = messageId,
|
||||||
messageId = messageId,
|
unreadCount = unreadCount
|
||||||
unreadCount = unreadCount
|
|
||||||
)
|
|
||||||
)
|
)
|
||||||
}
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -266,7 +284,7 @@ class LongPollUpdatesParser(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T : Any> registerListener(
|
private fun <T : LongPollEvent> registerListener(
|
||||||
eventType: ApiEvent,
|
eventType: ApiEvent,
|
||||||
listener: VkEventCallback<T>
|
listener: VkEventCallback<T>
|
||||||
) {
|
) {
|
||||||
@@ -275,7 +293,7 @@ class LongPollUpdatesParser(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun <T : Any> registerListeners(
|
private fun <T : LongPollEvent> registerListeners(
|
||||||
eventTypes: List<ApiEvent>,
|
eventTypes: List<ApiEvent>,
|
||||||
listener: VkEventCallback<T>
|
listener: VkEventCallback<T>
|
||||||
) {
|
) {
|
||||||
@@ -344,12 +362,12 @@ class LongPollUpdatesParser(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
internal inline fun <R : Any> assembleEventCallback(
|
internal inline fun <R : LongPollEvent> assembleEventCallback(
|
||||||
crossinline block: (R) -> Unit,
|
crossinline block: (R) -> Unit,
|
||||||
): VkEventCallback<R> {
|
): VkEventCallback<R> {
|
||||||
return VkEventCallback { event -> block.invoke(event) }
|
return VkEventCallback { event -> block.invoke(event) }
|
||||||
}
|
}
|
||||||
|
|
||||||
fun interface VkEventCallback<in T : Any> {
|
fun interface VkEventCallback<in T : LongPollEvent> {
|
||||||
fun onEvent(event: T)
|
fun onEvent(event: T)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,4 +32,14 @@ sealed interface LongPollEvent {
|
|||||||
val totalCount: Int,
|
val totalCount: Int,
|
||||||
val timestamp: Int
|
val timestamp: Int
|
||||||
) : LongPollEvent
|
) : LongPollEvent
|
||||||
|
|
||||||
|
data class UnreadCounter(
|
||||||
|
val unread: Int,
|
||||||
|
val unreadUnmuted: Int,
|
||||||
|
val showOnlyMuted: Boolean,
|
||||||
|
val business: Int,
|
||||||
|
val archive: Int,
|
||||||
|
val archiveUnmuted: Int,
|
||||||
|
val archiveMentions: Int
|
||||||
|
): LongPollEvent
|
||||||
}
|
}
|
||||||
|
|||||||
+30
@@ -224,7 +224,37 @@ class MessagesHistoryViewModelImpl(
|
|||||||
}
|
}
|
||||||
|
|
||||||
private fun handleReadIncomingEvent(event: LongPollEvent.VkMessageReadIncomingEvent) {
|
private fun handleReadIncomingEvent(event: LongPollEvent.VkMessageReadIncomingEvent) {
|
||||||
|
if (event.peerId != screenState.value.conversationId) return
|
||||||
|
|
||||||
|
val messages = messages.value
|
||||||
|
val messageIndex =
|
||||||
|
messages.indexOfFirstOrNull { it.id == event.messageId }
|
||||||
|
|
||||||
|
if (messageIndex == null) { // диалога нет в списке
|
||||||
|
// pizdets
|
||||||
|
} else {
|
||||||
|
val newConversation = screenState.value.conversation.copy(
|
||||||
|
inRead = event.messageId
|
||||||
|
)
|
||||||
|
|
||||||
|
val uiMessages = messages.mapIndexed { index, item ->
|
||||||
|
item.asPresentation(
|
||||||
|
resourceProvider = resourceProvider,
|
||||||
|
showName = false,
|
||||||
|
prevMessage = messages.getOrNull(index + 1),
|
||||||
|
nextMessage = messages.getOrNull(index - 1),
|
||||||
|
showTimeInActionMessages = userSettings.showTimeInActionMessages.value,
|
||||||
|
conversation = newConversation
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
screenState.setValue { old ->
|
||||||
|
old.copy(
|
||||||
|
conversation = newConversation,
|
||||||
|
messages = uiMessages,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun handleReadOutgoingEvent(event: LongPollEvent.VkMessageReadOutgoingEvent) {
|
private fun handleReadOutgoingEvent(event: LongPollEvent.VkMessageReadOutgoingEvent) {
|
||||||
|
|||||||
Reference in New Issue
Block a user