Skip to content

Commit

Permalink
Merge commit '348acd4ac26bb14b24f95d78d4ca9fb9bda200f2'
Browse files Browse the repository at this point in the history
  • Loading branch information
Ali committed Aug 3, 2022
2 parents f712c2b + 348acd4 commit 551674a
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
Expand Down Expand Up @@ -204,31 +204,77 @@ 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<MediaId>(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: []))
}
}
}

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))
Expand All @@ -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<MediaId>(localItems.map { $0.file.fileId })

var allItems: [FoundStickerItem] = localItems
let currentItemIds = Set<MediaId>(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)
Expand Down
4 changes: 2 additions & 2 deletions submodules/TelegramUI/Sources/ChatHistoryEntriesForView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 551674a

Please sign in to comment.