diff --git a/XIONControlPanel/Controllers/MainViewController.swift b/XIONControlPanel/Controllers/MainViewController.swift index b7c4fbc..1c5516c 100644 --- a/XIONControlPanel/Controllers/MainViewController.swift +++ b/XIONControlPanel/Controllers/MainViewController.swift @@ -130,6 +130,7 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate { _updateVisualization(true) for device in devices { + // WARNING: don't commit _server.toggleDevice(device, state: device.state, completion: { (error: NSError?) -> Void in }) } } diff --git a/XIONControlPanel/Controllers/SwitchesViewController.swift b/XIONControlPanel/Controllers/SwitchesViewController.swift index 30c2f3d..6d278d7 100644 --- a/XIONControlPanel/Controllers/SwitchesViewController.swift +++ b/XIONControlPanel/Controllers/SwitchesViewController.swift @@ -88,20 +88,13 @@ class SwitchesViewController: UIViewController, { didSet { + // sort devices by name + self.devices.sortInPlace({ (d1: WemoDevice, d2: WemoDevice) -> Bool in + return (d1.name.compare(d2.name) == .OrderedAscending) + }) + let hash = self.devices.reduce(0, combine: {$0 ^ $1.hashValue}) if (hash != _currentDevicesHash) { - // sort by name - self.devices.sortInPlace({ (d1: WemoDevice, d2: WemoDevice) -> Bool in - return (d1.name.compare(d2.name) == .OrderedAscending) - }) - - // replace "Dance Dance Revolution" names with "DDR" to save space - for device in self.devices { - if let range = device.name.rangeOfString("Dance Dance Revolution") { - device.name.replaceRange(range, with: "DDR") - } - } - let previousSet = NSOrderedSet(array: oldValue) let newSet = NSOrderedSet(array: self.devices) var insertedIndexPaths: [NSIndexPath] = [] @@ -174,11 +167,10 @@ class SwitchesViewController: UIViewController, let reuseID = SwitchesViewController.collectionViewDeviceSwitchCellReuseIdentifier let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! WemoDeviceCellView - let deviceIdx = indexPath.item - ActionCell.count - let device = self.devices[deviceIdx] - cell.device = device + let device = _deviceAtIndexPath(indexPath) + cell.deviceName = device.name cell.toggled = (device.state == .On) - cell.ordinal = deviceIdx + 1 + cell.ordinal = indexPath.item - ActionCell.count + 1 return cell } @@ -224,8 +216,7 @@ class SwitchesViewController: UIViewController, let cell = collectionView.cellForItemAtIndexPath(indexPath) as! WemoDeviceCellView cell.toggled = !cell.toggled - let deviceIdx = indexPath.item - ActionCell.count - let device = self.devices[deviceIdx] + let device = _deviceAtIndexPath(indexPath) device.state = (cell.toggled ? .On : .Off) self.delegate?.switchesViewControllerDidToggleDevices(self, devices: [device]) @@ -249,4 +240,13 @@ class SwitchesViewController: UIViewController, return true } } + + // MARK: Internal + + internal func _deviceAtIndexPath(indexPath: NSIndexPath) -> WemoDevice + { + let deviceIdx = indexPath.item - ActionCell.count + let device = self.devices[deviceIdx] + return device + } } diff --git a/XIONControlPanel/Controllers/WemoServer.swift b/XIONControlPanel/Controllers/WemoServer.swift index bb35763..db38c5f 100644 --- a/XIONControlPanel/Controllers/WemoServer.swift +++ b/XIONControlPanel/Controllers/WemoServer.swift @@ -93,6 +93,8 @@ class WemoServer { let request = NSMutableURLRequest(URL: url!) request.HTTPMethod = "POST" + print("Toggle: \(url!.absoluteString)") + let task = _urlSession.dataTaskWithRequest(request) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in var clientError: NSError? = nil diff --git a/XIONControlPanel/Views/WemoCellView.swift b/XIONControlPanel/Views/WemoCellView.swift index c608f40..89b7eef 100644 --- a/XIONControlPanel/Views/WemoCellView.swift +++ b/XIONControlPanel/Views/WemoCellView.swift @@ -54,7 +54,6 @@ public class WemoCellView: UICollectionViewCell { } public class WemoDeviceCellView: WemoCellView { - private var _device: WemoDevice? private var _ordinal: Int = 0 private var _nameLabel: UILabel = UILabel() private var _ordinalLabel: UILabel = UILabel() @@ -123,17 +122,11 @@ public class WemoDeviceCellView: WemoCellView { // MARK: API - var device: WemoDevice? + var deviceName: String = "" { - get + didSet { - return _device - } - - set(device) - { - _device = device - _nameLabel.text = _device?.name.uppercaseString + _nameLabel.text = self.deviceName.uppercaseString self.setNeedsLayout() } } @@ -153,16 +146,10 @@ public class WemoDeviceCellView: WemoCellView { } } - var toggled: Bool + var toggled: Bool = false { - get + didSet { - return (_device?.state == .On) - } - - set(toggled) - { - _device?.state = (toggled ? .On : .Off) _indicator.status = toggled if (toggled) {