You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
session-ios/Session/Emoji/Storage+Emoji.swift

37 lines
1.6 KiB
Swift

extension Storage {
private static let emojiPickerCollection = "EmojiPickerCollection"
private static let recentEmojiKey = "recentEmoji"
func getRecentEmoji(transaction: YapDatabaseReadTransaction) -> [EmojiWithSkinTones] {
var rawRecentEmoji = transaction.object(forKey: Self.recentEmojiKey, inCollection: Self.emojiPickerCollection) as? [String] ?? []
let defaultEmoji = ["🙈", "🙉", "🙊", "😈", "🥸", "🐀"].filter{ !rawRecentEmoji.contains($0) }
if rawRecentEmoji.count < 6 {
rawRecentEmoji.append(contentsOf: defaultEmoji[..<(defaultEmoji.count - rawRecentEmoji.count)])
}
return rawRecentEmoji.compactMap { EmojiWithSkinTones(rawValue: $0) }
}
func recordRecentEmoji(_ emoji: EmojiWithSkinTones, transaction: YapDatabaseReadWriteTransaction) {
let recentEmoji = getRecentEmoji(transaction: transaction)
guard recentEmoji.first != emoji else { return }
guard emoji.isNormalized else {
recordRecentEmoji(emoji.normalized, transaction: transaction)
return
}
var newRecentEmoji = recentEmoji
// Remove any existing entries for this emoji
newRecentEmoji.removeAll { emoji == $0 }
// Insert the selected emoji at the start of the list
newRecentEmoji.insert(emoji, at: 0)
// Truncate the recent emoji list to a maximum of 50 stored
newRecentEmoji = Array(newRecentEmoji[0..<min(50, newRecentEmoji.count)])
transaction.setObject(newRecentEmoji.map { $0.rawValue }, forKey: Self.recentEmojiKey, inCollection: Self.emojiPickerCollection)
}
}