forked from tvidenov/appstore-clone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFeaturedAppsController.swift
109 lines (72 loc) · 3.83 KB
/
FeaturedAppsController.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
//
// FeaturedAppsController.swift
// AppStore
//
// Created by Tihomir Videnov on 11/6/16.
// Copyright © 2016 Tihomir Videnov. 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"
//appCategories = AppCategory.sampleAppCategories()
ApiService.sharedInstance.fetchFeaturedApps(completionHandler: { (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)
}
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.featuredAppsController = self
return cell
}
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: cellId, for: indexPath) as! CategoryCell
cell.appCategory = appCategories?[indexPath.item]
cell.featuredAppsController = self
return cell
}
override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
if let count = appCategories?.count {
return count
}
return 0
}
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)
}
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return CGSize(width: view.frame.width, height: 120)
}
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
}
// override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
// let appDetailController = UIViewController()
// navigationController?.pushViewController(appDetailController, animated: true)
// print("selected")
// }
func showAppDetailFor(app: App) {
let layout = UICollectionViewFlowLayout()
let appDetailController = AppDetailViewController(collectionViewLayout: layout)
appDetailController.app = app
navigationController?.pushViewController(appDetailController, animated: true)
}
}