Merge pull request #5609 from osmandapp/fix_telegram_helper
Fix telegram helper
This commit is contained in:
commit
f2069eef9c
2 changed files with 38 additions and 53 deletions
|
@ -77,21 +77,10 @@ class TelegramHelper private constructor() {
|
||||||
|
|
||||||
fun getChatList(): TreeSet<OrderedChat> {
|
fun getChatList(): TreeSet<OrderedChat> {
|
||||||
synchronized(chatList) {
|
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> {
|
fun getChatTitles(): List<String> {
|
||||||
return chatTitles.keys().toList()
|
return chatTitles.keys().toList()
|
||||||
}
|
}
|
||||||
|
@ -363,6 +352,10 @@ class TelegramHelper private constructor() {
|
||||||
for (chatTitle in chatTitles) {
|
for (chatTitle in chatTitles) {
|
||||||
val chatId = this.chatTitles[chatTitle]
|
val chatId = this.chatTitles[chatTitle]
|
||||||
if (chatId != null) {
|
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 ->
|
client?.send(TdApi.SearchChatRecentLocationMessages(chatId, CHAT_LIVE_USERS_LIMIT)) { obj ->
|
||||||
when (obj.constructor) {
|
when (obj.constructor) {
|
||||||
TdApi.Error.CONSTRUCTOR -> {
|
TdApi.Error.CONSTRUCTOR -> {
|
||||||
|
@ -498,14 +491,17 @@ class TelegramHelper private constructor() {
|
||||||
|
|
||||||
private fun setChatOrder(chat: TdApi.Chat, order: Long) {
|
private fun setChatOrder(chat: TdApi.Chat, order: Long) {
|
||||||
synchronized(chatList) {
|
synchronized(chatList) {
|
||||||
|
val type = chat.type
|
||||||
|
val isChannel = type is TdApi.ChatTypeSupergroup && type.isChannel
|
||||||
|
|
||||||
if (chat.order != 0L) {
|
if (chat.order != 0L) {
|
||||||
chatList.remove(OrderedChat(chat.order, chat.id))
|
chatList.remove(OrderedChat(chat.order, chat.id, isChannel))
|
||||||
}
|
}
|
||||||
|
|
||||||
chat.order = order
|
chat.order = order
|
||||||
|
|
||||||
if (chat.order != 0L) {
|
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)
|
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 {
|
override fun compareTo(other: OrderedChat): Int {
|
||||||
if (this.order != other.order) {
|
if (this.order != other.order) {
|
||||||
|
@ -662,40 +658,38 @@ class TelegramHelper private constructor() {
|
||||||
val updateNewChat = obj as TdApi.UpdateNewChat
|
val updateNewChat = obj as TdApi.UpdateNewChat
|
||||||
val chat = updateNewChat.chat
|
val chat = updateNewChat.chat
|
||||||
synchronized(chat) {
|
synchronized(chat) {
|
||||||
if (chat.type !is TdApi.ChatTypeSupergroup || !(chat.type as TdApi.ChatTypeSupergroup).isChannel) {
|
chats[chat.id] = chat
|
||||||
chats[chat.id] = chat
|
val localPhoto = chat.photo?.small?.local
|
||||||
val localPhoto = chat.photo?.small?.local
|
val hasLocalPhoto = if (localPhoto != null) {
|
||||||
val hasLocalPhoto = if (localPhoto != null) {
|
localPhoto.canBeDownloaded && localPhoto.isDownloadingCompleted && localPhoto.path.isNotEmpty()
|
||||||
localPhoto.canBeDownloaded && localPhoto.isDownloadingCompleted && localPhoto.path.isNotEmpty()
|
} else {
|
||||||
} else {
|
false
|
||||||
false
|
}
|
||||||
}
|
if (!hasLocalPhoto) {
|
||||||
if (!hasLocalPhoto) {
|
val remotePhoto = chat.photo?.small?.remote
|
||||||
val remotePhoto = chat.photo?.small?.remote
|
if (remotePhoto != null && remotePhoto.id.isNotEmpty()) {
|
||||||
if (remotePhoto != null && remotePhoto.id.isNotEmpty()) {
|
downloadChatFilesMap[remotePhoto.id] = chat
|
||||||
downloadChatFilesMap[remotePhoto.id] = chat
|
client!!.send(TdApi.GetRemoteFile(remotePhoto.id, null)) { obj ->
|
||||||
client!!.send(TdApi.GetRemoteFile(remotePhoto.id, null)) { obj ->
|
when (obj.constructor) {
|
||||||
when (obj.constructor) {
|
TdApi.Error.CONSTRUCTOR -> {
|
||||||
TdApi.Error.CONSTRUCTOR -> {
|
val error = obj as TdApi.Error
|
||||||
val error = obj as TdApi.Error
|
val code = error.code
|
||||||
val code = error.code
|
if (code != IGNORED_ERROR_CODE) {
|
||||||
if (code != IGNORED_ERROR_CODE) {
|
listener?.onTelegramError(code, error.message)
|
||||||
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()
|
updateChatTitles()
|
||||||
listener?.onTelegramChatsChanged()
|
listener?.onTelegramChatsChanged()
|
||||||
|
|
|
@ -211,7 +211,7 @@ class MainActivity : AppCompatActivity(), TelegramListener {
|
||||||
|
|
||||||
override fun onTelegramChatChanged(chat: TdApi.Chat) {
|
override fun onTelegramChatChanged(chat: TdApi.Chat) {
|
||||||
runOnUi {
|
runOnUi {
|
||||||
updateChat(chat)
|
updateChatsList()
|
||||||
listeners.forEach { it.get()?.onTelegramChatChanged(chat) }
|
listeners.forEach { it.get()?.onTelegramChatChanged(chat) }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -258,15 +258,6 @@ class MainActivity : AppCompatActivity(), TelegramListener {
|
||||||
chatViewAdapter.chats = chats
|
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() {
|
fun loginTelegram() {
|
||||||
if (telegramHelper.getTelegramAuthorizationState() != TelegramAuthorizationState.CLOSED) {
|
if (telegramHelper.getTelegramAuthorizationState() != TelegramAuthorizationState.CLOSED) {
|
||||||
telegramHelper.logout()
|
telegramHelper.logout()
|
||||||
|
|
Loading…
Reference in a new issue