-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFeaturedAppsController.swift
More file actions
175 lines (122 loc) · 7.03 KB
/
FeaturedAppsController.swift
File metadata and controls
175 lines (122 loc) · 7.03 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
//
// ViewController.swift
// AppStore1
//
// Created by Tiago Bastos on 03/08/2017.
// Copyright © 2017 Tiago Bastos. All rights reserved.
//
import UIKit
class FeaturedAppsController: UICollectionViewController, UICollectionViewDelegateFlowLayout {
private let cellId = "CellId"
private let largeCellId = "LargeCellId"
private let headerId = "headerId"
var featuredApps: FeaturedApps?
var appCategories: [AppCategory]?
override func viewDidLoad() {
super.viewDidLoad()
navigationItem.title = "Featured Apps"
AppCategory.fetchFeaturedApps { (featuredApps) -> () in
self.featuredApps = featuredApps
self.appCategories = featuredApps.appCategories
self.collectionView?.reloadData()
}
collectionView?.backgroundColor = .white
collectionView?.register(CategoryCell.self , forCellWithReuseIdentifier: cellId)
collectionView?.register(LargeCategoryCell.self , forCellWithReuseIdentifier: largeCellId)
collectionView?.register(Header.self, forSupplementaryViewOfKind: UICollectionElementKindSectionHeader, withReuseIdentifier: headerId)
}
func showAppDetailForApp(app: App){
let layout = UICollectionViewFlowLayout()
let appDetailController = AppDetailController(collectionViewLayout: layout)
appDetailController.app = app
navigationController?.pushViewController(appDetailController, animated: true)
}
override func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind , withReuseIdentifier: headerId, for: indexPath) as! Header
header.appCategory = featuredApps?.bannerCategory
return header
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 120)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
if indexPath.item == 2 {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: largeCellId, for: indexPath) as! LargeCategoryCell
cell.appCategory = appCategories?[indexPath.item]
cell.featureAppsController = self
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId , for: indexPath) as! CategoryCell
cell.featureAppsController = self
cell.appCategory = appCategories?[indexPath.item]
return cell
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
if indexPath.item == 2 {
return CGSize(width: view.frame.width, height: 160)
}
return CGSize(width: view.frame.width, height: 230)
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = appCategories?.count {
return count
}
return 0
}
}
class Header: CategoryCell {
let cellId = "bannerCellId"
override func setupView() {
appsCollectionView.dataSource = self
appsCollectionView.delegate = self
appsCollectionView.register(BannerCell.self, forCellWithReuseIdentifier: cellId)
addSubview(appsCollectionView)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": appsCollectionView]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": appsCollectionView]))
}
override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsetsMake(0, 0, 0, 0)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! AppCell
cell.app = appCategory?.apps?[indexPath.item]
return cell
}
override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: frame.width / 2 + 50, height: frame.height)
}
fileprivate class BannerCell: AppCell {
fileprivate override func setupViews() {
imageView.layer.borderColor = UIColor(white: 0.5, alpha: 0.5).cgColor
imageView.layer.borderWidth = 0.5
imageView.layer.cornerRadius = 0
imageView.translatesAutoresizingMaskIntoConstraints = false
addSubview(imageView)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": imageView]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": imageView]))
}
}
}
class LargeCategoryCell: CategoryCell {
private let largeAppCellId = "largeAppCellId"
override func setupView() {
super.setupView()
appsCollectionView.register(LargeAppCell.self , forCellWithReuseIdentifier: largeAppCellId)
}
override func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: 200, height: frame.height - 32)
}
override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: largeAppCellId, for: indexPath) as! AppCell
cell.app = appCategory?.apps?[indexPath.item]
return cell
}
fileprivate class LargeAppCell: AppCell {
fileprivate override func setupViews() {
imageView.translatesAutoresizingMaskIntoConstraints = false
addSubview(imageView)
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "H:|[v0]|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": imageView]))
addConstraints(NSLayoutConstraint.constraints(withVisualFormat: "V:|-2-[v0]-14-|", options: NSLayoutFormatOptions(), metrics: nil, views: ["v0": imageView]))
}
}
}