-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHeroListViewController.swift
More file actions
168 lines (137 loc) · 6.67 KB
/
HeroListViewController.swift
File metadata and controls
168 lines (137 loc) · 6.67 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
import UIKit
class HeroListViewController: UIViewController, StoryboardInstantiable {
enum Constants {
static let navigationTitle = "Heroes"
static let storyboardName = "HeroList"
// collection view constants
static let minimumLineSpacing: CGFloat = 10
static let minimumInteritemSpacing: CGFloat = 10
static let footerHeight: CGFloat = 60
static let footerIdentifier = "HeroListFooterView"
}
static let storyboardName = Constants.storyboardName
@IBOutlet private weak var collectionView: UICollectionView!
private var loadingIndicator: UIActivityIndicatorView?
var interactor: HeroListOutput!
override func viewDidLoad() {
super.viewDidLoad()
setupCollectionView()
setupNavigationItem()
definesPresentationContext = true
interactor.viewDidLoad()
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
interactor.viewDidAppear()
}
private func setupNavigationItem() {
navigationItem.title = Constants.navigationTitle
navigationItem.searchController = UISearchController(searchResultsController: nil)
navigationItem.searchController?.searchBar.delegate = self
navigationItem.searchController?.delegate = self
}
private func setupCollectionView() {
let layout = UICollectionViewFlowLayout()
layout.minimumLineSpacing = Constants.minimumLineSpacing
layout.minimumInteritemSpacing = Constants.minimumInteritemSpacing
collectionView.collectionViewLayout = layout
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(CardCell.self)
}
}
extension HeroListViewController: HeroListInput {
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 HeroListViewController: UISearchBarDelegate, UISearchControllerDelegate {
func searchBarTextDidEndEditing(_ searchBar: UISearchBar) {
interactor.searchBarDidEndEditingWithText(searchBar.text)
}
func willDismissSearchController(_ searchController: UISearchController) {
interactor.willDismissSearch()
}
}
extension HeroListViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView,
didSelectItemAt indexPath: IndexPath) {
let hero = interactor.heroForIndex(indexPath.row)
let heroDetail = HeroDetailBuilder.build(with: hero)
navigationController?.pushViewController(heroDetail, animated: true)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: collectionView.frame.width, height: CardCell.preferredHeight)
}
}
extension HeroListViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView,
numberOfItemsInSection section: Int) -> Int {
return interactor.numberOfHeroes
}
func collectionView(_ collectionView: UICollectionView,
cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeue(CardCell.self, for: indexPath)
let hero = interactor.heroForIndex(indexPath.row)
cell.configure(for: hero) {
let isFavorite = AppEnvironment.current.favorites.isFavorite(hero.id)
let firstActionTitle = (isFavorite ? "Remove from" : "Add to") + " Favorites"
let infoIcon = UIButton(type: .infoLight)
infoIcon.tintColor = .gray
infoIcon.isUserInteractionEnabled = false
let actionSheet = ActionSheet.init(configuration: .init(header: .title(hero.name),
maxHeight: 500))
actionSheet.setActions([
DefaultActionView(title: firstActionTitle,
icon: .icon(Images.star,
size: ActionSheetConstants.iconSize,
color: isFavorite ? .orange : .gray),
sheetToDismiss: actionSheet,
onTap: {
AppEnvironment.current.favorites.toggle(with: hero.id)
}),
DefaultActionView(title: "See Details",
icon: .view(infoIcon),
sheetToDismiss: actionSheet,
onTap: { [weak self] in
guard let self = self else { return }
let heroDetail = HeroDetailBuilder.build(with: hero)
self.navigationController?.pushViewController(heroDetail, animated: true)
}),
])
actionSheet.present(in: self)
}
return cell
}
func collectionView(_ collectionView: UICollectionView,
willDisplay cell: UICollectionViewCell,
forItemAt indexPath: IndexPath) {
interactor.willDisplayCellAtIndex(indexPath.row)
}
func collectionView(_ collectionView: UICollectionView,
layout collectionViewLayout: UICollectionViewLayout,
referenceSizeForFooterInSection section: Int) -> CGSize {
return interactor.shouldShowFooter ?
CGSize(width: collectionView.frame.width, height: Constants.footerHeight):
.zero
}
func collectionView(_ collectionView: UICollectionView,
viewForSupplementaryElementOfKind kind: String,
at indexPath: IndexPath) -> UICollectionReusableView {
guard kind == UICollectionView.elementKindSectionFooter else { return UICollectionReusableView() }
return collectionView.dequeueReusableSupplementaryView(ofKind: kind,
withReuseIdentifier: Constants.footerIdentifier,
for: indexPath)
}
}