remove unnecessary changes and refactor getMessagesByChatIds

This commit is contained in:
Chumva 2018-08-28 11:49:22 +03:00
parent 2a2aa98bbb
commit 5a71f738df
2 changed files with 15 additions and 37 deletions

View file

@ -144,9 +144,10 @@ class TelegramHelper private constructor() {
fun getChatLiveMessages() = chatLiveMessages fun getChatLiveMessages() = chatLiveMessages
fun getMessagesByChatIds(): Map<Long, List<TdApi.Message>> { fun getMessagesByChatIds(messageExpTime: Long = 0): Map<Long, List<TdApi.Message>> {
val res = mutableMapOf<Long, MutableList<TdApi.Message>>() val res = mutableMapOf<Long, MutableList<TdApi.Message>>()
for (message in usersLocationMessages.values) { for (message in usersLocationMessages.values) {
if (System.currentTimeMillis() / 1000 - getLastUpdatedTime(message) < messageExpTime) {
var messages = res[message.chatId] var messages = res[message.chatId]
if (messages != null) { if (messages != null) {
messages.add(message) messages.add(message)
@ -155,6 +156,7 @@ class TelegramHelper private constructor() {
res[message.chatId] = messages res[message.chatId] = messages
} }
} }
}
return res return res
} }

View file

@ -218,25 +218,17 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
private fun updateList() { private fun updateList() {
val res = mutableListOf<ListItem>() val res = mutableListOf<ListItem>()
for ((id, messages) in telegramHelper.getMessagesByChatIds()) { for ((id, messages) in telegramHelper.getMessagesByChatIds(settings.locHistoryTime)) {
telegramHelper.getChat(id)?.also { chat -> telegramHelper.getChat(id)?.also { chat ->
if ((telegramHelper.isPrivateChat(chat) || telegramHelper.isSecretChat(chat))
&& !telegramHelper.isBot(telegramHelper.getUserIdFromChatType(chat.type))
) {
if (!checkOutdatedMessage(messages.firstOrNull())) {
res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages)) res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages))
}
} else {
res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages))
}
val type = chat.type val type = chat.type
if (type is TdApi.ChatTypeBasicGroup || type is TdApi.ChatTypeSupergroup) { if (type is TdApi.ChatTypeBasicGroup || type is TdApi.ChatTypeSupergroup) {
checkOutdatedItems(res, convertToLocationItems(chat, messages)) res.addAll(convertToLocationItems(chat, messages))
} else if (type is TdApi.ChatTypePrivate) { } else if (type is TdApi.ChatTypePrivate) {
if (telegramHelper.isOsmAndBot(type.userId)) { if (telegramHelper.isOsmAndBot(type.userId)) {
checkOutdatedItems(res, convertToLocationItems(chat, messages)) res.addAll(convertToLocationItems(chat, messages))
} else if (messages.firstOrNull { it.viaBotUserId != 0 } != null) { } else if (messages.firstOrNull { it.viaBotUserId != 0 } != null) {
checkOutdatedItems(res, convertToLocationItems(chat, messages, true)) res.addAll(convertToLocationItems(chat, messages, true))
} }
} }
} }
@ -244,22 +236,6 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
adapter.items = res adapter.items = res
} }
private fun checkOutdatedItems(mainListItems: MutableList<ListItem>, groupListLocItems: List<LocationItem>) {
if (groupListLocItems.isNotEmpty()) {
mainListItems.addAll(groupListLocItems)
} else {
mainListItems.remove(mainListItems.last())
}
}
private fun checkOutdatedMessage(message: TdApi.Message?): Boolean {
return if (message != null) {
System.currentTimeMillis() / 1000 - telegramHelper.getLastUpdatedTime(message) > settings.locHistoryTime
} else {
false
}
}
private fun convertToLocationItems( private fun convertToLocationItems(
chat: TdApi.Chat, chat: TdApi.Chat,
messages: List<TdApi.Message>, messages: List<TdApi.Message>,
@ -267,7 +243,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
): List<LocationItem> { ): List<LocationItem> {
val res = mutableListOf<LocationItem>() val res = mutableListOf<LocationItem>()
messages.forEach { message -> messages.forEach { message ->
if ((!addOnlyViaBotMessages || message.viaBotUserId != 0) && !checkOutdatedMessage(message)) { if ((!addOnlyViaBotMessages || message.viaBotUserId != 0)) {
TelegramUiHelper.messageToLocationItem(telegramHelper, chat, message)?.also { TelegramUiHelper.messageToLocationItem(telegramHelper, chat, message)?.also {
res.add(it) res.add(it)
} }