From e47fd457a78680c0b6c68eb8991182a71f1809d5 Mon Sep 17 00:00:00 2001 From: Vitaliy Date: Tue, 8 Dec 2020 21:29:23 +0200 Subject: [PATCH] Fix chats order --- .../osmand/telegram/helpers/TelegramHelper.kt | 46 +++++++++++-------- .../telegram/utils/OsmandLocationUtils.kt | 16 +++---- 2 files changed, 36 insertions(+), 26 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 0df3877863..4e8d657bbd 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -1086,7 +1086,7 @@ class TelegramHelper private constructor() { } } - private fun setChatPositions(chat: TdApi.Chat, positions: Array) { + private fun setChatPositions(chat: TdApi.Chat, positions: Array) { synchronized(chatList) { synchronized(chat) { val isChannel = isChannel(chat) @@ -1337,23 +1337,33 @@ class TelegramHelper private constructor() { //listener?.onTelegramChatsChanged() } } - TdApi.UpdateChatOrder.CONSTRUCTOR -> { - val updateChat = obj as TdApi.UpdateChatOrder - val chat = chats[updateChat.chatId] - if (chat != null) { - synchronized(chat) { - setChatOrder(chat, updateChat.order) - } - listener?.onTelegramChatsChanged() - } - } - TdApi.UpdateChatIsPinned.CONSTRUCTOR -> { - val updateChat = obj as TdApi.UpdateChatIsPinned - val chat = chats[updateChat.chatId] - if (chat != null) { - synchronized(chat) { - chat.isPinned = updateChat.isPinned - setChatOrder(chat, updateChat.order) + TdApi.UpdateChatPosition.CONSTRUCTOR -> { + val updateChat = obj as TdApi.UpdateChatPosition + if (updateChat.position.list.constructor == TdApi.ChatListMain.CONSTRUCTOR) { + val chat = chats[updateChat.chatId] + if (chat != null) { + synchronized(chat) { + var index = 0 + for (i in chat.positions.indices) { + if (chat.positions[i].list.constructor == TdApi.ChatListMain.CONSTRUCTOR) { + index = i + break + } + } + val length = chat.positions.size + (if (updateChat.position.order == 0L) 0 else 1) - if (index < chat.positions.size) 1 else 0 + val newPositions = arrayOfNulls(length) + + var pos = 0 + if (updateChat.position.order != 0L) { + newPositions[pos++] = updateChat.position + } + for (j in chat.positions.indices) { + if (j != index) { + newPositions[pos++] = chat.positions[j]; + } + } + setChatPositions(chat, newPositions) + } } //listener?.onTelegramChatsChanged() } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt index 2f801fe17f..4b12c7d6b7 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/utils/OsmandLocationUtils.kt @@ -99,15 +99,15 @@ object OsmandLocationUtils { fun getSenderMessageId(message: TdApi.Message): Int { val forwardInfo = message.forwardInfo - return if (forwardInfo != null) { - val origin: TdApi.MessageForwardOrigin? = forwardInfo.origin - if (origin != null && origin is TdApi.MessageForwardOriginUser) { - origin.senderUserId - } else { - message.senderUserId - } + return if (forwardInfo != null && forwardInfo.origin is TdApi.MessageForwardOriginUser) { + (forwardInfo.origin as TdApi.MessageForwardOriginUser).senderUserId } else { - message.senderUserId + val sender = message.sender + if (sender is TdApi.MessageSenderUser) { + sender.userId + } else { + 0 + } } }