From 366035714869c3c23fe764a3f0ceb8f3c0fa68ce Mon Sep 17 00:00:00 2001 From: Alex Sytnyk Date: Thu, 2 Aug 2018 17:30:59 +0300 Subject: [PATCH] Handle deletion of location messages --- .../src/net/osmand/telegram/TelegramService.kt | 18 ++++++++++++++++++ .../telegram/helpers/ShowLocationHelper.kt | 7 +++++-- .../osmand/telegram/helpers/TelegramHelper.kt | 9 ++++++++- .../osmand/telegram/ui/LiveNowTabFragment.kt | 4 ++++ 4 files changed, 35 insertions(+), 3 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt index 96cc14cb69..f54f74a1aa 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramService.kt @@ -253,6 +253,14 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis } } + override fun onDeleteChatLocationMessages(chatId: Long, messages: List) { + // TODO: handle notification? + val app = app() + if (app.settings.isShowingChatOnMap(chatId)) { + DeleteMessagesTask(app).executeOnExecutor(executor, messages) + } + } + override fun updateLocationMessages() { UpdateMessagesTask(app()).executeOnExecutor(executor) } @@ -267,6 +275,16 @@ class TelegramService : Service(), LocationListener, TelegramIncomingMessagesLis } } + private class DeleteMessagesTask(private val app: TelegramApplication) : AsyncTask, Void, Void?>() { + + override fun doInBackground(vararg messages: List): Void? { + for (list in messages) { + app.showLocationHelper.hideMessages(list) + } + return null + } + } + private class UpdateMessagesTask(private val app: TelegramApplication) : AsyncTask() { override fun doInBackground(vararg params: Void?): Void? { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt index 2676fc1aed..8c82b8665f 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/ShowLocationHelper.kt @@ -107,12 +107,15 @@ class ShowLocationHelper(private val app: TelegramApplication) { } fun hideChatMessages(chatId: Long) { + hideMessages(telegramHelper.getChatMessages(chatId)) + } + + fun hideMessages(messages: List) { execOsmandApi { - val messages = telegramHelper.getChatMessages(chatId) for (message in messages) { val user = telegramHelper.getUser(message.senderUserId) if (user != null) { - removeMapPoint(chatId, message) + removeMapPoint(message.chatId, message) } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index cf72e507b5..3cd053c849 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -174,6 +174,7 @@ class TelegramHelper private constructor() { interface TelegramIncomingMessagesListener { fun onReceiveChatLocationMessages(chatId: Long, vararg messages: TdApi.Message) + fun onDeleteChatLocationMessages(chatId: Long, messages: List) fun updateLocationMessages() } @@ -913,10 +914,16 @@ class TelegramHelper private constructor() { val updateDeleteMessages = obj as TdApi.UpdateDeleteMessages if (updateDeleteMessages.isPermanent) { val chatId = updateDeleteMessages.chatId + val deletedMessages = mutableListOf() for (messageId in updateDeleteMessages.messageIds) { if (chatLiveMessages[chatId] == messageId) { chatLiveMessages.remove(chatId) - break + } + usersLocationMessages.remove(messageId)?.also { deletedMessages.add(it) } + } + if (deletedMessages.isNotEmpty()) { + incomingMessagesListeners.forEach { + it.onDeleteChatLocationMessages(chatId, deletedMessages) } } } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt index d93ac18973..34fbc8355c 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/LiveNowTabFragment.kt @@ -124,6 +124,10 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage app.runInUIThread { updateList() } } + override fun onDeleteChatLocationMessages(chatId: Long, messages: List) { + app.runInUIThread { updateList() } + } + override fun updateLocationMessages() {} override fun updateLocation(location: Location?) {