From b6036d5eb88ef044c06d9ad183e04451b01a64fa Mon Sep 17 00:00:00 2001 From: Ilya Laktyushin Date: Thu, 1 Feb 2024 04:47:38 +0400 Subject: [PATCH] Search installed emoji packs --- .../Stickers/SearchStickers.swift | 33 +++++++++++++++++++ .../Stickers/TelegramEngineStickers.swift | 4 +++ .../Sources/ChatEntityKeyboardInputNode.swift | 13 ++++++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Stickers/SearchStickers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Stickers/SearchStickers.swift index 9470ac2a76..70159a16c5 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Stickers/SearchStickers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Stickers/SearchStickers.swift @@ -639,6 +639,39 @@ func _internal_searchStickerSets(postbox: Postbox, query: String) -> Signal switchToLatest } +func _internal_searchEmojiSets(postbox: Postbox, query: String) -> Signal { + return postbox.transaction { transaction -> Signal in + let infos = transaction.getItemCollectionsInfos(namespace: Namespaces.ItemCollection.CloudEmojiPacks) + + var collections: [(ItemCollectionId, ItemCollectionInfo)] = [] + var topItems: [ItemCollectionId: ItemCollectionItem] = [:] + var entries: [ItemCollectionViewEntry] = [] + for info in infos { + if let info = info.1 as? StickerPackCollectionInfo { + let split = info.title.split(separator: " ") + if !split.filter({$0.lowercased().hasPrefix(query.lowercased())}).isEmpty || info.shortName.lowercased().hasPrefix(query.lowercased()) { + collections.append((info.id, info)) + } + } + } + var index: Int32 = 0 + + for info in collections { + let items = transaction.getItemCollectionItems(collectionId: info.0) + let values = items.map({ ItemCollectionViewEntry(index: ItemCollectionViewEntryIndex(collectionIndex: index, collectionId: info.0, itemIndex: $0.index), item: $0) }) + entries.append(contentsOf: values) + if let first = items.first { + topItems[info.0] = first + } + index += 1 + } + + let result = FoundStickerSets(infos: collections.map { ($0.0, $0.1, topItems[$0.0], true) }, entries: entries) + + return .single(result) + } |> switchToLatest +} + func _internal_searchGifs(account: Account, query: String, nextOffset: String = "") -> Signal { return account.postbox.transaction { transaction -> String in let configuration = currentSearchBotsConfiguration(transaction: transaction) diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Stickers/TelegramEngineStickers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Stickers/TelegramEngineStickers.swift index e124cf4659..bd7f762e09 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Stickers/TelegramEngineStickers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Stickers/TelegramEngineStickers.swift @@ -45,6 +45,10 @@ public extension TelegramEngine { public func searchStickerSets(query: String) -> Signal { return _internal_searchStickerSets(postbox: self.account.postbox, query: query) } + + public func searchEmojiSets(query: String) -> Signal { + return _internal_searchEmojiSets(postbox: self.account.postbox, query: query) + } public func searchGifs(query: String, nextOffset: String = "") -> Signal { return _internal_searchGifs(account: self.account, query: query, nextOffset: nextOffset) diff --git a/submodules/TelegramUI/Components/ChatEntityKeyboardInputNode/Sources/ChatEntityKeyboardInputNode.swift b/submodules/TelegramUI/Components/ChatEntityKeyboardInputNode/Sources/ChatEntityKeyboardInputNode.swift index b25f6f1339..8cc36dd5aa 100644 --- a/submodules/TelegramUI/Components/ChatEntityKeyboardInputNode/Sources/ChatEntityKeyboardInputNode.swift +++ b/submodules/TelegramUI/Components/ChatEntityKeyboardInputNode/Sources/ChatEntityKeyboardInputNode.swift @@ -885,9 +885,16 @@ public final class ChatEntityKeyboardInputNode: ChatInputNode { let remotePacksSignal: Signal<(sets: FoundStickerSets, isFinalResult: Bool), NoError> if hasPremium { remoteSignal = context.engine.stickers.searchEmoji(emojiString: Array(allEmoticons.keys)) - remotePacksSignal = .single((FoundStickerSets(), false)) |> then(context.engine.stickers.searchEmojiSetsRemotely(query: query) |> map { - ($0, true) - }) + remotePacksSignal = context.engine.stickers.searchEmojiSets(query: query) + |> mapToSignal { localResult in + return .single((localResult, false)) + |> then( + context.engine.stickers.searchEmojiSetsRemotely(query: query) + |> map { remoteResult in + return (localResult.merge(with: remoteResult), true) + } + ) + } } else { remoteSignal = .single(([], true)) remotePacksSignal = .single((FoundStickerSets(), true))