Merge pull request #5609 from osmandapp/fix_telegram_helper

Fix telegram helper
This commit is contained in:
Alexey 2018-06-22 19:09:59 +03:00 committed by GitHub
commit f2069eef9c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 38 additions and 53 deletions

View file

@ -77,21 +77,10 @@ class TelegramHelper private constructor() {
fun getChatList(): TreeSet<OrderedChat> {
synchronized(chatList) {
return TreeSet<OrderedChat>(chatList)
return TreeSet(chatList.filter { !it.isChannel })
}
}
fun getChatIndex(chatId: Long): Int {
synchronized(chatList) {
for ((i, chat) in chatList.withIndex()) {
if (chat.chatId == chatId) {
return i
}
}
}
return -1
}
fun getChatTitles(): List<String> {
return chatTitles.keys().toList()
}
@ -363,6 +352,10 @@ class TelegramHelper private constructor() {
for (chatTitle in chatTitles) {
val chatId = this.chatTitles[chatTitle]
if (chatId != null) {
val chat = chats[chatId]
if (chat == null || (chat.type is TdApi.ChatTypeSupergroup && (chat.type as TdApi.ChatTypeSupergroup).isChannel)) {
return
}
client?.send(TdApi.SearchChatRecentLocationMessages(chatId, CHAT_LIVE_USERS_LIMIT)) { obj ->
when (obj.constructor) {
TdApi.Error.CONSTRUCTOR -> {
@ -498,14 +491,17 @@ class TelegramHelper private constructor() {
private fun setChatOrder(chat: TdApi.Chat, order: Long) {
synchronized(chatList) {
val type = chat.type
val isChannel = type is TdApi.ChatTypeSupergroup && type.isChannel
if (chat.order != 0L) {
chatList.remove(OrderedChat(chat.order, chat.id))
chatList.remove(OrderedChat(chat.order, chat.id, isChannel))
}
chat.order = order
if (chat.order != 0L) {
chatList.add(OrderedChat(chat.order, chat.id))
chatList.add(OrderedChat(chat.order, chat.id, isChannel))
}
}
}
@ -598,7 +594,7 @@ class TelegramHelper private constructor() {
listener?.onTelegramStatusChanged(prevAuthState, newAuthState)
}
class OrderedChat internal constructor(internal val order: Long, internal val chatId: Long) : Comparable<OrderedChat> {
class OrderedChat internal constructor(internal val order: Long, internal val chatId: Long, internal val isChannel: Boolean) : Comparable<OrderedChat> {
override fun compareTo(other: OrderedChat): Int {
if (this.order != other.order) {
@ -662,40 +658,38 @@ class TelegramHelper private constructor() {
val updateNewChat = obj as TdApi.UpdateNewChat
val chat = updateNewChat.chat
synchronized(chat) {
if (chat.type !is TdApi.ChatTypeSupergroup || !(chat.type as TdApi.ChatTypeSupergroup).isChannel) {
chats[chat.id] = chat
val localPhoto = chat.photo?.small?.local
val hasLocalPhoto = if (localPhoto != null) {
localPhoto.canBeDownloaded && localPhoto.isDownloadingCompleted && localPhoto.path.isNotEmpty()
} else {
false
}
if (!hasLocalPhoto) {
val remotePhoto = chat.photo?.small?.remote
if (remotePhoto != null && remotePhoto.id.isNotEmpty()) {
downloadChatFilesMap[remotePhoto.id] = chat
client!!.send(TdApi.GetRemoteFile(remotePhoto.id, null)) { obj ->
when (obj.constructor) {
TdApi.Error.CONSTRUCTOR -> {
val error = obj as TdApi.Error
val code = error.code
if (code != IGNORED_ERROR_CODE) {
listener?.onTelegramError(code, error.message)
}
chats[chat.id] = chat
val localPhoto = chat.photo?.small?.local
val hasLocalPhoto = if (localPhoto != null) {
localPhoto.canBeDownloaded && localPhoto.isDownloadingCompleted && localPhoto.path.isNotEmpty()
} else {
false
}
if (!hasLocalPhoto) {
val remotePhoto = chat.photo?.small?.remote
if (remotePhoto != null && remotePhoto.id.isNotEmpty()) {
downloadChatFilesMap[remotePhoto.id] = chat
client!!.send(TdApi.GetRemoteFile(remotePhoto.id, null)) { obj ->
when (obj.constructor) {
TdApi.Error.CONSTRUCTOR -> {
val error = obj as TdApi.Error
val code = error.code
if (code != IGNORED_ERROR_CODE) {
listener?.onTelegramError(code, error.message)
}
TdApi.File.CONSTRUCTOR -> {
val file = obj as TdApi.File
client!!.send(TdApi.DownloadFile(file.id, 10), defaultHandler)
}
else -> listener?.onTelegramError(-1, "Receive wrong response from TDLib: $obj")
}
TdApi.File.CONSTRUCTOR -> {
val file = obj as TdApi.File
client!!.send(TdApi.DownloadFile(file.id, 10), defaultHandler)
}
else -> listener?.onTelegramError(-1, "Receive wrong response from TDLib: $obj")
}
}
}
val order = chat.order
chat.order = 0
setChatOrder(chat, order)
}
val order = chat.order
chat.order = 0
setChatOrder(chat, order)
}
updateChatTitles()
listener?.onTelegramChatsChanged()

View file

@ -211,7 +211,7 @@ class MainActivity : AppCompatActivity(), TelegramListener {
override fun onTelegramChatChanged(chat: TdApi.Chat) {
runOnUi {
updateChat(chat)
updateChatsList()
listeners.forEach { it.get()?.onTelegramChatChanged(chat) }
}
}
@ -258,15 +258,6 @@ class MainActivity : AppCompatActivity(), TelegramListener {
chatViewAdapter.chats = chats
}
private fun updateChat(chat: TdApi.Chat) {
val chatIndex = telegramHelper.getChatIndex(chat.id)
if (chatIndex != -1) {
chatViewAdapter.notifyItemChanged(chatIndex)
} else {
updateChatsList()
}
}
fun loginTelegram() {
if (telegramHelper.getTelegramAuthorizationState() != TelegramAuthorizationState.CLOSED) {
telegramHelper.logout()