Refactor sendNewLiveLocationMessage for text messages

This commit is contained in:
Chumva 2018-12-10 15:49:30 +02:00
parent 6d63adc2ba
commit 0de3d8af76
3 changed files with 39 additions and 25 deletions

View file

@ -245,7 +245,10 @@ class TelegramSettings(private val app: TelegramApplication) {
if (shareChatInfo != null) { if (shareChatInfo != null) {
when (content) { when (content) {
is TdApi.MessageLocation -> shareChatInfo.currentMapMessageId = message.id 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 shareChatInfo.lastSuccessfulSendTimeMs = Math.max(message.editDate, message.date) * 1000L
} }

View file

@ -760,11 +760,18 @@ class TelegramHelper private constructor() {
} }
} }
private fun recreateLiveLocationMessage(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageLocation) { private fun recreateLiveLocationMessage(
if (shareInfo.currentMapMessageId != -1L && shareInfo.chatId != -1L) { shareInfo: TelegramSettings.ShareChatInfo,
log.info("recreateLiveLocationMessage - ${shareInfo.currentMapMessageId}") content: TdApi.InputMessageContent
) {
if (shareInfo.chatId != -1L) {
val array = LongArray(1) val array = LongArray(1)
if (content is TdApi.InputMessageLocation) {
array[0] = shareInfo.currentMapMessageId 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 -> client?.send(TdApi.DeleteMessages(shareInfo.chatId, array, true)) { obj ->
when (obj.constructor) { when (obj.constructor) {
TdApi.Ok.CONSTRUCTOR -> sendNewLiveLocationMessage(shareInfo, content) TdApi.Ok.CONSTRUCTOR -> sendNewLiveLocationMessage(shareInfo, content)
@ -780,10 +787,11 @@ class TelegramHelper private constructor() {
} }
} }
} }
}
needRefreshActiveLiveLocationMessages = true needRefreshActiveLiveLocationMessages = true
} }
private fun sendNewLiveLocationMessage(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageLocation) { private fun sendNewLiveLocationMessage(shareInfo: TelegramSettings.ShareChatInfo, content: TdApi.InputMessageContent) {
needRefreshActiveLiveLocationMessages = true needRefreshActiveLiveLocationMessages = true
log.info("sendNewLiveLocationMessage") log.info("sendNewLiveLocationMessage")
client?.send( client?.send(
@ -862,17 +870,20 @@ class TelegramHelper private constructor() {
if (shareInfo.getChatLiveMessageExpireTime() <= 0) { if (shareInfo.getChatLiveMessageExpireTime() <= 0) {
return@forEach return@forEach
} }
val content = getTextMessageContent(shareInfo.updateTextMessageId, location)
val msgId = shareInfo.currentTextMessageId val msgId = shareInfo.currentTextMessageId
if (msgId != -1L) { if (msgId == -1L) {
client?.send(TdApi.EditMessageText(chatId, msgId, null, content)) { obj -> shareInfo.updateTextMessageId = 1
if (obj is TdApi.Message) {
shareInfo.updateTextMessageId++
outgoingMessagesListeners.forEach {
it.onUpdateMessages(listOf(obj))
} }
val content = getTextMessageContent(shareInfo.updateTextMessageId, location)
if (msgId != -1L) {
if (shareInfo.shouldDeletePreviousMessage) {
recreateLiveLocationMessage(shareInfo, content)
shareInfo.shouldDeletePreviousMessage = false
shareInfo.currentTextMessageId = -1
shareInfo.updateTextMessageId = 1
} else { } else {
log.info("EditMessageTextFail - $obj") client?.send(TdApi.EditMessageText(chatId, msgId, null, content)) { obj ->
handleLiveLocationMessageUpdate(obj, shareInfo)
} }
} }
} else { } else {

View file

@ -228,7 +228,7 @@ class SettingsDialogFragment : BaseDialogFragment() {
isModal = true isModal = true
anchorView = valueView anchorView = valueView
setContentWidth(AndroidUtils.getPopupMenuWidth(ctx, menuList)) setContentWidth(AndroidUtils.getPopupMenuWidth(ctx, menuList))
height = if (menuList.size > 6) { height = if (menuList.size < 6) {
ListPopupWindow.WRAP_CONTENT ListPopupWindow.WRAP_CONTENT
} else { } else {
AndroidUtils.getPopupMenuHeight(ctx) AndroidUtils.getPopupMenuHeight(ctx)