-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChooseDeviceViewController.swift
More file actions
192 lines (138 loc) · 6.31 KB
/
ChooseDeviceViewController.swift
File metadata and controls
192 lines (138 loc) · 6.31 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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// ChooseDeviceViewController.swift
// Bourbon-iOS
//
// Created by Alyssa Torres on 3/13/17.
// Copyright © 2017 Ourglass. All rights reserved.
//
import UIKit
import PKHUD
import PromiseKit
import SwiftyJSON
class ChooseDeviceViewController : UIViewController, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
@IBOutlet weak var venueNameLabel: UILabel!
@IBOutlet weak var deviceCollection: UICollectionView!
let SEARCHING_TIMEOUT_INTERVAL = 7.0
var refreshControl : UIRefreshControl!
var refreshing = true
var devices = [OGDevice]()
var venue: OGVenue!
override var preferredStatusBarStyle : UIStatusBarStyle {
return .lightContent
}
override func viewDidLoad() {
super.viewDidLoad()
self.venueNameLabel.text = venue.name
// Setup collection view
self.deviceCollection.dataSource = self
self.deviceCollection.delegate = self
self.deviceCollection.allowsMultipleSelection = false
// Setup refresh control and add
self.refreshControl = UIRefreshControl()
self.refreshControl.addTarget(self, action: #selector(findDevices), for: UIControlEvents.valueChanged)
self.deviceCollection.addSubview(self.refreshControl)
self.deviceCollection.alwaysBounceVertical = true
self.navigationItem.backBarButtonItem = UIBarButtonItem(title:"", style:.plain, target:nil, action:nil)
setNeedsStatusBarAppearanceUpdate()
}
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
navigationItem.title = "Devices"
self.findDevices()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func findDevices() {
self.refreshing = true
self.refreshControl.beginRefreshing()
OGCloud.sharedInstance.getDevices(self.venue.uuid)
.then{ json -> Void in
self.processDevices(json)
self.stopRefresh()
}
.catch{ err -> Void in
self.stopRefresh()
}
// Stops the spinner if we have seen no venues
Timer.scheduledTimer(timeInterval: SEARCHING_TIMEOUT_INTERVAL, target: self, selector: #selector(stopRefresh), userInfo: nil, repeats: false)
}
func processDevices(_ inboundDeviceJson: JSON) {
guard let devicesArray = inboundDeviceJson.array else {
log.debug("No devices found!")
return
}
self.devices = [OGDevice]()
for device in devicesArray {
// let name = device["name"].stringValue
// let udid = device["deviceUDID"].stringValue
// let venueUUID = device["atVenueUUID"].stringValue
self.devices.append(OGDevice(inboundJson: device))
}
}
func stopRefresh() {
self.refreshing = false
self.refreshControl.endRefreshing()
self.sortAndReload()
}
func sortAndReload() {
/*if self.devices.count > 1 {
self.devices.sort {
(a : OGDevice, b : OGDevice) -> Bool in
let comp = a.ipAddress.compare(b.ipAddress, options: NSString.CompareOptions.numeric)
if comp == ComparisonResult.orderedAscending {
return true
} else {
return false
}
}
}*/
self.deviceCollection.reloadData()
}
// MARK: - UICollectionViewDelegate
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return self.devices.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell : DeviceCell = collectionView.dequeueReusableCell(withReuseIdentifier: "DefaultDeviceCell", for: indexPath) as! DeviceCell
let thisDevice = self.devices[indexPath.row]
cell.name.text = thisDevice.name
cell.name.textColor = thisDevice.isActive ? .white : Style.ogRedColor
if thisDevice.isActive {
cell.statusLabel.text = thisDevice.stationName
//cell.statusLabel.isHidden = thisDevice.stationName.isEmpty
} else {
cell.statusLabel.text = "OFFLINE"
//cell.statusLabel.isHidden = false
}
cell.numberLabel.text = String(format: "%02d", indexPath.row + 1)
return cell
}
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
self.performSegue(withIdentifier: "toDeviceControl", sender: indexPath)
}
// Set header view height low if we're not showing the error message
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
return self.devices.count < 1 && !self.refreshing ? CGSize(width: 330, height: 100) : CGSize(width: 330, height: 20)
}
func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
let headerView = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "headerView", for: indexPath)
if self.devices.count < 1 && !self.refreshing {
headerView.isHidden = false
} else {
headerView.isHidden = true
}
return headerView
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
self.refreshControl.endRefreshing()
if segue.identifier == "toDeviceControl" && sender != nil {
let indexPath: IndexPath = sender as! IndexPath
let device = self.devices[indexPath.row]
let dvc : DeviceViewController = segue.destination as! DeviceViewController
dvc.ogDevice = device
}
}
}