2021-01-14 18:49:53 +01:00
|
|
|
// Copyright © 2021 Metabolist. All rights reserved.
|
|
|
|
|
|
|
|
import Combine
|
|
|
|
import UIKit
|
|
|
|
import ViewModels
|
|
|
|
|
|
|
|
final class EmojiPickerViewController: UIViewController {
|
|
|
|
let searchBar = UISearchBar()
|
|
|
|
|
|
|
|
private let viewModel: EmojiPickerViewModel
|
2021-01-15 11:13:10 +01:00
|
|
|
private let selectionAction: (PickerEmoji) -> Void
|
|
|
|
private let dismissAction: () -> Void
|
2021-01-15 22:43:46 +01:00
|
|
|
private let skinToneButton = UIButton()
|
2021-01-14 18:49:53 +01:00
|
|
|
private var cancellables = Set<AnyCancellable>()
|
|
|
|
|
2021-01-15 11:13:10 +01:00
|
|
|
private lazy var collectionView: UICollectionView = {
|
|
|
|
let itemSize = NSCollectionLayoutSize(
|
|
|
|
widthDimension: .absolute(.minimumButtonDimension),
|
|
|
|
heightDimension: .absolute(.minimumButtonDimension))
|
|
|
|
let item = NSCollectionLayoutItem(layoutSize: itemSize)
|
|
|
|
let groupSize = NSCollectionLayoutSize(
|
|
|
|
widthDimension: .fractionalWidth(1.0),
|
|
|
|
heightDimension: .estimated(.minimumButtonDimension))
|
|
|
|
let group = NSCollectionLayoutGroup.horizontal(layoutSize: groupSize, subitems: [item])
|
|
|
|
|
|
|
|
group.interItemSpacing = .flexible(.defaultSpacing)
|
|
|
|
|
|
|
|
let section = NSCollectionLayoutSection(group: group)
|
|
|
|
|
|
|
|
section.interGroupSpacing = .defaultSpacing
|
|
|
|
section.contentInsets = NSDirectionalEdgeInsets(
|
|
|
|
top: .defaultSpacing,
|
|
|
|
leading: .defaultSpacing,
|
|
|
|
bottom: .defaultSpacing,
|
|
|
|
trailing: .defaultSpacing)
|
|
|
|
|
|
|
|
let headerSize = NSCollectionLayoutSize(
|
|
|
|
widthDimension: .fractionalWidth(1),
|
|
|
|
heightDimension: .estimated(.defaultSpacing))
|
|
|
|
let header = NSCollectionLayoutBoundarySupplementaryItem(
|
|
|
|
layoutSize: headerSize,
|
|
|
|
elementKind: Self.headerElementKind,
|
|
|
|
alignment: .top)
|
|
|
|
|
|
|
|
section.boundarySupplementaryItems = [header]
|
|
|
|
|
|
|
|
let layout = UICollectionViewCompositionalLayout(section: section)
|
|
|
|
|
|
|
|
return UICollectionView(frame: .zero, collectionViewLayout: layout)
|
|
|
|
}()
|
|
|
|
|
|
|
|
private lazy var dataSource: UICollectionViewDiffableDataSource<PickerEmoji.Category, PickerEmoji> = {
|
|
|
|
let cellRegistration = UICollectionView.CellRegistration
|
2021-01-15 22:43:46 +01:00
|
|
|
<EmojiCollectionViewCell, PickerEmoji> { [weak self] in
|
|
|
|
$0.emoji = self?.applyingDefaultSkinTone(emoji: $2) ?? $2
|
2021-01-15 11:13:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
let headerRegistration = UICollectionView.SupplementaryRegistration
|
|
|
|
<EmojiCategoryHeaderView>(elementKind: "Header") { [weak self] in
|
|
|
|
$0.label.text = self?.dataSource.snapshot().sectionIdentifiers[$2.section].displayName
|
|
|
|
}
|
|
|
|
|
|
|
|
let dataSource = UICollectionViewDiffableDataSource
|
|
|
|
<PickerEmoji.Category, PickerEmoji>(collectionView: collectionView) {
|
|
|
|
$0.dequeueConfiguredReusableCell(using: cellRegistration, for: $1, item: $2)
|
|
|
|
}
|
|
|
|
|
|
|
|
dataSource.supplementaryViewProvider = {
|
|
|
|
$0.dequeueConfiguredReusableSupplementary(using: headerRegistration, for: $2)
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataSource
|
|
|
|
}()
|
|
|
|
|
2021-01-15 22:43:46 +01:00
|
|
|
private lazy var defaultSkinToneSelectionMenu: UIMenu = {
|
|
|
|
let clearSkinToneAction = UIAction(title: SystemEmoji.SkinTone.noneExample) { [weak self] _ in
|
|
|
|
self?.skinToneButton.setTitle(SystemEmoji.SkinTone.noneExample, for: .normal)
|
2021-01-26 01:06:35 +01:00
|
|
|
self?.viewModel.identityContext.appPreferences.defaultEmojiSkinTone = nil
|
2021-01-15 22:43:46 +01:00
|
|
|
self?.reloadVisibleItems()
|
|
|
|
}
|
|
|
|
|
|
|
|
let setSkinToneActions = SystemEmoji.SkinTone.allCases.map { [weak self] skinTone in
|
|
|
|
UIAction(title: skinTone.example) { _ in
|
|
|
|
self?.skinToneButton.setTitle(skinTone.example, for: .normal)
|
2021-01-26 01:06:35 +01:00
|
|
|
self?.viewModel.identityContext.appPreferences.defaultEmojiSkinTone = skinTone
|
2021-01-15 22:43:46 +01:00
|
|
|
self?.reloadVisibleItems()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return UIMenu(
|
|
|
|
title: NSLocalizedString("emoji.default-skin-tone", comment: ""),
|
|
|
|
children: [clearSkinToneAction] + setSkinToneActions)
|
|
|
|
}()
|
|
|
|
|
2021-01-15 11:13:10 +01:00
|
|
|
init(viewModel: EmojiPickerViewModel,
|
|
|
|
selectionAction: @escaping (PickerEmoji) -> Void,
|
|
|
|
dismissAction: @escaping () -> Void) {
|
2021-01-14 18:49:53 +01:00
|
|
|
self.viewModel = viewModel
|
2021-01-15 11:13:10 +01:00
|
|
|
self.selectionAction = selectionAction
|
|
|
|
self.dismissAction = dismissAction
|
2021-01-14 18:49:53 +01:00
|
|
|
|
|
|
|
super.init(nibName: nil, bundle: nil)
|
|
|
|
}
|
|
|
|
|
|
|
|
@available(*, unavailable)
|
|
|
|
required init?(coder: NSCoder) {
|
|
|
|
fatalError("init(coder:) has not been implemented")
|
|
|
|
}
|
|
|
|
|
2021-01-15 22:43:46 +01:00
|
|
|
// swiftlint:disable:next function_body_length
|
2021-01-14 18:49:53 +01:00
|
|
|
override func viewDidLoad() {
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
|
|
view.addSubview(searchBar)
|
|
|
|
searchBar.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
searchBar.searchBarStyle = .minimal
|
2021-01-15 11:13:10 +01:00
|
|
|
searchBar.placeholder = NSLocalizedString("emoji.search", comment: "")
|
|
|
|
searchBar.searchTextField.addAction(
|
2021-01-16 03:22:03 +01:00
|
|
|
UIAction { [weak self] _ in
|
|
|
|
self?.viewModel.query = self?.searchBar.text ?? ""
|
|
|
|
self?.collectionView.setContentOffset(.zero, animated: false)
|
|
|
|
},
|
2021-01-15 11:13:10 +01:00
|
|
|
for: .editingChanged)
|
|
|
|
|
2021-01-15 22:43:46 +01:00
|
|
|
view.addSubview(skinToneButton)
|
|
|
|
skinToneButton.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
skinToneButton.titleLabel?.adjustsFontSizeToFitWidth = true
|
|
|
|
skinToneButton.setTitle(
|
2021-01-26 01:06:35 +01:00
|
|
|
viewModel.identityContext.appPreferences.defaultEmojiSkinTone?.example ?? SystemEmoji.SkinTone.noneExample,
|
2021-01-15 22:43:46 +01:00
|
|
|
for: .normal)
|
|
|
|
skinToneButton.showsMenuAsPrimaryAction = true
|
|
|
|
skinToneButton.menu = defaultSkinToneSelectionMenu
|
2021-02-02 04:47:48 +01:00
|
|
|
skinToneButton.accessibilityLabel =
|
|
|
|
NSLocalizedString("emoji.default-skin-tone-button.accessibility-label", comment: "")
|
2021-01-15 22:43:46 +01:00
|
|
|
|
2021-01-15 11:13:10 +01:00
|
|
|
view.addSubview(collectionView)
|
|
|
|
collectionView.translatesAutoresizingMaskIntoConstraints = false
|
|
|
|
collectionView.backgroundColor = .clear
|
|
|
|
collectionView.dataSource = dataSource
|
|
|
|
collectionView.delegate = self
|
2021-02-02 04:47:48 +01:00
|
|
|
collectionView.isAccessibilityElement = false
|
|
|
|
collectionView.shouldGroupAccessibilityChildren = true
|
2021-01-14 18:49:53 +01:00
|
|
|
|
|
|
|
NSLayoutConstraint.activate([
|
|
|
|
searchBar.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
searchBar.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
|
2021-01-15 22:43:46 +01:00
|
|
|
skinToneButton.leadingAnchor.constraint(equalTo: searchBar.trailingAnchor, constant: .defaultSpacing),
|
|
|
|
skinToneButton.topAnchor.constraint(equalTo: view.layoutMarginsGuide.topAnchor),
|
|
|
|
skinToneButton.trailingAnchor.constraint(equalTo: view.layoutMarginsGuide.trailingAnchor),
|
|
|
|
skinToneButton.bottomAnchor.constraint(equalTo: searchBar.bottomAnchor),
|
2021-01-15 11:13:10 +01:00
|
|
|
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor),
|
|
|
|
collectionView.topAnchor.constraint(equalTo: searchBar.bottomAnchor),
|
|
|
|
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor),
|
|
|
|
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor)
|
2021-01-14 18:49:53 +01:00
|
|
|
])
|
|
|
|
|
2021-01-15 11:13:10 +01:00
|
|
|
viewModel.$emoji
|
2021-01-15 22:43:46 +01:00
|
|
|
.sink { [weak self] in self?.dataSource.apply(
|
|
|
|
$0.snapshot(),
|
|
|
|
animatingDifferences: !UIAccessibility.isReduceMotionEnabled) }
|
2021-01-15 11:13:10 +01:00
|
|
|
.store(in: &cancellables)
|
|
|
|
|
|
|
|
if let currentKeyboardLanguageIdentifier = searchBar.textInputMode?.primaryLanguage {
|
|
|
|
viewModel.locale = Locale(identifier: currentKeyboardLanguageIdentifier)
|
|
|
|
}
|
|
|
|
|
|
|
|
NotificationCenter.default.publisher(for: UITextInputMode.currentInputModeDidChangeNotification)
|
|
|
|
.compactMap { [weak self] _ in self?.searchBar.textInputMode?.primaryLanguage }
|
|
|
|
.compactMap(Locale.init(identifier:))
|
|
|
|
.assign(to: \.locale, on: viewModel)
|
|
|
|
.store(in: &cancellables)
|
2021-01-14 18:49:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
override func viewDidAppear(_ animated: Bool) {
|
|
|
|
super.viewDidAppear(animated)
|
|
|
|
|
|
|
|
guard let containerView = popoverPresentationController?.containerView else { return }
|
|
|
|
|
|
|
|
// gets the popover presentation controller's built-in visual effect view to actually show
|
|
|
|
func setClear(view: UIView) {
|
|
|
|
view.backgroundColor = .clear
|
|
|
|
|
|
|
|
if view == self.view {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for view in view.subviews {
|
|
|
|
setClear(view: view)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
setClear(view: containerView)
|
|
|
|
}
|
2021-01-15 11:13:10 +01:00
|
|
|
|
|
|
|
override func viewDidDisappear(_ animated: Bool) {
|
|
|
|
super.viewDidDisappear(animated)
|
|
|
|
|
|
|
|
dismissAction()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
extension EmojiPickerViewController: UICollectionViewDelegate {
|
|
|
|
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
|
2021-01-15 22:43:46 +01:00
|
|
|
guard let item = dataSource.itemIdentifier(for: indexPath) else { return }
|
2021-01-15 11:13:10 +01:00
|
|
|
|
2021-01-15 22:43:46 +01:00
|
|
|
select(emoji: applyingDefaultSkinTone(emoji: item))
|
2021-01-16 01:58:10 +01:00
|
|
|
viewModel.updateUse(emoji: item)
|
2021-01-15 22:43:46 +01:00
|
|
|
}
|
2021-01-15 11:13:10 +01:00
|
|
|
|
2021-01-15 22:43:46 +01:00
|
|
|
func collectionView(_ collectionView: UICollectionView,
|
|
|
|
contextMenuConfigurationForItemAt indexPath: IndexPath,
|
|
|
|
point: CGPoint) -> UIContextMenuConfiguration? {
|
|
|
|
guard let item = dataSource.itemIdentifier(for: indexPath),
|
2021-01-16 01:58:10 +01:00
|
|
|
case let .system(emoji, inFrequentlyUsed) = item,
|
2021-01-15 22:43:46 +01:00
|
|
|
!emoji.skinToneVariations.isEmpty
|
|
|
|
else { return nil }
|
|
|
|
|
|
|
|
return UIContextMenuConfiguration(identifier: nil, previewProvider: nil) { _ in
|
|
|
|
UIMenu(children: ([emoji] + emoji.skinToneVariations).map { skinToneVariation in
|
|
|
|
UIAction(title: skinToneVariation.emoji) { [weak self] _ in
|
2021-01-16 01:58:10 +01:00
|
|
|
self?.select(emoji: .system(skinToneVariation, inFrequentlyUsed: inFrequentlyUsed))
|
|
|
|
self?.viewModel.updateUse(emoji: item)
|
2021-01-15 22:43:46 +01:00
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
2021-01-15 11:13:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private extension EmojiPickerViewController {
|
|
|
|
static let headerElementKind = "com.metabolist.metatext.emoji-picker.header"
|
2021-01-15 22:43:46 +01:00
|
|
|
|
|
|
|
func select(emoji: PickerEmoji) {
|
|
|
|
selectionAction(emoji)
|
|
|
|
|
|
|
|
UISelectionFeedbackGenerator().selectionChanged()
|
|
|
|
}
|
|
|
|
|
|
|
|
func reloadVisibleItems() {
|
|
|
|
var snapshot = dataSource.snapshot()
|
|
|
|
let visibleItems = collectionView.indexPathsForVisibleItems.compactMap(dataSource.itemIdentifier(for:))
|
|
|
|
|
|
|
|
snapshot.reloadItems(visibleItems)
|
|
|
|
dataSource.apply(snapshot)
|
|
|
|
}
|
|
|
|
|
|
|
|
func applyingDefaultSkinTone(emoji: PickerEmoji) -> PickerEmoji {
|
2021-01-16 01:58:10 +01:00
|
|
|
if case let .system(systemEmoji, inFrequentlyUsed) = emoji,
|
2021-01-26 01:06:35 +01:00
|
|
|
let defaultEmojiSkinTone = viewModel.identityContext.appPreferences.defaultEmojiSkinTone {
|
2021-01-16 01:58:10 +01:00
|
|
|
return .system(systemEmoji.applying(skinTone: defaultEmojiSkinTone), inFrequentlyUsed: inFrequentlyUsed)
|
2021-01-15 22:43:46 +01:00
|
|
|
} else {
|
|
|
|
return emoji
|
|
|
|
}
|
|
|
|
}
|
2021-01-14 18:49:53 +01:00
|
|
|
}
|