From 0de3d8af7633450e28045719422beaa1e80e868e Mon Sep 17 00:00:00 2001 From: Chumva Date: Mon, 10 Dec 2018 15:49:30 +0200 Subject: [PATCH] Refactor sendNewLiveLocationMessage for text messages --- .../net/osmand/telegram/TelegramSettings.kt | 5 +- .../osmand/telegram/helpers/TelegramHelper.kt | 57 +++++++++++-------- .../telegram/ui/SettingsDialogFragment.kt | 2 +- 3 files changed, 39 insertions(+), 25 deletions(-) diff --git a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt index ab5a3d0ff3..5525ec3b6b 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/TelegramSettings.kt @@ -245,7 +245,10 @@ class TelegramSettings(private val app: TelegramApplication) { if (shareChatInfo != null) { when (content) { is TdApi.MessageLocation -> shareChatInfo.currentMapMessageId = message.id - is TdApi.MessageText -> shareChatInfo.currentTextMessageId = message.id + is TdApi.MessageText -> { + shareChatInfo.currentTextMessageId = message.id + shareChatInfo.updateTextMessageId++ + } } shareChatInfo.lastSuccessfulSendTimeMs = Math.max(message.editDate, message.date) * 1000L } diff --git a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt index 177c98b9b4..be361f2a9d 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/helpers/TelegramHelper.kt @@ -760,20 +760,28 @@ class TelegramHelper private constructor() { } } - private fun recreateLiveLocationMessage(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageLocation) { - if (shareInfo.currentMapMessageId != -1L && shareInfo.chatId != -1L) { - log.info("recreateLiveLocationMessage - ${shareInfo.currentMapMessageId}") + private fun recreateLiveLocationMessage( + shareInfo: TelegramSettings.ShareChatInfo, + content: TdApi.InputMessageContent + ) { + if (shareInfo.chatId != -1L) { val array = LongArray(1) - array[0] = shareInfo.currentMapMessageId - client?.send(TdApi.DeleteMessages(shareInfo.chatId, array, true)) { obj -> - when (obj.constructor) { - TdApi.Ok.CONSTRUCTOR -> sendNewLiveLocationMessage(shareInfo, content) - TdApi.Error.CONSTRUCTOR -> { - val error = obj as TdApi.Error - if (error.code != IGNORED_ERROR_CODE) { - needRefreshActiveLiveLocationMessages = true - outgoingMessagesListeners.forEach { - it.onSendLiveLocationError(error.code, error.message) + if (content is TdApi.InputMessageLocation) { + array[0] = shareInfo.currentMapMessageId + } else if (content is TdApi.InputMessageLocation) { + array[0] = shareInfo.currentTextMessageId + } + if (array[0] != 0L) { + client?.send(TdApi.DeleteMessages(shareInfo.chatId, array, true)) { obj -> + when (obj.constructor) { + TdApi.Ok.CONSTRUCTOR -> sendNewLiveLocationMessage(shareInfo, content) + TdApi.Error.CONSTRUCTOR -> { + val error = obj as TdApi.Error + if (error.code != IGNORED_ERROR_CODE) { + needRefreshActiveLiveLocationMessages = true + outgoingMessagesListeners.forEach { + it.onSendLiveLocationError(error.code, error.message) + } } } } @@ -783,7 +791,7 @@ class TelegramHelper private constructor() { needRefreshActiveLiveLocationMessages = true } - private fun sendNewLiveLocationMessage(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageLocation) { + private fun sendNewLiveLocationMessage(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageContent) { needRefreshActiveLiveLocationMessages = true log.info("sendNewLiveLocationMessage") client?.send( @@ -862,17 +870,20 @@ class TelegramHelper private constructor() { if (shareInfo.getChatLiveMessageExpireTime() <= 0) { return@forEach } - val content = getTextMessageContent(shareInfo.updateTextMessageId, location) val msgId = shareInfo.currentTextMessageId + if (msgId == -1L) { + shareInfo.updateTextMessageId = 1 + } + val content = getTextMessageContent(shareInfo.updateTextMessageId, location) if (msgId != -1L) { - client?.send(TdApi.EditMessageText(chatId, msgId, null, content)) { obj -> - if (obj is TdApi.Message) { - shareInfo.updateTextMessageId++ - outgoingMessagesListeners.forEach { - it.onUpdateMessages(listOf(obj)) - } - } else { - log.info("EditMessageTextFail - $obj") + if (shareInfo.shouldDeletePreviousMessage) { + recreateLiveLocationMessage(shareInfo, content) + shareInfo.shouldDeletePreviousMessage = false + shareInfo.currentTextMessageId = -1 + shareInfo.updateTextMessageId = 1 + } else { + client?.send(TdApi.EditMessageText(chatId, msgId, null, content)) { obj -> + handleLiveLocationMessageUpdate(obj, shareInfo) } } } else { diff --git a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt index 178512ec1e..44db164777 100644 --- a/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt +++ b/OsmAnd-telegram/src/net/osmand/telegram/ui/SettingsDialogFragment.kt @@ -228,7 +228,7 @@ class SettingsDialogFragment : BaseDialogFragment() { isModal = true anchorView = valueView setContentWidth(AndroidUtils.getPopupMenuWidth(ctx, menuList)) - height = if (menuList.size > 6) { + height = if (menuList.size < 6) { ListPopupWindow.WRAP_CONTENT } else { AndroidUtils.getPopupMenuHeight(ctx)