TalkAccount.swift 1.1 KB

1234567891011121314151617181920212223242526272829303132333435
  1. //
  2. // SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  3. // SPDX-License-Identifier: GPL-3.0-or-later
  4. //
  5. import Foundation
  6. import SwiftyAttributes
  7. @objc extension TalkAccount {
  8. public var defaultEmojis: [String] {
  9. return ["👍", "❤️", "😂", "😅"]
  10. }
  11. public var frequentlyUsedEmojis: [String] {
  12. guard let data = self.frequentlyUsedEmojisJSONString.data(using: .utf8),
  13. let jsonData = try? JSONSerialization.jsonObject(with: data) as? [String: Int]
  14. else { return defaultEmojis }
  15. // First sort by value (the amount), then by key (the emoji)
  16. var emojis = jsonData.sorted(by: {
  17. $0.value != $1.value ?
  18. $0.value > $1.value :
  19. $0.key < $1.key
  20. }).prefix(4).map({ $0.key })
  21. if emojis.count < 4 {
  22. // Fill up to 4 emojis
  23. let uniqueDefaultEmojis = defaultEmojis.filter { !emojis.contains($0) }
  24. emojis.append(contentsOf: uniqueDefaultEmojis.prefix(4 - emojis.count))
  25. }
  26. return emojis
  27. }
  28. }