-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeroDetailViewController.swift
More file actions
153 lines (125 loc) · 5.53 KB
/
HeroDetailViewController.swift
File metadata and controls
153 lines (125 loc) · 5.53 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
import UIKit
import SnapKit
import LazyTransitions
class HeroDetailViewController: UIViewController, StoryboardInstantiable {
enum Constants {
static let storyboardName = "HeroDetail"
static let iconSize: CGFloat = 24
// collection view constants
static let minimumLineSpacing: CGFloat = 10
static let minimumInteritemSpacing: CGFloat = 10
static let sectionHeaderHeight: CGFloat = 50
}
static let storyboardName = Constants.storyboardName
var interactor: HeroDetailOutput!
@IBOutlet private weak var collectionView: UICollectionView!
private var loadingIndicator: UIActivityIndicatorView?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.largeTitleDisplayMode = .never
becomeLazy(for: .pop)
setupCollectionView()
setupBarButtonItem()
interactor.viewDidLoad()
}
private func setupBarButtonItem() {
let image = Images.star.withRenderingMode(.alwaysTemplate)
let button = UIButton(type: .custom)
button.setImage(image, for: .normal)
button.addTarget(self, action: #selector(favoriteButtonTapped), for: .touchUpInside)
button.snp.makeConstraints { make in
make.width.height.equalTo(Constants.iconSize)
}
navigationItem.rightBarButtonItem = .init(customView: button)
}
private func setupCollectionView() {
collectionView.register(CardCell.self)
collectionView.register(PosterCell.self)
collectionView.delegate = self
collectionView.dataSource = self
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = Constants.minimumLineSpacing
layout.minimumInteritemSpacing = Constants.minimumInteritemSpacing
collectionView.collectionViewLayout = layout
}
@objc private func favoriteButtonTapped() {
interactor.favoriteButtonTapped()
}
}
extension HeroDetailViewController: HeroDetailInput {
func favorite() {
navigationItem.rightBarButtonItem?.customView?.tintColor = .orange
}
func unfavorite() {
navigationItem.rightBarButtonItem?.customView?.tintColor = .gray
}
func reloadData() {
collectionView.reloadData()
}
func showLoading() {
guard loadingIndicator == nil else { return }
let indicator = UIActivityIndicatorView(style: .gray)
loadingIndicator = indicator
collectionView.addToCenter(indicator)
indicator.startAnimating()
}
func hideLoading() {
loadingIndicator?.removeFromSuperview()
loadingIndicator = nil
}
}
extension HeroDetailViewController: UICollectionViewDataSource {
func numberOfSections(in collectionView: UICollectionView) -> Int {
return interactor.numberOfSections
}
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return interactor.numberOfItemsInSection(section)
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
switch interactor.cellTypeForSection(indexPath.section) {
case .poster: return posterCell(collectionView, cellForItemAt: indexPath)
case .card: return cardCell(collectionView, cellForItemAt: indexPath)
}
}
private func posterCell(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> PosterCell {
let cell = collectionView.dequeue(PosterCell.self, for: indexPath)
cell.configure(with: interactor.hero)
return cell
}
private func cardCell(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> CardCell {
let cell = collectionView.dequeue(CardCell.self, for: indexPath)
cell.configure(for: interactor.product(at: indexPath))
return cell
}
}
extension HeroDetailViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForHeaderInSection section: Int) -> CGSize {
return section > 0 ?
.init(width: collectionView.frame.width, height: Constants.sectionHeaderHeight):
.zero
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return indexPath.section == 0 ?
.init(width: collectionView.frame.width, height: PosterCell.preferredHeight):
.init(width: collectionView.frame.width, height: CardCell.preferredHeight)
}
func collectionView(_ collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String,
at indexPath: IndexPath) -> UICollectionReusableView {
guard kind == UICollectionView.elementKindSectionHeader,
indexPath.section != 0 else { return UICollectionReusableView() }
let header = collectionView.dequeueSupplementaryView(HeroDetailSectionHeader.self,
for: indexPath,
kind: UICollectionView.elementKindSectionHeader)
header.titleLabel.text = interactor.titleForSection(indexPath.section)
return header
}
}