-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEventsViewController.swift
More file actions
105 lines (89 loc) · 3.74 KB
/
EventsViewController.swift
File metadata and controls
105 lines (89 loc) · 3.74 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
//
// EventsViewController.swift
// Where2Help
//
// Created by Aaron Cruz on 4/7/16.
// Copyright © 2016 Where2Help. All rights reserved.
//
import UIKit
class EventsViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, EventsHandler, UICollectionViewDelegateFlowLayout {
let kEventCellNibName = "EventCollectionViewCell"
let kEventCellID = "EventCell"
let kNoEventCellID = "NoEventCell"
let kShiftDetailControllerID = "shiftDetailViewController"
let interactor = EventsInteractor()
let refreshControl = UIRefreshControl()
var detailViewController: ShiftDetailViewController!
@IBOutlet weak var collectionView: UICollectionView!
override func viewDidLoad() {
super.viewDidLoad()
setupEventCell()
setupDetailController()
refreshControl.addTarget(self, action: #selector(EventsViewController.refresh), forControlEvents: UIControlEvents.ValueChanged)
collectionView.addSubview(refreshControl)
// let flowLayout = collectionView.collectionViewLayout as! UICollectionViewFlowLayout
// let w = view.frame.width - 30
// flowLayout.itemSize =
}
func refresh() {
TopNotification.showLoading(self, message: "Loading events...")
interactor.setup(handler: self)
refreshControl.endRefreshing()
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
refresh()
}
private func setupEventCell() {
let nib = UINib(nibName: kEventCellNibName, bundle: nil)
collectionView.registerNib(nib, forCellWithReuseIdentifier: kEventCellID)
collectionView.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: kNoEventCellID)
}
private func setupDetailController() {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
detailViewController = storyboard
.instantiateViewControllerWithIdentifier(kShiftDetailControllerID) as! ShiftDetailViewController
}
// Mark - handlers
func handleEventsUpdate() {
print("Events updated")
collectionView.reloadData()
TopNotification.hideNotifications(self)
}
// MARK - TableViewDelegate/DataSource
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
if interactor.eventCount() > 0 {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kEventCellID, forIndexPath: indexPath) as! EventCollectionViewCell
let event = interactor.eventFor(indexPath)
cell.setEvent(event)
cell.navigationController = navigationController
cell.detailViewController = detailViewController
return cell
}
else {
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(kNoEventCellID, forIndexPath: indexPath)
let title = UILabel(frame: CGRectMake(0, 0, cell.bounds.size.width, 80))
title.text = "Nothing to see here!"
title.textColor = Theme.darkGray()
title.font = UIFont.systemFontOfSize(24)
title.textAlignment = NSTextAlignment.Center
cell.contentView.addSubview(title)
return cell
}
}
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return interactor.eventCount() == 0 ? 1 : interactor.eventCount()
}
func collectionView(collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize {
if interactor.eventCount() > 0 {
let event = interactor.eventFor(indexPath)
let multiplier = event.shifts.count
let h = CGFloat(80 + (multiplier * 50))
let w = view.frame.width - 30
return CGSize(width: w, height: h)
}
else {
return CGSize(width: view.frame.width - 30, height: view.frame.height)
}
}
}