From 60e94120ccdcbea98958dffb5752be1692b20386 Mon Sep 17 00:00:00 2001 From: Stefan Ceriu Date: Mon, 16 Dec 2024 17:42:33 +0200 Subject: [PATCH] Implement frequency tracking for frequently used emojis --- ElementX/Sources/Application/AppSettings.swift | 2 +- .../Services/Emojis/EmojiProvider.swift | 18 +++++++++++++----- .../Emojis/EmojiProviderProtocol.swift | 9 +++++++++ 3 files changed, 23 insertions(+), 6 deletions(-) diff --git a/ElementX/Sources/Application/AppSettings.swift b/ElementX/Sources/Application/AppSettings.swift index 12c2ed0d2d..44bcb6b9fa 100644 --- a/ElementX/Sources/Application/AppSettings.swift +++ b/ElementX/Sources/Application/AppSettings.swift @@ -232,7 +232,7 @@ final class AppSettings { @UserPreference(key: UserDefaultsKeys.hasRunIdentityConfirmationOnboarding, defaultValue: false, storageType: .userDefaults(store)) var hasRunIdentityConfirmationOnboarding - @UserPreference(key: UserDefaultsKeys.frequentlyUsedSystemEmojis, defaultValue: [String](), storageType: .userDefaults(store)) + @UserPreference(key: UserDefaultsKeys.frequentlyUsedSystemEmojis, defaultValue: [FrequentlyUsedEmoji](), storageType: .userDefaults(store)) var frequentlyUsedSystemEmojis // MARK: - Home Screen diff --git a/ElementX/Sources/Services/Emojis/EmojiProvider.swift b/ElementX/Sources/Services/Emojis/EmojiProvider.swift index ce604b135d..877e0adda3 100644 --- a/ElementX/Sources/Services/Emojis/EmojiProvider.swift +++ b/ElementX/Sources/Services/Emojis/EmojiProvider.swift @@ -7,7 +7,6 @@ import Emojibase import Foundation -import OrderedCollections class EmojiProvider: EmojiProviderProtocol { private let maxFrequentEmojis = 20 @@ -61,7 +60,7 @@ class EmojiProvider: EmojiProviderProtocol { return [] } - return appSettings.frequentlyUsedSystemEmojis + return appSettings.frequentlyUsedSystemEmojis.map(\.key) } func markEmojiAsFrequentlyUsed(_ emoji: String) { @@ -69,10 +68,19 @@ class EmojiProvider: EmojiProviderProtocol { return } - var uniqueOrderedRecents = OrderedSet(appSettings.frequentlyUsedSystemEmojis) - uniqueOrderedRecents.insert(emoji, at: 0) + let frequentlyUsed = if !frequentlyUsedSystemEmojis().contains(emoji) { + appSettings.frequentlyUsedSystemEmojis + [.init(count: 0, key: emoji)] + } else { + appSettings.frequentlyUsedSystemEmojis.map { frequentlyUsedEmoji in + if frequentlyUsedEmoji.key == emoji { + return FrequentlyUsedEmoji(count: frequentlyUsedEmoji.count + 1, key: emoji) + } + + return frequentlyUsedEmoji + } + } - appSettings.frequentlyUsedSystemEmojis = Array(uniqueOrderedRecents) + appSettings.frequentlyUsedSystemEmojis = frequentlyUsed.sorted { $0.count > $1.count } } // MARK: - Private diff --git a/ElementX/Sources/Services/Emojis/EmojiProviderProtocol.swift b/ElementX/Sources/Services/Emojis/EmojiProviderProtocol.swift index b0137716e8..377560b2ef 100644 --- a/ElementX/Sources/Services/Emojis/EmojiProviderProtocol.swift +++ b/ElementX/Sources/Services/Emojis/EmojiProviderProtocol.swift @@ -31,6 +31,15 @@ enum EmojiProviderState { case loaded([EmojiCategory]) } +struct FrequentlyUsedEmoji: Codable, Hashable { + let count: UInt + let key: String + + static func == (lhs: FrequentlyUsedEmoji, rhs: FrequentlyUsedEmoji) -> Bool { + lhs.key == rhs.key + } +} + @MainActor protocol EmojiProviderProtocol { var state: EmojiProviderState { get }