This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathReactionsMenuViewController.swift
121 lines (97 loc) · 3.83 KB
/
ReactionsMenuViewController.swift
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
//
// ReactionsMenuViewController.swift
// Freetime
//
// Created by Ryan Nystrom on 8/12/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//
import UIKit
final class EmojiCell: UICollectionViewCell {
let label = UILabel()
override init(frame: CGRect) {
super.init(frame: frame)
label.textAlignment = .center
label.backgroundColor = .clear
label.font = UIFont.systemFont(ofSize: Styles.Text.body.size + 4)
contentView.addSubview(label)
selectedBackgroundView = UIView()
selectedBackgroundView?.backgroundColor = Styles.Colors.Gray.medium.color
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func layoutSubviews() {
super.layoutSubviews()
label.frame = contentView.bounds
}
}
private extension Array {
func split(toNumberOfChunks number: Int) -> [[Element]] {
let size = count / number
return (0 ..< number).map {
let from = $0 * size
let to = from + size
let isLast = ($0 == number - 1)
return Array(self[from ..< (isLast ? count : to)])
}
}
}
final class ReactionsMenuViewController: UICollectionViewController,
UICollectionViewDelegateFlowLayout {
private let reuseIdentifier = "cell"
private let size: CGFloat = 50
private let sectionedReactions = IssueCommentReactionViewModel.allReactions.split(toNumberOfChunks: 2)
var selectedReaction: ReactionContent? {
guard let item = collectionView?.indexPathsForSelectedItems?.first else { return nil }
return sectionedReactions[item.section][item.item]
}
init() {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = 0
layout.minimumInteritemSpacing = 0
super.init(collectionViewLayout: layout)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func viewDidLoad() {
super.viewDidLoad()
collectionView?.backgroundColor = Styles.Colors.menuBackgroundColor.color
collectionView?.register(EmojiCell.self, forCellWithReuseIdentifier: reuseIdentifier)
collectionView?.reloadData()
collectionView?.layoutIfNeeded()
guard let reactionsInRow = sectionedReactions.first?.count else {
assertionFailure("Reactions in grid row must be >0")
return
}
preferredContentSize = CGSize(
width: size * CGFloat(reactionsInRow),
height: size * CGFloat(sectionedReactions.count)
)
}
// MARK: UICollectionViewDataSource
override func numberOfSections(in collectionView: UICollectionView) -> Int {
return sectionedReactions.count
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return sectionedReactions[section].count
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseIdentifier, for: indexPath)
if let cell = cell as? EmojiCell {
cell.label.text = sectionedReactions[indexPath.section][indexPath.item].emoji
}
return cell
}
// MARK: UICollectionViewDelegate
override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
dismiss(animated: true)
}
// MARK: UICollectionViewDelegateFlowLayout
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(
width: size,
height: size
)
}
}