Apply emoji animation preference in picker

This commit is contained in:
Justin Mazzocchi 2021-02-22 20:40:08 -08:00
parent 97bd008ea3
commit e5d8a4d132
No known key found for this signature in database
GPG key ID: E223E6937AAFB01C
8 changed files with 60 additions and 31 deletions

View file

@ -36,7 +36,7 @@ final class AutocompleteDataSource: UICollectionViewDiffableDataSource<Autocompl
}
let emojiRegistration = UICollectionView.CellRegistration<EmojiCollectionViewCell, PickerEmoji> {
$0.emoji = $2.applyingDefaultSkinTone(identityContext: parentViewModel.identityContext)
$0.viewModel = EmojiViewModel(emoji: $2, identityContext: parentViewModel.identityContext)
}
super.init(collectionView: collectionView) {

View file

@ -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<PickerEmoji.Category, PickerEmoji> {
var snapshot = NSDiffableDataSourceSnapshot<PickerEmoji.Category, PickerEmoji>()

View file

@ -22,7 +22,7 @@ final class EmojiPickerViewController: UICollectionViewController {
<EmojiCollectionViewCell, PickerEmoji> { [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

View file

@ -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
}
}
}

View file

@ -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
}
}
}

View file

@ -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)

View file

@ -4,7 +4,7 @@ import UIKit
import ViewModels
struct EmojiContentConfiguration {
let emoji: PickerEmoji
let viewModel: EmojiViewModel
}
extension EmojiContentConfiguration: UIContentConfiguration {

View file

@ -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() {