2020-10-18 16:22:12 +00:00
|
|
|
//
|
|
|
|
// BaseEmojiLabel.swift
|
|
|
|
// Tusker
|
|
|
|
//
|
|
|
|
// Created by Shadowfacts on 10/18/20.
|
|
|
|
// Copyright © 2020 Shadowfacts. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
|
|
|
import Pachyderm
|
2022-02-04 04:11:29 +00:00
|
|
|
import WebURLFoundationExtras
|
2020-10-18 16:22:12 +00:00
|
|
|
|
|
|
|
private let emojiRegex = try! NSRegularExpression(pattern: ":(\\w+):", options: [])
|
|
|
|
|
2021-05-22 17:42:53 +00:00
|
|
|
protocol BaseEmojiLabel: AnyObject {
|
2020-10-18 16:22:12 +00:00
|
|
|
var emojiIdentifier: String? { get set }
|
|
|
|
var emojiRequests: [ImageCache.Request] { get set }
|
|
|
|
var emojiFont: UIFont { get }
|
|
|
|
var emojiTextColor: UIColor { get }
|
|
|
|
}
|
|
|
|
|
|
|
|
extension BaseEmojiLabel {
|
2021-11-07 19:23:56 +00:00
|
|
|
func replaceEmojis(in attributedString: NSAttributedString, emojis: [Emoji], identifier: String?, setAttributedString: @escaping (_ attributedString: NSAttributedString, _ didReplaceEmojis: Bool) -> Void) {
|
|
|
|
// blergh
|
|
|
|
precondition(Thread.isMainThread)
|
|
|
|
|
2022-09-12 02:19:54 +00:00
|
|
|
emojiIdentifier = identifier
|
2021-11-07 19:23:56 +00:00
|
|
|
emojiRequests.forEach { $0.cancel() }
|
|
|
|
emojiRequests = []
|
|
|
|
|
2021-11-07 18:11:49 +00:00
|
|
|
guard !emojis.isEmpty else {
|
2021-11-07 19:23:56 +00:00
|
|
|
setAttributedString(attributedString, false)
|
2021-11-07 18:11:49 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
let matches = emojiRegex.matches(in: attributedString.string, options: [], range: NSRange(location: 0, length: attributedString.length))
|
2020-10-18 16:22:12 +00:00
|
|
|
guard !matches.isEmpty else {
|
2021-11-07 19:23:56 +00:00
|
|
|
setAttributedString(attributedString, false)
|
2020-10-18 16:22:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-09-12 02:09:04 +00:00
|
|
|
let emojiImages = MultiThreadDictionary<String, UIImage>()
|
2020-10-18 16:22:12 +00:00
|
|
|
var foundEmojis = false
|
|
|
|
|
|
|
|
let group = DispatchGroup()
|
|
|
|
|
|
|
|
for emoji in emojis {
|
|
|
|
// only make requests for emojis that are present in the text to avoid making unnecessary network requests
|
2021-11-07 18:11:49 +00:00
|
|
|
guard matches.contains(where: { (match) -> Bool in
|
|
|
|
let matchShortcode = (attributedString.string as NSString).substring(with: match.range(at: 1))
|
2020-10-18 16:22:12 +00:00
|
|
|
return emoji.shortcode == matchShortcode
|
|
|
|
}) else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
foundEmojis = true
|
|
|
|
|
2022-02-04 04:11:29 +00:00
|
|
|
if let image = ImageCache.emojis.get(URL(emoji.url)!)?.image {
|
2021-11-07 19:23:56 +00:00
|
|
|
// if the image is cached, add it immediately
|
|
|
|
emojiImages[emoji.shortcode] = image
|
|
|
|
} else {
|
|
|
|
// otherwise, perform the network request
|
|
|
|
|
|
|
|
group.enter()
|
|
|
|
// todo: ImageCache.emojis.get here will re-check the memory and disk caches, there should be another method to force-refetch
|
2022-02-04 04:11:29 +00:00
|
|
|
let request = ImageCache.emojis.get(URL(emoji.url)!) { (_, image) in
|
2021-11-07 19:23:56 +00:00
|
|
|
guard let image = image,
|
2022-02-04 04:11:29 +00:00
|
|
|
let transformedImage = ImageGrayscalifier.convertIfNecessary(url: URL(emoji.url)!, image: image) else {
|
2021-11-07 19:23:56 +00:00
|
|
|
group.leave()
|
|
|
|
return
|
|
|
|
}
|
2022-09-12 02:09:04 +00:00
|
|
|
emojiImages[emoji.shortcode] = transformedImage
|
|
|
|
group.leave()
|
2021-11-07 19:23:56 +00:00
|
|
|
}
|
|
|
|
if let request = request {
|
|
|
|
emojiRequests.append(request)
|
2020-10-18 16:22:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
guard foundEmojis else {
|
2021-11-07 19:23:56 +00:00
|
|
|
setAttributedString(attributedString, false)
|
2020-10-18 16:22:12 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-11-07 19:23:56 +00:00
|
|
|
func buildStringWithEmojisReplaced(usePlaceholders: Bool) -> NSAttributedString {
|
2021-11-07 18:11:49 +00:00
|
|
|
let mutAttrString = NSMutableAttributedString(attributedString: attributedString)
|
2021-11-07 19:23:56 +00:00
|
|
|
|
2022-09-12 02:09:04 +00:00
|
|
|
// OSAllocatedUnfairLock.withLock expects a @Sendable closure, so this warns about captures of non-sendable types (attribute dstrings, text checking results)
|
|
|
|
// even though the closures is invoked on the same thread that withLock is called, so it's unclear why it needs to be @Sendable (FB11494878)
|
|
|
|
// so, just ignore the warnings
|
2022-11-05 15:00:14 +00:00
|
|
|
let emojiAttachments = emojiImages.withLock {
|
|
|
|
$0.mapValues { image in
|
|
|
|
NSTextAttachment(emojiImage: image, in: self.emojiFont, with: self.emojiTextColor)
|
2020-10-18 16:22:12 +00:00
|
|
|
}
|
|
|
|
}
|
2022-11-05 15:00:14 +00:00
|
|
|
let placeholder = usePlaceholders ? NSTextAttachment(emojiPlaceholderIn: self.emojiFont) : nil
|
|
|
|
|
|
|
|
// replaces the emojis starting from the end of the string as to not alter the indices of preceeding emojis
|
|
|
|
for match in matches.reversed() {
|
|
|
|
let shortcode = (attributedString.string as NSString).substring(with: match.range(at: 1))
|
|
|
|
let attachment: NSTextAttachment
|
|
|
|
if let emoji = emojiAttachments[shortcode] {
|
|
|
|
attachment = emoji
|
|
|
|
} else if usePlaceholders {
|
|
|
|
attachment = placeholder!
|
|
|
|
} else {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
let attachmentStr = NSAttributedString(attachment: attachment)
|
|
|
|
mutAttrString.replaceCharacters(in: match.range, with: attachmentStr)
|
|
|
|
}
|
2020-10-18 16:22:12 +00:00
|
|
|
|
2021-11-07 19:23:56 +00:00
|
|
|
return mutAttrString
|
|
|
|
}
|
|
|
|
|
|
|
|
if emojiRequests.isEmpty {
|
|
|
|
// waiting on the group when there are only cached emojis results in a bad first layout,
|
|
|
|
// because it would still wait until the next runloop iteration
|
|
|
|
// but, since there were no requests, we can generate the correct string immediately
|
|
|
|
let replacedString = buildStringWithEmojisReplaced(usePlaceholders: false)
|
|
|
|
setAttributedString(replacedString, true)
|
|
|
|
} else {
|
|
|
|
// if we did find emojis and there are pending network requests,
|
|
|
|
// set an attributed strings with placeholders for non-loaded emojis to avoid layout shift
|
|
|
|
setAttributedString(buildStringWithEmojisReplaced(usePlaceholders: true), true)
|
|
|
|
|
|
|
|
group.notify(queue: .main) { [weak self] in
|
|
|
|
// if e.g. the account changes before all emojis are loaded, don't bother trying to set them
|
|
|
|
guard let self = self, self.emojiIdentifier == identifier else { return }
|
|
|
|
|
|
|
|
let replacedString = buildStringWithEmojisReplaced(usePlaceholders: false)
|
|
|
|
setAttributedString(replacedString, true)
|
|
|
|
}
|
2020-10-18 16:22:12 +00:00
|
|
|
}
|
|
|
|
}
|
2021-11-07 18:11:49 +00:00
|
|
|
|
2021-11-07 19:23:56 +00:00
|
|
|
func replaceEmojis(in string: String, emojis: [Emoji], identifier: String?, setAttributedString: @escaping (_ attributedString: NSAttributedString, _ didReplaceEmojis: Bool) -> Void) {
|
|
|
|
replaceEmojis(in: NSAttributedString(string: string), emojis: emojis, identifier: identifier, setAttributedString: setAttributedString)
|
2021-11-07 18:11:49 +00:00
|
|
|
}
|
2020-10-18 16:22:12 +00:00
|
|
|
}
|