Fix live messages

This commit is contained in:
Alex Sytnyk 2018-08-16 15:15:50 +03:00
parent 7d3ea473e8
commit 04d6e82157
3 changed files with 40 additions and 27 deletions

View file

@ -491,7 +491,7 @@ class TelegramHelper private constructor() {
} else if (oldContent is TdApi.MessageLocation && (fromBot || viaBot)) { } else if (oldContent is TdApi.MessageLocation && (fromBot || viaBot)) {
message.content = parseOsmAndBotLocation(message) message.content = parseOsmAndBotLocation(message)
} }
removeOldMessages(message.senderUserId, message.chatId) removeOldMessages(message, fromBot, viaBot)
usersLocationMessages[message.id] = message usersLocationMessages[message.id] = message
incomingMessagesListeners.forEach { incomingMessagesListeners.forEach {
it.onReceiveChatLocationMessages(message.chatId, message) it.onReceiveChatLocationMessages(message.chatId, message)
@ -499,13 +499,25 @@ class TelegramHelper private constructor() {
} }
} }
private fun removeOldMessages(userId: Int, chatId: Long) { private fun removeOldMessages(newMessage: TdApi.Message, fromBot: Boolean, viaBot: Boolean) {
val user = users[userId] val iterator = usersLocationMessages.entries.iterator()
if (user != null && user.username != OSMAND_BOT_USERNAME) { while (iterator.hasNext()) {
usersLocationMessages.values.filter { it.senderUserId == userId && it.chatId == chatId } val message = iterator.next().value
.forEach { if (newMessage.chatId == message.chatId) {
usersLocationMessages.remove(it.id) val sameSender = newMessage.senderUserId == message.senderUserId
val viaSameBot = newMessage.viaBotUserId == message.viaBotUserId
if ((fromBot && sameSender) || (viaBot && viaSameBot)) {
val newCont = newMessage.content
val cont = message.content
if (newCont is MessageOsmAndBotLocation && cont is MessageOsmAndBotLocation) {
if (newCont.name == cont.name) {
iterator.remove()
}
}
} else if (sameSender) {
iterator.remove()
} }
}
} }
} }

View file

@ -52,10 +52,6 @@ object TelegramUiHelper {
placeholderId = R.drawable.img_user_picture placeholderId = R.drawable.img_user_picture
} }
val type = chat.type val type = chat.type
val message = messages.firstOrNull()
if (message != null) {
res.lastUpdated = helper.getLastUpdatedTime(message)
}
if (type is TdApi.ChatTypePrivate || type is TdApi.ChatTypeSecret) { if (type is TdApi.ChatTypePrivate || type is TdApi.ChatTypeSecret) {
val userId = getUserIdFromChatType(type) val userId = getUserIdFromChatType(type)
val chatWithBot = helper.isBot(userId) val chatWithBot = helper.isBot(userId)
@ -63,9 +59,13 @@ object TelegramUiHelper {
res.chatWithBot = chatWithBot res.chatWithBot = chatWithBot
if (!chatWithBot) { if (!chatWithBot) {
res.userId = userId res.userId = userId
val content = message?.content val message = messages.firstOrNull { it.viaBotUserId == 0 }
if (content is TdApi.MessageLocation) { if (message != null) {
res.latLon = LatLon(content.location.latitude, content.location.longitude) res.lastUpdated = helper.getLastUpdatedTime(message)
val content = message.content
if (content is TdApi.MessageLocation) {
res.latLon = LatLon(content.location.latitude, content.location.longitude)
}
} }
} }
} else if (type is TdApi.ChatTypeBasicGroup) { } else if (type is TdApi.ChatTypeBasicGroup) {

View file

@ -221,31 +221,32 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
for ((id, messages) in telegramHelper.getMessagesByChatIds()) { for ((id, messages) in telegramHelper.getMessagesByChatIds()) {
telegramHelper.getChat(id)?.also { chat -> telegramHelper.getChat(id)?.also { chat ->
res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages))
if (needLocationItems(chat.type)) { val type = chat.type
if (type is TdApi.ChatTypeBasicGroup || type is TdApi.ChatTypeSupergroup) {
res.addAll(convertToLocationItems(chat, messages)) res.addAll(convertToLocationItems(chat, messages))
} else if (type is TdApi.ChatTypePrivate) {
if (telegramHelper.isOsmAndBot(type.userId)) {
res.addAll(convertToLocationItems(chat, messages))
} else if (messages.firstOrNull { it.viaBotUserId != 0 } != null) {
res.addAll(convertToLocationItems(chat, messages, true))
}
} }
} }
} }
adapter.items = res adapter.items = res
} }
private fun needLocationItems(type: TdApi.ChatType): Boolean {
return when (type) {
is TdApi.ChatTypeBasicGroup -> true
is TdApi.ChatTypeSupergroup -> true
is TdApi.ChatTypePrivate -> telegramHelper.isOsmAndBot(type.userId)
else -> false
}
}
private fun convertToLocationItems( private fun convertToLocationItems(
chat: TdApi.Chat, chat: TdApi.Chat,
messages: List<TdApi.Message> messages: List<TdApi.Message>,
addOnlyViaBotMessages: Boolean = false
): List<LocationItem> { ): List<LocationItem> {
val res = mutableListOf<LocationItem>() val res = mutableListOf<LocationItem>()
messages.forEach { message -> messages.forEach { message ->
TelegramUiHelper.messageToLocationItem(telegramHelper, chat, message)?.also { if (!addOnlyViaBotMessages || message.viaBotUserId != 0) {
res.add(it) TelegramUiHelper.messageToLocationItem(telegramHelper, chat, message)?.also {
res.add(it)
}
} }
} }
return res return res