diff --git a/Data Sources/AutocompleteDataSource.swift b/Data Sources/AutocompleteDataSource.swift index 888f2d5..66685fe 100644 --- a/Data Sources/AutocompleteDataSource.swift +++ b/Data Sources/AutocompleteDataSource.swift @@ -36,7 +36,7 @@ final class AutocompleteDataSource: UICollectionViewDiffableDataSource { - $0.emoji = $2.applyingDefaultSkinTone(identityContext: parentViewModel.identityContext) + $0.viewModel = EmojiViewModel(emoji: $2, identityContext: parentViewModel.identityContext) } super.init(collectionView: collectionView) { diff --git a/Extensions/PickerEmoji+Extensions.swift b/Extensions/PickerEmoji+Extensions.swift index d4136f6..d80de63 100644 --- a/Extensions/PickerEmoji+Extensions.swift +++ b/Extensions/PickerEmoji+Extensions.swift @@ -3,17 +3,6 @@ import UIKit import ViewModels -extension PickerEmoji { - func applyingDefaultSkinTone(identityContext: IdentityContext) -> PickerEmoji { - if case let .system(systemEmoji, inFrequentlyUsed) = self, - let defaultEmojiSkinTone = identityContext.appPreferences.defaultEmojiSkinTone { - return .system(systemEmoji.applying(skinTone: defaultEmojiSkinTone), inFrequentlyUsed: inFrequentlyUsed) - } else { - return self - } - } -} - extension Dictionary where Key == PickerEmoji.Category, Value == [PickerEmoji] { func snapshot() -> NSDiffableDataSourceSnapshot { var snapshot = NSDiffableDataSourceSnapshot() diff --git a/View Controllers/EmojiPickerViewController.swift b/View Controllers/EmojiPickerViewController.swift index 21a4a49..a5b9679 100644 --- a/View Controllers/EmojiPickerViewController.swift +++ b/View Controllers/EmojiPickerViewController.swift @@ -22,7 +22,7 @@ final class EmojiPickerViewController: UICollectionViewController { { [weak self] in guard let self = self else { return } - $0.emoji = $2.applyingDefaultSkinTone(identityContext: self.viewModel.identityContext) + $0.viewModel = EmojiViewModel(emoji: $2, identityContext: self.viewModel.identityContext) } let headerRegistration = UICollectionView.SupplementaryRegistration diff --git a/ViewModels/Sources/ViewModels/Extensions/PickerEmoji+Extensions.swift b/ViewModels/Sources/ViewModels/Extensions/PickerEmoji+Extensions.swift new file mode 100644 index 0000000..d367d4c --- /dev/null +++ b/ViewModels/Sources/ViewModels/Extensions/PickerEmoji+Extensions.swift @@ -0,0 +1,14 @@ +// Copyright © 2021 Metabolist. All rights reserved. + +import Foundation + +public extension PickerEmoji { + func applyingDefaultSkinTone(identityContext: IdentityContext) -> PickerEmoji { + if case let .system(systemEmoji, inFrequentlyUsed) = self, + let defaultEmojiSkinTone = identityContext.appPreferences.defaultEmojiSkinTone { + return .system(systemEmoji.applying(skinTone: defaultEmojiSkinTone), inFrequentlyUsed: inFrequentlyUsed) + } else { + return self + } + } +} diff --git a/ViewModels/Sources/ViewModels/View Models/EmojiViewModel.swift b/ViewModels/Sources/ViewModels/View Models/EmojiViewModel.swift new file mode 100644 index 0000000..baacb91 --- /dev/null +++ b/ViewModels/Sources/ViewModels/View Models/EmojiViewModel.swift @@ -0,0 +1,30 @@ +// Copyright © 2021 Metabolist. All rights reserved. + +import Foundation + +public struct EmojiViewModel { + let identityContext: IdentityContext + + private let emoji: PickerEmoji + + public init(emoji: PickerEmoji, identityContext: IdentityContext) { + self.emoji = emoji.applyingDefaultSkinTone(identityContext: identityContext) + self.identityContext = identityContext + } +} + +public extension EmojiViewModel { + var name: String { emoji.name } + + var system: Bool { emoji.system } + + var url: URL? { + guard case let .custom(emoji, _) = emoji else { return nil } + + if identityContext.appPreferences.animateCustomEmojis { + return emoji.url + } else { + return emoji.staticUrl + } + } +} diff --git a/Views/UIKit/Collection View Cells/EmojiCollectionViewCell.swift b/Views/UIKit/Collection View Cells/EmojiCollectionViewCell.swift index 2fe4b71..6d4599a 100644 --- a/Views/UIKit/Collection View Cells/EmojiCollectionViewCell.swift +++ b/Views/UIKit/Collection View Cells/EmojiCollectionViewCell.swift @@ -4,12 +4,12 @@ import UIKit import ViewModels final class EmojiCollectionViewCell: UICollectionViewCell { - var emoji: PickerEmoji? + var viewModel: EmojiViewModel? override func updateConfiguration(using state: UICellConfigurationState) { - guard let emoji = emoji else { return } + guard let viewModel = viewModel else { return } - contentConfiguration = EmojiContentConfiguration(emoji: emoji) + contentConfiguration = EmojiContentConfiguration(viewModel: viewModel) var backgroundConfiguration = UIBackgroundConfiguration.listPlainCell().updated(for: state) diff --git a/Views/UIKit/Content Configurations/EmojiContentConfiguration.swift b/Views/UIKit/Content Configurations/EmojiContentConfiguration.swift index 463c61c..d62c1ec 100644 --- a/Views/UIKit/Content Configurations/EmojiContentConfiguration.swift +++ b/Views/UIKit/Content Configurations/EmojiContentConfiguration.swift @@ -4,7 +4,7 @@ import UIKit import ViewModels struct EmojiContentConfiguration { - let emoji: PickerEmoji + let viewModel: EmojiViewModel } extension EmojiContentConfiguration: UIContentConfiguration { diff --git a/Views/UIKit/Content Configurations/EmojiView.swift b/Views/UIKit/Content Configurations/EmojiView.swift index 6a1eb8a..09d54c4 100644 --- a/Views/UIKit/Content Configurations/EmojiView.swift +++ b/Views/UIKit/Content Configurations/EmojiView.swift @@ -68,22 +68,18 @@ private extension EmojiView { } func applyEmojiConfiguration() { - imageView.isHidden = emojiConfiguration.emoji.system - - if case let .custom(emoji, _) = emojiConfiguration.emoji { - imageView.isHidden = false - emojiLabel.isHidden = true - - // TODO: Use static URL if emoji animation preference is false - imageView.sd_setImage(with: emoji.url) - accessibilityLabel = emoji.shortcode - } else { - imageView.isHidden = true + if emojiConfiguration.viewModel.system { emojiLabel.isHidden = false - - emojiLabel.text = emojiConfiguration.emoji.name - accessibilityLabel = emojiConfiguration.emoji.name + emojiLabel.text = emojiConfiguration.viewModel.name + imageView.isHidden = true + } else { + emojiLabel.isHidden = true + emojiLabel.text = nil + imageView.isHidden = false + imageView.sd_setImage(with: emojiConfiguration.viewModel.url) } + + accessibilityLabel = emojiConfiguration.viewModel.name } func setupAccessibility() {