From fea5807de8525d0b39e15d82d8daafe435d03672 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 22 Jun 2018 18:29:30 +0300 Subject: [PATCH 1/3] Fix requesting of chats --- .../osmand/telegram/helpers/TelegramHelper.kt | 65 ++++++++++--------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 7945139a9c..a9de7b8eaf 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -77,7 +77,7 @@ class TelegramHelper private constructor() { fun getChatList(): TreeSet { synchronized(chatList) { - return TreeSet(chatList) + return TreeSet(chatList.filter { !it.isChannel }) } } @@ -498,14 +498,17 @@ class TelegramHelper private constructor() { private fun setChatOrder(chat: TdApi.Chat, order: Long) { synchronized(chatList) { + val type = chat.type + val isChannel = type is TdApi.ChatTypeSupergroup && type.isChannel + if (chat.order != 0L) { - chatList.remove(OrderedChat(chat.order, chat.id)) + chatList.remove(OrderedChat(chat.order, chat.id, isChannel)) } chat.order = order if (chat.order != 0L) { - chatList.add(OrderedChat(chat.order, chat.id)) + chatList.add(OrderedChat(chat.order, chat.id, isChannel)) } } } @@ -598,7 +601,7 @@ class TelegramHelper private constructor() { listener?.onTelegramStatusChanged(prevAuthState, newAuthState) } - class OrderedChat internal constructor(internal val order: Long, internal val chatId: Long) : Comparable { + class OrderedChat internal constructor(internal val order: Long, internal val chatId: Long, internal val isChannel: Boolean) : Comparable { override fun compareTo(other: OrderedChat): Int { if (this.order != other.order) { @@ -662,40 +665,38 @@ class TelegramHelper private constructor() { val updateNewChat = obj as TdApi.UpdateNewChat val chat = updateNewChat.chat synchronized(chat) { - if (chat.type !is TdApi.ChatTypeSupergroup || !(chat.type as TdApi.ChatTypeSupergroup).isChannel) { - chats[chat.id] = chat - val localPhoto = chat.photo?.small?.local - val hasLocalPhoto = if (localPhoto != null) { - localPhoto.canBeDownloaded && localPhoto.isDownloadingCompleted && localPhoto.path.isNotEmpty() - } else { - false - } - if (!hasLocalPhoto) { - val remotePhoto = chat.photo?.small?.remote - if (remotePhoto != null && remotePhoto.id.isNotEmpty()) { - downloadChatFilesMap[remotePhoto.id] = chat - client!!.send(TdApi.GetRemoteFile(remotePhoto.id, null)) { obj -> - when (obj.constructor) { - TdApi.Error.CONSTRUCTOR -> { - val error = obj as TdApi.Error - val code = error.code - if (code != IGNORED_ERROR_CODE) { - listener?.onTelegramError(code, error.message) - } + chats[chat.id] = chat + val localPhoto = chat.photo?.small?.local + val hasLocalPhoto = if (localPhoto != null) { + localPhoto.canBeDownloaded && localPhoto.isDownloadingCompleted && localPhoto.path.isNotEmpty() + } else { + false + } + if (!hasLocalPhoto) { + val remotePhoto = chat.photo?.small?.remote + if (remotePhoto != null && remotePhoto.id.isNotEmpty()) { + downloadChatFilesMap[remotePhoto.id] = chat + client!!.send(TdApi.GetRemoteFile(remotePhoto.id, null)) { obj -> + when (obj.constructor) { + TdApi.Error.CONSTRUCTOR -> { + val error = obj as TdApi.Error + val code = error.code + if (code != IGNORED_ERROR_CODE) { + listener?.onTelegramError(code, error.message) } - TdApi.File.CONSTRUCTOR -> { - val file = obj as TdApi.File - client!!.send(TdApi.DownloadFile(file.id, 10), defaultHandler) - } - else -> listener?.onTelegramError(-1, "Receive wrong response from TDLib: $obj") } + TdApi.File.CONSTRUCTOR -> { + val file = obj as TdApi.File + client!!.send(TdApi.DownloadFile(file.id, 10), defaultHandler) + } + else -> listener?.onTelegramError(-1, "Receive wrong response from TDLib: $obj") } } } - val order = chat.order - chat.order = 0 - setChatOrder(chat, order) } + val order = chat.order + chat.order = 0 + setChatOrder(chat, order) } updateChatTitles() listener?.onTelegramChatsChanged() From 25f39eefa7d535aaf84c1f86213826c74a9ff4fe Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 22 Jun 2018 19:00:40 +0300 Subject: [PATCH 2/3] Do not request location messages for channels --- .../src/net/osmand/telegram/helpers/TelegramHelper.kt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index a9de7b8eaf..d3f0585980 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -363,6 +363,10 @@ class TelegramHelper private constructor() { for (chatTitle in chatTitles) { val chatId = this.chatTitles[chatTitle] if (chatId != null) { + val chat = chats[chatId] + if (chat == null || (chat.type is TdApi.ChatTypeSupergroup && (chat.type as TdApi.ChatTypeSupergroup).isChannel)) { + return + } client?.send(TdApi.SearchChatRecentLocationMessages(chatId, CHAT_LIVE_USERS_LIMIT)) { obj -> when (obj.constructor) { TdApi.Error.CONSTRUCTOR -> { From 8056de8b3ec6a22b6c1191b4881a92bab1be5e11 Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Fri, 22 Jun 2018 19:01:58 +0300 Subject: [PATCH 3/3] Remove unnecessary method --- .../src/net/osmand/telegram/helpers/TelegramHelper.kt | 11 ----------- .../src/net/osmand/telegram/ui/MainActivity.kt | 11 +---------- 2 files changed, 1 insertion(+), 21 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index d3f0585980..747a967693 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -81,17 +81,6 @@ class TelegramHelper private constructor() { } } - fun getChatIndex(chatId: Long): Int { - synchronized(chatList) { - for ((i, chat) in chatList.withIndex()) { - if (chat.chatId == chatId) { - return i - } - } - } - return -1 - } - fun getChatTitles(): List { return chatTitles.keys().toList() } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt index 0a69c1bd43..22ade33cc0 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/MainActivity.kt @@ -211,7 +211,7 @@ class MainActivity : AppCompatActivity(), TelegramListener { override fun onTelegramChatChanged(chat: TdApi.Chat) { runOnUi { - updateChat(chat) + updateChatsList() listeners.forEach { it.get()?.onTelegramChatChanged(chat) } } } @@ -258,15 +258,6 @@ class MainActivity : AppCompatActivity(), TelegramListener { chatViewAdapter.chats = chats } - private fun updateChat(chat: TdApi.Chat) { - val chatIndex = telegramHelper.getChatIndex(chat.id) - if (chatIndex != -1) { - chatViewAdapter.notifyItemChanged(chatIndex) - } else { - updateChatsList() - } - } - fun loginTelegram() { if (telegramHelper.getTelegramAuthorizationState() != TelegramAuthorizationState.CLOSED) { telegramHelper.logout()