This repository was archived by the owner on Jun 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 437
Expand file tree
/
Copy pathEmojiPicker.swift
More file actions
362 lines (286 loc) · 13.5 KB
/
EmojiPicker.swift
File metadata and controls
362 lines (286 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
//
// EmojiPicker.swift
// Rocket.Chat
//
// Created by Matheus Cardoso on 12/20/17.
// Copyright © 2017 Rocket.Chat. All rights reserved.
//
import UIKit
private typealias EmojiCategory = (name: String, emojis: [Emoji])
final class EmojiPicker: UIView, RCEmojiKitLocalizable {
static let defaults = UserDefaults(suiteName: "EmojiPicker")
var customEmojis: [Emoji] = []
var customCategory: (name: String, emojis: [Emoji]) {
return (name: "custom", emojis: self.customEmojis)
}
var recentEmojis: [Emoji] {
get {
if let data = EmojiPicker.defaults?.value(forKey: "recentEmojis") as? Data {
let emojis = try? PropertyListDecoder().decode(Array<Emoji>.self, from: data)
return emojis ?? []
}
return []
}
set {
let emojis = newValue.count < 31 ? newValue : Array(newValue.dropLast(newValue.count - 30))
EmojiPicker.defaults?.set(try? PropertyListEncoder().encode(emojis), forKey: "recentEmojis")
}
}
var recentCategory: (name: String, emojis: [Emoji]) {
// remove invalid custom emojis
let recentEmojis = self.recentEmojis.filter {
guard case let .custom(imageUrl) = $0.type else { return true }
return customEmojis.contains(where: { $0.imageUrl == imageUrl })
}
return (name: "recent", emojis: recentEmojis)
}
fileprivate let defaultCategories: [EmojiCategory] = [
(name: "people", emojis: Emojione.people),
(name: "nature", emojis: Emojione.nature),
(name: "food", emojis: Emojione.food),
(name: "activity", emojis: Emojione.activity),
(name: "travel", emojis: Emojione.travel),
(name: "objects", emojis: Emojione.objects),
(name: "symbols", emojis: Emojione.symbols),
(name: "flags", emojis: Emojione.flags)
]
fileprivate var searchedCategories: [(name: String, emojis: [Emoji])] = []
fileprivate func searchCategories(string: String) -> [EmojiCategory] {
return ([customCategory] + defaultCategories).map {
let emojis = $0.emojis.filter {
$0.name.contains(string) || $0.shortname.contains(string) ||
$0.keywords.joined(separator: " ").contains(string) ||
$0.alternates.joined(separator: " ").contains(string)
}
return (name: $0.name, emojis: emojis)
}.filter { !$0.emojis.isEmpty }
}
var isSearching: Bool {
return searchBar?.text != nil && searchBar?.text?.isEmpty != true
}
fileprivate var currentCategories: [EmojiCategory] {
if isSearching { return searchedCategories }
let recent = (recentEmojis.count > 0 ? [recentCategory] : [])
let custom = (customEmojis.count > 0 ? [customCategory] : [])
return recent + custom + defaultCategories
}
@IBOutlet weak var contentView: UIView!
@IBOutlet weak var categoriesView: UITabBar!
@IBOutlet weak var searchBar: UISearchBar! {
didSet {
searchBar.placeholder = localized("searchbar.placeholder")
searchBar.delegate = self
}
}
@IBOutlet weak var emojisCollectionView: UICollectionView!
let skinTones: [(name: String?, color: UIColor)] = [
(name: nil, color: #colorLiteral(red: 0.999120295, green: 0.8114234805, blue: 0.06628075987, alpha: 1)),
(name: "tone1", color: #colorLiteral(red: 0.982526958, green: 0.8808286786, blue: 0.7670835853, alpha: 1)),
(name: "tone2", color: #colorLiteral(red: 0.8934452534, green: 0.7645885944, blue: 0.6247871518, alpha: 1)),
(name: "tone3", color: #colorLiteral(red: 0.7776196599, green: 0.6034522057, blue: 0.4516467452, alpha: 1)),
(name: "tone4", color: #colorLiteral(red: 0.6469842792, green: 0.4368215203, blue: 0.272474587, alpha: 1)),
(name: "tone5", color: #colorLiteral(red: 0.391161263, green: 0.3079459369, blue: 0.2550256848, alpha: 1))
]
var currentSkinToneIndex: Int {
get {
return EmojiPicker.defaults?.integer(forKey: "currentSkinToneIndex") ?? 0
}
set {
EmojiPicker.defaults?.set(newValue, forKey: "currentSkinToneIndex")
}
}
var currentSkinTone: (name: String?, color: UIColor) {
return skinTones[currentSkinToneIndex]
}
@IBOutlet weak var skinToneButton: UIButton! {
didSet {
skinToneButton.layer.cornerRadius = skinToneButton.frame.width/2
skinToneButton.backgroundColor = currentSkinTone.color
skinToneButton.showsTouchWhenHighlighted = true
skinToneButton.accessibilityLabel = localized("skinTone.label")
}
}
@IBAction func didPressSkinToneButton(_ sender: UIButton) {
currentSkinToneIndex += 1
currentSkinToneIndex = currentSkinToneIndex % skinTones.count
skinToneButton.backgroundColor = currentSkinTone.color
skinToneButton.accessibilityHint = currentSkinTone.name
emojisCollectionView.reloadData()
}
var emojiPicked: ((String) -> Void)?
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
private func commonInit() {
Bundle.main.loadNibNamed("EmojiPicker", owner: self, options: nil)
addSubview(contentView)
contentView.translatesAutoresizingMaskIntoConstraints = false
addConstraints(
NSLayoutConstraint.constraints(
withVisualFormat: "|-0-[view]-0-|", options: [], metrics: nil, views: ["view": contentView]
)
)
addConstraints(
NSLayoutConstraint.constraints(
withVisualFormat: "V:|-0-[view]-0-|", options: [], metrics: nil, views: ["view": contentView]
)
)
}
private func setupCollectionView() {
emojisCollectionView.dataSource = self
emojisCollectionView.delegate = self
emojisCollectionView.register(EmojiCollectionViewCell.self, forCellWithReuseIdentifier: "EmojiCollectionViewCell")
emojisCollectionView.register(EmojiPickerSectionHeaderView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "EmojiPickerSectionHeaderView")
if let layout = emojisCollectionView.collectionViewLayout as? UICollectionViewFlowLayout {
layout.sectionHeadersPinToVisibleBounds = true
layout.headerReferenceSize = CGSize(width: self.frame.width, height: 20)
}
emojisCollectionView.contentInset = UIEdgeInsets(top: 0, left: 8, bottom: 0, right: 8)
}
private func setupCategoriesView() {
let categoryItems = currentCategories.map { category -> UITabBarItem in
let image = UIImage(named: category.name) ?? UIImage(named: "custom")
let item = UITabBarItem(title: nil, image: image, selectedImage: image)
item.isAccessibilityElement = true
item.accessibilityLabel = category.name
item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: -6, right: 0)
return item
}
categoriesView.setItems(categoryItems, animated: false)
categoriesView.delegate = self
categoriesView.layoutIfNeeded()
}
override func didMoveToSuperview() {
super.didMoveToSuperview()
setupCategoriesView()
setupCollectionView()
}
override func layoutSubviews() {
super.layoutSubviews()
applyTheme()
}
}
extension EmojiPicker: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return currentCategories.count
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
guard let headerView = collectionView.dequeueReusableSupplementaryView(
ofKind: kind,
withReuseIdentifier: "EmojiPickerSectionHeaderView",
for: indexPath
) as? EmojiPickerSectionHeaderView else { return UICollectionReusableView() }
headerView.textLabel.text = localized("categories.\(currentCategories[indexPath.section].name)")
return headerView
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return currentCategories[section].emojis.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "EmojiCollectionViewCell", for: indexPath) as? EmojiCollectionViewCell else { return UICollectionViewCell() }
let emoji = currentCategories[indexPath.section].emojis[indexPath.row]
if let file = emoji.imageUrl {
cell.emoji = .custom(URL(string: file))
cell.emojiImageView.accessibilityLabel = emoji.name
cell.emojiImageView.accessibilityTraits = .staticText
} else {
var toneModifier = ""
if emoji.supportsTones, let currentTone = currentSkinTone.name { toneModifier = "_\(currentTone)" }
let searchString = String(emoji.shortname.dropFirst().dropLast()) + toneModifier
cell.emoji = .standard(Emojione.values[searchString])
}
return cell
}
}
extension EmojiPicker: UISearchBarDelegate {
func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
searchedCategories = searchCategories(string: searchText.lowercased())
emojisCollectionView.reloadData()
}
}
extension EmojiPicker: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 36.0, height: 36.0)
}
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool {
return true
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let emoji = currentCategories[indexPath.section].emojis[indexPath.row]
if emoji.supportsTones, let currentTone = currentSkinTone.name {
let shortname = String(emoji.shortname.dropLast()) + "_\(currentTone):"
emojiPicked?(shortname)
} else {
emojiPicked?(emoji.shortname)
}
if let index = recentEmojis.index(where: { $0.shortname == emoji.shortname }) {
recentEmojis.remove(at: index)
}
recentEmojis = [emoji] + recentEmojis
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: collectionView.bounds.width, height: 36.0)
}
func collectionView(_ collectionView: UICollectionView, willDisplay cell: UICollectionViewCell, forItemAt indexPath: IndexPath) {
if let first = collectionView.indexPathsForVisibleItems.first {
categoriesView.selectedItem = categoriesView.items?[first.section]
}
}
}
extension EmojiPicker: UITabBarDelegate {
func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
guard let index = tabBar.items?.index(of: item) else { return }
searchBar.resignFirstResponder()
searchBar.text = ""
emojisCollectionView.reloadData()
emojisCollectionView.layoutIfNeeded()
let numberOfCells = emojisCollectionView.numberOfItems(inSection: index)
let indexPath = IndexPath(row: 1, section: index)
if numberOfCells > 0 {
emojisCollectionView.scrollToItem(at: indexPath, at: .top, animated: false)
emojisCollectionView.setContentOffset(emojisCollectionView.contentOffset.applying(CGAffineTransform(translationX: 0.0, y: -36.0)), animated: false)
}
}
}
private class EmojiPickerSectionHeaderView: UICollectionReusableView {
var textLabel: UILabel
let screenWidth = UIScreen.main.bounds.width
override init(frame: CGRect) {
textLabel = UILabel()
super.init(frame: frame)
addSubview(textLabel)
textLabel.font = .boldSystemFont(ofSize: UIFont.systemFontSize)
textLabel.textColor = .gray
textLabel.textAlignment = .left
textLabel.numberOfLines = 0
textLabel.lineBreakMode = .byWordWrapping
textLabel.translatesAutoresizingMaskIntoConstraints = false
NSLayoutConstraint.activate([
NSLayoutConstraint(item: textLabel, attribute: .leading, relatedBy: .equal,
toItem: self, attribute: .leadingMargin,
multiplier: 1.0, constant: -8.0),
NSLayoutConstraint(item: textLabel, attribute: .trailing, relatedBy: .equal,
toItem: self, attribute: .trailingMargin,
multiplier: 1.0, constant: 0.0),
NSLayoutConstraint(item: textLabel, attribute: .height, relatedBy: .equal,
toItem: nil, attribute: .height,
multiplier: 1.0, constant: 36.0)
])
backgroundColor = .white
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
// MARK: Themeable
extension EmojiPicker {
override func applyTheme() {
super.applyTheme()
skinToneButton.backgroundColor = currentSkinTone.color
}
}