2021-03-25 08:56:17 +01:00
|
|
|
//
|
|
|
|
// CustomEmojiPickerSection.swift
|
|
|
|
// Mastodon
|
|
|
|
//
|
|
|
|
// Created by MainasuK Cirno on 2021-3-24.
|
|
|
|
//
|
|
|
|
|
|
|
|
import UIKit
|
2021-06-29 10:41:58 +02:00
|
|
|
import Nuke
|
2021-03-25 08:56:17 +01:00
|
|
|
|
|
|
|
enum CustomEmojiPickerSection: Equatable, Hashable {
|
|
|
|
case emoji(name: String)
|
|
|
|
}
|
|
|
|
|
|
|
|
extension CustomEmojiPickerSection {
|
|
|
|
static func collectionViewDiffableDataSource(
|
|
|
|
for collectionView: UICollectionView,
|
|
|
|
dependency: NeedsDependency
|
|
|
|
) -> UICollectionViewDiffableDataSource<CustomEmojiPickerSection, CustomEmojiPickerItem> {
|
2021-04-09 05:05:10 +02:00
|
|
|
let dataSource = UICollectionViewDiffableDataSource<CustomEmojiPickerSection, CustomEmojiPickerItem>(collectionView: collectionView) { [weak dependency] collectionView, indexPath, item -> UICollectionViewCell? in
|
|
|
|
guard let _ = dependency else { return nil }
|
2021-03-25 08:56:17 +01:00
|
|
|
switch item {
|
|
|
|
case .emoji(let attribute):
|
|
|
|
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: String(describing: CustomEmojiPickerItemCollectionViewCell.self), for: indexPath) as! CustomEmojiPickerItemCollectionViewCell
|
|
|
|
let placeholder = UIImage.placeholder(size: CustomEmojiPickerItemCollectionViewCell.itemSize, color: .systemFill)
|
|
|
|
.af.imageRounded(withCornerRadius: 4)
|
2021-06-29 10:41:58 +02:00
|
|
|
cell.imageTask = Nuke.loadImage(
|
|
|
|
with: attribute.emoji.url,
|
|
|
|
options: .init(
|
|
|
|
placeholder: placeholder,
|
|
|
|
transition: .fadeIn(duration: 0.2)
|
|
|
|
),
|
|
|
|
into: cell.emojiImageView
|
2021-03-25 08:56:17 +01:00
|
|
|
)
|
2021-05-13 08:27:57 +02:00
|
|
|
cell.accessibilityLabel = attribute.emoji.shortcode
|
2021-03-25 08:56:17 +01:00
|
|
|
return cell
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
dataSource.supplementaryViewProvider = { [weak dataSource] collectionView, kind, indexPath -> UICollectionReusableView? in
|
|
|
|
guard let dataSource = dataSource else { return nil }
|
|
|
|
let sections = dataSource.snapshot().sectionIdentifiers
|
|
|
|
guard indexPath.section < sections.count else { return nil }
|
|
|
|
let section = sections[indexPath.section]
|
|
|
|
|
|
|
|
switch kind {
|
|
|
|
case String(describing: CustomEmojiPickerHeaderCollectionReusableView.self):
|
|
|
|
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: String(describing: CustomEmojiPickerHeaderCollectionReusableView.self), for: indexPath) as! CustomEmojiPickerHeaderCollectionReusableView
|
|
|
|
switch section {
|
|
|
|
case .emoji(let name):
|
2021-06-29 10:41:58 +02:00
|
|
|
header.titleLabel.text = name
|
2021-03-25 08:56:17 +01:00
|
|
|
}
|
|
|
|
return header
|
|
|
|
default:
|
|
|
|
assertionFailure()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dataSource
|
|
|
|
}
|
|
|
|
}
|