11package org.monogram.data.chats
22
3- import org.monogram.data.core.coRunCatching
43import kotlinx.coroutines.launch
54import org.drinkless.tdlib.TdApi
65import org.monogram.core.DispatcherProvider
76import org.monogram.core.ScopeProvider
7+ import org.monogram.data.core.coRunCatching
88import org.monogram.data.gateway.TelegramGateway
99import org.monogram.data.infra.FileDownloadQueue
10+ import org.monogram.data.infra.FileUpdateHandler
1011import java.util.*
1112import java.util.concurrent.ConcurrentHashMap
1213
@@ -15,6 +16,7 @@ class ChatFileManager(
1516 private val gateway : TelegramGateway ,
1617 private val dispatchers : DispatcherProvider ,
1718 private val fileQueue : FileDownloadQueue ,
19+ private val fileUpdateHandler : FileUpdateHandler ,
1820 scopeProvider : ScopeProvider ,
1921 private val onUpdate : () -> Unit
2022) {
@@ -23,13 +25,11 @@ class ChatFileManager(
2325 private val downloadingFiles: MutableSet <Int > = Collections .newSetFromMap(ConcurrentHashMap ())
2426 private val loadingEmojis: MutableSet <Long > = Collections .newSetFromMap(ConcurrentHashMap ())
2527 private val filePaths = ConcurrentHashMap <Int , String >()
26- private val emojiPathsCache = ConcurrentHashMap <Long , String >()
27- private val fileIdToEmojiId = ConcurrentHashMap <Int , Long >()
2828 private val chatPhotoIds = ConcurrentHashMap <Int , Long >()
2929 private val trackedFileIds = Collections .newSetFromMap(ConcurrentHashMap <Int , Boolean >())
3030
3131 fun getFilePath (fileId : Int ): String? = filePaths[fileId]
32- fun getEmojiPath (emojiId : Long ): String? = emojiPathsCache [emojiId]
32+ fun getEmojiPath (emojiId : Long ): String? = fileUpdateHandler.customEmojiPaths [emojiId]
3333 fun getChatIdByPhotoId (fileId : Int ): Long? = chatPhotoIds[fileId]
3434
3535 fun registerChatPhoto (fileId : Int , chatId : Long ) {
@@ -51,8 +51,8 @@ class ChatFileManager(
5151 private fun handleFileUpdated (fileId : Int , path : String ): Boolean {
5252 if (path.isEmpty()) return false
5353 var updated = false
54- fileIdToEmojiId [fileId]?.let { emojiId ->
55- emojiPathsCache [emojiId] = path
54+ fileUpdateHandler.fileIdToCustomEmojiId [fileId]?.let { emojiId ->
55+ fileUpdateHandler.customEmojiPaths [emojiId] = path
5656 updated = true
5757 }
5858 if (chatPhotoIds.containsKey(fileId)) updated = true
@@ -74,17 +74,17 @@ class ChatFileManager(
7474 }
7575
7676 fun loadEmoji (emojiId : Long ) {
77- if (emojiId == 0L || emojiPathsCache .containsKey(emojiId)) return
77+ if (emojiId == 0L || fileUpdateHandler.customEmojiPaths .containsKey(emojiId)) return
7878 if (loadingEmojis.add(emojiId)) {
7979 scope.launch(dispatchers.io) {
8080 coRunCatching {
8181 val result = gateway.execute(TdApi .GetCustomEmojiStickers (longArrayOf(emojiId)))
8282 val sticker = result.stickers.firstOrNull() ? : return @launch
8383 val file = sticker.sticker
8484 val path = file.local.path.ifEmpty { filePaths[file.id] ? : " " }
85- fileIdToEmojiId [file.id] = emojiId
85+ fileUpdateHandler.fileIdToCustomEmojiId [file.id] = emojiId
8686 if (path.isNotEmpty()) {
87- emojiPathsCache [emojiId] = path
87+ fileUpdateHandler.customEmojiPaths [emojiId] = path
8888 onUpdate()
8989 } else {
9090 downloadFile(file.id, 32 )
0 commit comments