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,15 +144,17 @@ class TelegramHelper private constructor() {
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>>()
for (message in usersLocationMessages.values) {
var messages = res[message.chatId]
if (messages != null) {
messages.add(message)
} else {
messages = mutableListOf(message)
res[message.chatId] = messages
if (System.currentTimeMillis() / 1000 - getLastUpdatedTime(message) < messageExpTime) {
var messages = res[message.chatId]
if (messages != null) {
messages.add(message)
} else {
messages = mutableListOf(message)
res[message.chatId] = messages
}
}
}
return res

View file

@ -218,47 +218,23 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
private fun updateList() {
val res = mutableListOf<ListItem>()
for ((id, messages) in telegramHelper.getMessagesByChatIds()) {
for ((id, messages) in telegramHelper.getMessagesByChatIds(settings.locHistoryTime)) {
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))
}
} else {
res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages))
}
res.add(TelegramUiHelper.chatToChatItem(telegramHelper, chat, messages))
val type = chat.type
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) {
if (telegramHelper.isOsmAndBot(type.userId)) {
checkOutdatedItems(res, convertToLocationItems(chat, messages))
res.addAll(convertToLocationItems(chat, messages))
} else if (messages.firstOrNull { it.viaBotUserId != 0 } != null) {
checkOutdatedItems(res, convertToLocationItems(chat, messages, true))
res.addAll(convertToLocationItems(chat, messages, true))
}
}
}
}
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(
chat: TdApi.Chat,
@ -267,7 +243,7 @@ class LiveNowTabFragment : Fragment(), TelegramListener, TelegramIncomingMessage
): List<LocationItem> {
val res = mutableListOf<LocationItem>()
messages.forEach { message ->
if ((!addOnlyViaBotMessages || message.viaBotUserId != 0) && !checkOutdatedMessage(message)) {
if ((!addOnlyViaBotMessages || message.viaBotUserId != 0)) {
TelegramUiHelper.messageToLocationItem(telegramHelper, chat, message)?.also {
res.add(it)
}