forked from asuc-octo/berkeley-mobile-ios
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResourceGroupCell.swift
More file actions
121 lines (95 loc) · 4.1 KB
/
ResourceGroupCell.swift
File metadata and controls
121 lines (95 loc) · 4.1 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
import UIKit
import Firebase
fileprivate let kResourceTileID = "ResourceTile"
/**
*
*/
class ResourceGroupCell: UITableViewCell, RequiresData, UICollectionViewDataSource, UICollectionViewDelegate
{
typealias SelectionHandler = (Resource) -> Void
// Data
private var resources: [Resource]!
private var selectionHandler: SelectionHandler?
// UI
@IBOutlet private weak var groupLabel: UILabel!
@IBOutlet private weak var collectionView: UICollectionView!
/// Configure the collectionView.
override func awakeFromNib()
{
self.collectionView.delegate = self
self.collectionView.dataSource = self
self.collectionView.showsVerticalScrollIndicator = false
self.collectionView.showsHorizontalScrollIndicator = false
}
// ========================================
// MARK: - RequiresData
// ========================================
typealias DataType = (name: String, resources: [Resource], handler: SelectionHandler?)
/**
* Set the name, dining halls, and selection callback handler.
* The carousel collectionView is reloaded.
*/
public func setData(_ data: DataType)
{
self.resources = data.resources
self.groupLabel.text = data.name
self.groupLabel.font = UIFont.systemFont(ofSize: 20, weight: UIFontWeightBold)
self.selectionHandler = data.handler
self.collectionView.reloadData()
}
// ========================================
// MARK: - UICollectionViewDataSource
// ========================================
/// Carousel only has 1 section.
func numberOfSections(in collectionView: UICollectionView) -> Int
{
return 1
}
/// Return the number of tiles in the group.
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
var count = 0
if (groupLabel.text == "Gym Classes") {
for res in resources {
if ((res as! GymClass).start_time == nil) {
count += 1
}
}
return count
}
if let r = resources {
return resources.count
} else {
return 0
}
}
/// Get a ResourceTile and pass it the corresponding Resource.
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
{
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: kResourceTileID, for: indexPath) as! ResourceTile
cell.setData( self.resources[indexPath.item] )
return cell
}
// ========================================
// MARK: - UICollectionViewDelegate
// ========================================
/// When a ResourceTile is selected (tapped), call the selectionHandler with the Resource.
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
{
let some_type = type(of: self.resources[indexPath.item])
if (some_type == berkeleyMobileiOS.Library.self) {
Analytics.logEvent("opened_library", parameters: ["library" : self.resources[indexPath.item].name])
} else if (some_type == berkeleyMobileiOS.CampusResource.self) {
Analytics.logEvent("opened_resource", parameters: ["resource" : self.resources[indexPath.item].name])
} else if (some_type == berkeleyMobileiOS.DiningHall.self) {
// Analytics.logEvent("opened_food", parameters: nil)
} else if (some_type == berkeleyMobileiOS.Gym.self) {
Analytics.logEvent("opened_gym", parameters: ["gym" : self.resources[indexPath.item].name])
} else if (some_type == berkeleyMobileiOS.GymClass.self) {
Analytics.logEvent("opened_gym_class", parameters: ["gym class category" : self.resources[indexPath.item].name])
} else {
print("Error")
}
self.selectionHandler?( self.resources[indexPath.item] )
}
}