diff --git a/submodules/TelegramCore/Sources/TelegramEngine/Stickers/SearchStickers.swift b/submodules/TelegramCore/Sources/TelegramEngine/Stickers/SearchStickers.swift index abe381b388d..187871d5d4a 100644 --- a/submodules/TelegramCore/Sources/TelegramEngine/Stickers/SearchStickers.swift +++ b/submodules/TelegramCore/Sources/TelegramEngine/Stickers/SearchStickers.swift @@ -142,12 +142,10 @@ func _internal_searchStickers(account: Account, query: String, scope: SearchStic var installedItems: [FoundStickerItem] = [] var installedAnimatedItems: [FoundStickerItem] = [] + var installedPremiumItems: [FoundStickerItem] = [] + for item in transaction.searchItemCollection(namespace: Namespaces.ItemCollection.CloudStickerPacks, query: searchQuery) { if let item = item as? StickerPackItem { - if item.file.isPremiumSticker && !isPremium { - continue - } - if !currentItems.contains(item.file.fileId) { var stringRepresentations: [String] = [] for key in item.indexKeys { @@ -158,7 +156,9 @@ func _internal_searchStickers(account: Account, query: String, scope: SearchStic } } if !recentItemsIds.contains(item.file.fileId) { - if item.file.isAnimatedSticker || item.file.isVideoSticker { + if item.file.isPremiumSticker { + installedPremiumItems.append(FoundStickerItem(file: item.file, stringRepresentations: stringRepresentations)) + } else if item.file.isAnimatedSticker || item.file.isVideoSticker { installedAnimatedItems.append(FoundStickerItem(file: item.file, stringRepresentations: stringRepresentations)) } else { installedItems.append(FoundStickerItem(file: item.file, stringRepresentations: stringRepresentations)) @@ -204,22 +204,34 @@ func _internal_searchStickers(account: Account, query: String, scope: SearchStic return (result, cached, isPremium, searchStickersConfiguration) } |> mapToSignal { localItems, cached, isPremium, searchStickersConfiguration -> Signal<[FoundStickerItem], NoError> in - var tempResult: [FoundStickerItem] = localItems if !scope.contains(.remote) { - return .single(tempResult) + return .single(localItems) } + + var tempResult: [FoundStickerItem] = [] let currentItemIds = Set(localItems.map { $0.file.fileId }) + var premiumItems: [FoundStickerItem] = [] + var otherItems: [FoundStickerItem] = [] + + for item in localItems { + if item.file.isPremiumSticker { + premiumItems.append(item) + } else { + otherItems.append(item) + } + } + if let cached = cached { var cachedItems: [FoundStickerItem] = [] var cachedAnimatedItems: [FoundStickerItem] = [] - + var cachedPremiumItems: [FoundStickerItem] = [] + for file in cached.items { - if file.isPremiumSticker && !isPremium { - continue - } if !currentItemIds.contains(file.fileId) { - if file.isAnimatedSticker || file.isVideoSticker { + if file.isPremiumSticker { + cachedPremiumItems.append(FoundStickerItem(file: file, stringRepresentations: [])) + } else if file.isAnimatedSticker || file.isVideoSticker { cachedAnimatedItems.append(FoundStickerItem(file: file, stringRepresentations: [])) } else { cachedItems.append(FoundStickerItem(file: file, stringRepresentations: [])) @@ -227,8 +239,42 @@ func _internal_searchStickers(account: Account, query: String, scope: SearchStic } } - tempResult.append(contentsOf: cachedAnimatedItems) - tempResult.append(contentsOf: cachedItems) + otherItems.append(contentsOf: cachedAnimatedItems) + otherItems.append(contentsOf: cachedItems) + + let allPremiumItems = premiumItems + cachedPremiumItems + let allOtherItems = otherItems + cachedAnimatedItems + cachedItems + + if isPremium { + let batchCount = Int(searchStickersConfiguration.normalStickersPerPremiumCount) + if batchCount == 0 { + tempResult.append(contentsOf: allPremiumItems) + tempResult.append(contentsOf: allOtherItems) + } else { + if allPremiumItems.isEmpty { + tempResult.append(contentsOf: allOtherItems) + } else { + var i = 0 + for premiumItem in allPremiumItems { + if i < allOtherItems.count { + for j in i ..< min(i + batchCount, allOtherItems.count) { + tempResult.append(allOtherItems[j]) + } + i += batchCount + } + tempResult.append(premiumItem) + } + if i < allOtherItems.count { + for j in i ..< allOtherItems.count { + tempResult.append(allOtherItems[j]) + } + } + } + } + } else { + tempResult.append(contentsOf: allOtherItems) + tempResult.append(contentsOf: allPremiumItems.prefix(max(0, Int(searchStickersConfiguration.premiumStickersCount)))) + } } let remote = account.network.request(Api.functions.messages.getStickers(emoticon: query, hash: cached?.hash ?? 0)) @@ -239,44 +285,72 @@ func _internal_searchStickers(account: Account, query: String, scope: SearchStic return account.postbox.transaction { transaction -> [FoundStickerItem] in switch result { case let .stickers(hash, stickers): - var items: [FoundStickerItem] = [] - var animatedItems: [FoundStickerItem] = [] - var premiumItems: [FoundStickerItem] = [] + var result: [FoundStickerItem] = [] + let currentItemIds = Set(localItems.map { $0.file.fileId }) - var allItems: [FoundStickerItem] = localItems - let currentItemIds = Set(allItems.map { $0.file.fileId }) + var premiumItems: [FoundStickerItem] = [] + var otherItems: [FoundStickerItem] = [] + for item in localItems { + if item.file.isPremiumSticker { + premiumItems.append(item) + } else { + otherItems.append(item) + } + } + + var foundItems: [FoundStickerItem] = [] + var foundAnimatedItems: [FoundStickerItem] = [] + var foundPremiumItems: [FoundStickerItem] = [] + var files: [TelegramMediaFile] = [] for sticker in stickers { if let file = telegramMediaFileFromApiDocument(sticker), let id = file.id { files.append(file) if !currentItemIds.contains(id) { if file.isPremiumSticker { - premiumItems.append(FoundStickerItem(file: file, stringRepresentations: [])) + foundPremiumItems.append(FoundStickerItem(file: file, stringRepresentations: [])) } else if file.isAnimatedSticker || file.isVideoSticker { - animatedItems.append(FoundStickerItem(file: file, stringRepresentations: [])) + foundAnimatedItems.append(FoundStickerItem(file: file, stringRepresentations: [])) } else { - items.append(FoundStickerItem(file: file, stringRepresentations: [])) + foundItems.append(FoundStickerItem(file: file, stringRepresentations: [])) } } } } - - allItems.append(contentsOf: animatedItems) - allItems.append(contentsOf: items) - var result: [FoundStickerItem] = [] + let allPremiumItems = premiumItems + foundPremiumItems + let allOtherItems = otherItems + foundAnimatedItems + foundItems + if isPremium { - if searchStickersConfiguration.normalStickersPerPremiumCount == 0 { - result.append(contentsOf: premiumItems) - result.append(contentsOf: allItems) + let batchCount = Int(searchStickersConfiguration.normalStickersPerPremiumCount) + if batchCount == 0 { + result.append(contentsOf: allPremiumItems) + result.append(contentsOf: allOtherItems) } else { - result.append(contentsOf: premiumItems) - result.append(contentsOf: allItems) + if allPremiumItems.isEmpty { + result.append(contentsOf: allOtherItems) + } else { + var i = 0 + for premiumItem in allPremiumItems { + if i < allOtherItems.count { + for j in i ..< min(i + batchCount, allOtherItems.count) { + result.append(allOtherItems[j]) + } + i += batchCount + } + result.append(premiumItem) + } + if i < allOtherItems.count { + for j in i ..< allOtherItems.count { + result.append(allOtherItems[j]) + } + } + } } } else { - result.append(contentsOf: allItems) - result.append(contentsOf: premiumItems.prefix(max(0, Int(searchStickersConfiguration.premiumStickersCount)))) + result.append(contentsOf: allOtherItems) + result.append(contentsOf: allPremiumItems.prefix(max(0, Int(searchStickersConfiguration.premiumStickersCount)))) } let currentTime = Int32(CFAbsoluteTimeGetCurrent() + kCFAbsoluteTimeIntervalSince1970) diff --git a/submodules/TelegramUI/Sources/ChatHistoryEntriesForView.swift b/submodules/TelegramUI/Sources/ChatHistoryEntriesForView.swift index bfb70557ab0..92861c43bcb 100644 --- a/submodules/TelegramUI/Sources/ChatHistoryEntriesForView.swift +++ b/submodules/TelegramUI/Sources/ChatHistoryEntriesForView.swift @@ -135,7 +135,7 @@ func chatHistoryEntriesForView( } if presentationData.largeEmoji, message.media.isEmpty { - if stickersEnabled && messageIsElligibleForLargeCustomEmoji(message) { + if messageIsElligibleForLargeCustomEmoji(message) { contentTypeHint = .animatedEmoji } else if stickersEnabled && message.text.count == 1, let _ = associatedData.animatedEmojiStickers[message.text.basicEmoji.0], (message.textEntitiesAttribute?.entities.isEmpty ?? true) { contentTypeHint = .animatedEmoji @@ -218,7 +218,7 @@ func chatHistoryEntriesForView( var contentTypeHint: ChatMessageEntryContentType = .generic if presentationData.largeEmoji, topMessage.media.isEmpty { - if stickersEnabled && messageIsElligibleForLargeCustomEmoji(topMessage) { + if messageIsElligibleForLargeCustomEmoji(topMessage) { contentTypeHint = .animatedEmoji } else if stickersEnabled && topMessage.text.count == 1, let _ = associatedData.animatedEmojiStickers[topMessage.text.basicEmoji.0] { contentTypeHint = .animatedEmoji diff --git a/submodules/TelegramUI/Sources/ChatMessageAnimatedStickerItemNode.swift b/submodules/TelegramUI/Sources/ChatMessageAnimatedStickerItemNode.swift index 1807f4aa389..02fae6f7bdf 100644 --- a/submodules/TelegramUI/Sources/ChatMessageAnimatedStickerItemNode.swift +++ b/submodules/TelegramUI/Sources/ChatMessageAnimatedStickerItemNode.swift @@ -543,17 +543,23 @@ class ChatMessageAnimatedStickerItemNode: ChatMessageItemView { var emojiFile: TelegramMediaFile? var emojiString: String? - if messageIsElligibleForLargeCustomEmoji(item.message) || (item.message.text.count > 1 && messageIsElligibleForLargeEmoji(item.message)) { + if messageIsElligibleForLargeCustomEmoji(item.message) || messageIsElligibleForLargeEmoji(item.message) { emojiString = item.message.text } - if emojiFile == nil && emojiString == nil { + if emojiFile == nil { emojiFile = item.associatedData.animatedEmojiStickers[emoji]?.first?.file } - if emojiFile == nil && emojiString == nil { + if emojiFile == nil { emojiFile = item.associatedData.animatedEmojiStickers[emoji.strippedEmoji]?.first?.file } + if item.message.text.count == 1 && emojiFile != nil { + emojiString = nil + } else if emojiString != nil { + emojiFile = nil + } + if self.emojiString != emojiString { self.emojiString = emojiString } else if self.emojiFile?.id != emojiFile?.id { @@ -953,7 +959,7 @@ class ChatMessageAnimatedStickerItemNode: ChatMessageItemView { } } else if let _ = emojiString { imageVerticalInset = 0.0 - imageTopPadding = 12.0 + imageTopPadding = 16.0 imageBottomPadding = 20.0 let baseWidth = params.width