diff --git a/XIONControlPanel.xcodeproj/project.pbxproj b/XIONControlPanel.xcodeproj/project.pbxproj index 1da4f33..14a174f 100644 --- a/XIONControlPanel.xcodeproj/project.pbxproj +++ b/XIONControlPanel.xcodeproj/project.pbxproj @@ -19,7 +19,7 @@ 0C41EF541C34BC9800AF847F /* XIONLogoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C41EF531C34BC9800AF847F /* XIONLogoView.swift */; }; 0C41EF561C35ABCD00AF847F /* ConnectionStatusView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C41EF551C35ABCD00AF847F /* ConnectionStatusView.swift */; }; 0C4315001C35B1E700164559 /* WemoDevice.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C4314FF1C35B1E700164559 /* WemoDevice.swift */; }; - 0C4315061C35E67600164559 /* WemoDeviceCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C4315051C35E67600164559 /* WemoDeviceCellView.swift */; }; + 0C4315061C35E67600164559 /* WemoCellView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0C4315051C35E67600164559 /* WemoCellView.swift */; }; 0C63A8731C337381007E4B52 /* CubletsTechnique.plist in Resources */ = {isa = PBXBuildFile; fileRef = 0C63A8721C337381007E4B52 /* CubletsTechnique.plist */; }; 0C63A8761C3373D1007E4B52 /* HorizontalBlur.vsh in Resources */ = {isa = PBXBuildFile; fileRef = 0C63A8751C3373D1007E4B52 /* HorizontalBlur.vsh */; }; 0C63A8781C3373D9007E4B52 /* HorizontalBlur.fsh in Resources */ = {isa = PBXBuildFile; fileRef = 0C63A8771C3373D9007E4B52 /* HorizontalBlur.fsh */; }; @@ -49,7 +49,7 @@ 0C41EF531C34BC9800AF847F /* XIONLogoView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = XIONLogoView.swift; sourceTree = ""; }; 0C41EF551C35ABCD00AF847F /* ConnectionStatusView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConnectionStatusView.swift; sourceTree = ""; }; 0C4314FF1C35B1E700164559 /* WemoDevice.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WemoDevice.swift; sourceTree = ""; }; - 0C4315051C35E67600164559 /* WemoDeviceCellView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WemoDeviceCellView.swift; sourceTree = ""; }; + 0C4315051C35E67600164559 /* WemoCellView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = WemoCellView.swift; sourceTree = ""; }; 0C63A8721C337381007E4B52 /* CubletsTechnique.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = CubletsTechnique.plist; sourceTree = ""; }; 0C63A8751C3373D1007E4B52 /* HorizontalBlur.vsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = HorizontalBlur.vsh; sourceTree = ""; }; 0C63A8771C3373D9007E4B52 /* HorizontalBlur.fsh */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.glsl; path = HorizontalBlur.fsh; sourceTree = ""; }; @@ -121,7 +121,7 @@ 0C41EF4F1C34B97700AF847F /* HeaderView.swift */, 0CF9A48D1C35E9C800276F13 /* SwitchIndicatorView.swift */, 0C41EF531C34BC9800AF847F /* XIONLogoView.swift */, - 0C4315051C35E67600164559 /* WemoDeviceCellView.swift */, + 0C4315051C35E67600164559 /* WemoCellView.swift */, ); path = Views; sourceTree = ""; @@ -290,7 +290,7 @@ 0C20E2BB1C33529700BA789B /* MainViewController.swift in Sources */, 0C41EF501C34B97700AF847F /* HeaderView.swift in Sources */, 0C41EF541C34BC9800AF847F /* XIONLogoView.swift in Sources */, - 0C4315061C35E67600164559 /* WemoDeviceCellView.swift in Sources */, + 0C4315061C35E67600164559 /* WemoCellView.swift in Sources */, 0CF9A48E1C35E9C800276F13 /* SwitchIndicatorView.swift in Sources */, 0C4315001C35B1E700164559 /* WemoDevice.swift in Sources */, ); diff --git a/XIONControlPanel/Controllers/MainViewController.swift b/XIONControlPanel/Controllers/MainViewController.swift index 2c7a836..1bad214 100644 --- a/XIONControlPanel/Controllers/MainViewController.swift +++ b/XIONControlPanel/Controllers/MainViewController.swift @@ -103,7 +103,7 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate { // MARK: SwitchesViewControllerDelegate - func switchesViewControllerDidToggleDevice(controller: SwitchesViewController, device: WemoDevice) + func switchesViewControllerDidToggleDevices(controller: SwitchesViewController, devices: [WemoDevice]) { _updateVisualization(true) } @@ -120,6 +120,8 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate { } let percentageActivated = Float(activatedDevicesCount) / Float(self.devices.count) - _visualizationController.setPercentActivated(percentageActivated, animated: animated) + if (percentageActivated != _visualizationController.percentActivated) { + _visualizationController.setPercentActivated(percentageActivated, animated: animated) + } } } diff --git a/XIONControlPanel/Controllers/SwitchesViewController.swift b/XIONControlPanel/Controllers/SwitchesViewController.swift index cb5c343..ec116a4 100644 --- a/XIONControlPanel/Controllers/SwitchesViewController.swift +++ b/XIONControlPanel/Controllers/SwitchesViewController.swift @@ -11,32 +11,53 @@ import Foundation import UIKit protocol SwitchesViewControllerDelegate: class { - func switchesViewControllerDidToggleDevice(controller: SwitchesViewController, device: WemoDevice) + func switchesViewControllerDidToggleDevices(controller: SwitchesViewController, devices: [WemoDevice]) } extension SwitchesViewControllerDelegate { - func switchesViewControllerDidToggleDevice(controller: SwitchesViewController, device: WemoDevice) {} + func switchesViewControllerDidToggleDevices(controller: SwitchesViewController, devices: [WemoDevice]) {} } class SwitchesViewController: UIViewController, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout { - weak var delegate: SwitchesViewControllerDelegate? - + weak var delegate: SwitchesViewControllerDelegate? private var _collectionView: UICollectionView = UICollectionView(frame: CGRectZero, collectionViewLayout: UICollectionViewFlowLayout()) - private var _currentDevicesHash: UInt = 0 - static private let collectionViewCellReuseIdentifier = "SwitchesCollectionViewReuseID" + static private let collectionViewDeviceSwitchCellReuseIdentifier = "DeviceSwitchReuseID" + static private let collectionViewActionCellReuseIdentifier = "ActionCellReuseID" static private let collectionViewCellsSpacing: CGFloat = 5.0 static private let collectionViewCellsPerRow = 3 + private enum ActionCell: Int { + case AllOn + case AllOff + + func name() -> String + { + switch self { + case .AllOn: + return "All On" + case .AllOff: + return "All Off" + } + } + + static let count: Int = { + var max = 0 + while let _ = ActionCell(rawValue: max) { ++max } + return max + }() + } + override func viewDidLoad() { super.viewDidLoad() - let reuseID = SwitchesViewController.collectionViewCellReuseIdentifier + let deviceCellReuseID = SwitchesViewController.collectionViewDeviceSwitchCellReuseIdentifier + let actionCellReuseID = SwitchesViewController.collectionViewActionCellReuseIdentifier let layout = _collectionView.collectionViewLayout as! UICollectionViewFlowLayout layout.scrollDirection = .Vertical layout.minimumLineSpacing = SwitchesViewController.collectionViewCellsSpacing @@ -45,7 +66,8 @@ class SwitchesViewController: UIViewController, _collectionView.backgroundColor = UIColor.blackColor() _collectionView.delegate = self _collectionView.dataSource = self - _collectionView.registerClass(WemoDeviceCellView.self, forCellWithReuseIdentifier: reuseID) + _collectionView.registerClass(WemoDeviceCellView.self, forCellWithReuseIdentifier: deviceCellReuseID) + _collectionView.registerClass(WemoActionCellView.self, forCellWithReuseIdentifier: actionCellReuseID) self.view.addSubview(_collectionView) } @@ -63,15 +85,7 @@ class SwitchesViewController: UIViewController, { didSet { - var devicesHash: UInt = 0 - for device in devices { - devicesHash += UInt(device.serial.hash) - } - - if (_currentDevicesHash != devicesHash) { - _collectionView.reloadData() - _currentDevicesHash = devicesHash - } + _collectionView.reloadData() } } @@ -79,19 +93,28 @@ class SwitchesViewController: UIViewController, func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { - return self.devices.count + return self.devices.count + ActionCell.count } func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell { - let reuseID = SwitchesViewController.collectionViewCellReuseIdentifier - let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! WemoDeviceCellView - - let device = self.devices[indexPath.row] - cell.device = device - cell.ordinal = indexPath.row + 1 - - return cell + if (indexPath.item < ActionCell.count) { + let reuseID = SwitchesViewController.collectionViewActionCellReuseIdentifier + let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! WemoActionCellView + cell.textLabel.text = ActionCell(rawValue: indexPath.item)?.name().uppercaseString + + return cell + } else { + 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 + cell.ordinal = deviceIdx + 1 + + return cell + } } func collectionView(collectionView: UICollectionView, @@ -101,17 +124,44 @@ class SwitchesViewController: UIViewController, let spacing = SwitchesViewController.collectionViewCellsSpacing let cellsPerRow = CGFloat(SwitchesViewController.collectionViewCellsPerRow) let dimensions = rint(collectionView.bounds.size.width / cellsPerRow) - spacing - return CGSize(width: dimensions, height: dimensions) + + if (indexPath.item < ActionCell.count) { + return CGSize(width: collectionView.bounds.size.width, height: rint(dimensions / 2.0)) + } else { + return CGSize(width: dimensions, height: dimensions) + } } func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath) { - let cell = collectionView.cellForItemAtIndexPath(indexPath) as! WemoDeviceCellView - cell.toggled = !cell.toggled - - let device = self.devices[indexPath.row] - device.state = (cell.toggled ? .On : .Off) - - self.delegate?.switchesViewControllerDidToggleDevice(self, device: device) + if (indexPath.item < ActionCell.count) { + let tappedActionCell = ActionCell(rawValue: indexPath.item) + + var currentDelay: NSTimeInterval = 0.0 + for var i = ActionCell.count; i < collectionView.numberOfItemsInSection(indexPath.section); ++i { + let cell = collectionView.cellForItemAtIndexPath(NSIndexPath(forItem: i, inSection: indexPath.section)) as! WemoDeviceCellView + + UIView.animateWithDuration(0.3, delay: currentDelay, options: UIViewAnimationOptions(), animations: { () -> Void in + cell.toggled = (tappedActionCell == .AllOn) + }, completion: nil) + + currentDelay += 0.05 + } + + for device in self.devices { + device.state = (tappedActionCell == .AllOn ? .On : .Off) + } + + self.delegate?.switchesViewControllerDidToggleDevices(self, devices: self.devices) + } else { + let cell = collectionView.cellForItemAtIndexPath(indexPath) as! WemoDeviceCellView + cell.toggled = !cell.toggled + + let deviceIdx = indexPath.item - ActionCell.count + let device = self.devices[deviceIdx] + device.state = (cell.toggled ? .On : .Off) + + self.delegate?.switchesViewControllerDidToggleDevices(self, devices: [device]) + } } } diff --git a/XIONControlPanel/Controllers/VisualizationViewController.swift b/XIONControlPanel/Controllers/VisualizationViewController.swift index a874b6b..b6fb096 100644 --- a/XIONControlPanel/Controllers/VisualizationViewController.swift +++ b/XIONControlPanel/Controllers/VisualizationViewController.swift @@ -15,11 +15,12 @@ import UIKit let π = CGFloat(M_PI) class VisualizationViewController: UIViewController { - private var _scene: SCNScene = SCNScene() - private var _sceneView: SCNView? - private var _cameraNode: SCNNode = SCNNode() - private var _cubletsNode: SCNNode = SCNNode() - private var _cublets: [SCNNode] = [] + private var _scene: SCNScene = SCNScene() + private var _sceneView: SCNView? + private var _cameraNode: SCNNode = SCNNode() + private var _cubletsNode: SCNNode = SCNNode() + private var _cublets: [SCNNode] = [] + private var _percentActivated: Float = 0.0 static private let cubletsDimensions = 5 static private let cubletsSize = 1.0 @@ -63,6 +64,19 @@ class VisualizationViewController: UIViewController { // MARK: API + var percentActivated: Float + { + get + { + return _percentActivated + } + + set(percentage) + { + self.setPercentActivated(percentage, animated: false) + } + } + func setPercentActivated(percentage: Float, animated: Bool) { let cubletsCount = _cublets.count @@ -98,6 +112,8 @@ class VisualizationViewController: UIViewController { _cubletsNode.runAction(rotAction) } + + _percentActivated = percentage } // MARK: Internal diff --git a/XIONControlPanel/Views/WemoDeviceCellView.swift b/XIONControlPanel/Views/WemoCellView.swift similarity index 74% rename from XIONControlPanel/Views/WemoDeviceCellView.swift rename to XIONControlPanel/Views/WemoCellView.swift index 2d3db4a..1ba2c58 100644 --- a/XIONControlPanel/Views/WemoDeviceCellView.swift +++ b/XIONControlPanel/Views/WemoCellView.swift @@ -1,5 +1,5 @@ // -// WemoDeviceCellView.swift +// WemoDeviceView.swift // XIONControlPanel // // Created by Charles Magahern on 12/31/15. @@ -8,16 +8,58 @@ import UIKit -class WemoDeviceCellView: UICollectionViewCell { +public class WemoCellView: UICollectionViewCell { + private var _selectionOverlayView: UIView = UIView() + + static private var disabledBackgroundColor = UIColor(white: 0.2, alpha: 1.0) + static private var enabledBackgroundColor = UIColor(red: 0.0, green: 0.8, blue: 0.0, alpha: 1.0) + + override init(frame: CGRect) + { + super.init(frame: frame) + + self.contentView.backgroundColor = WemoCellView.disabledBackgroundColor + + _selectionOverlayView.backgroundColor = UIColor.clearColor() + self.contentView.addSubview(_selectionOverlayView) + } + + required public init?(coder aDecoder: NSCoder) + { + fatalError("unsupported") + } + + // MARK: Overrides + + override public func layoutSubviews() + { + super.layoutSubviews() + + let bounds = self.contentView.bounds + _selectionOverlayView.frame = bounds + } + + override public var highlighted: Bool { + didSet + { + if (self.highlighted) { + _selectionOverlayView.backgroundColor = UIColor(white: 0.8, alpha: 1.0) + } else { + UIView.animateWithDuration(1.0, animations: { () -> Void in + self._selectionOverlayView.backgroundColor = UIColor.clearColor() + }) + } + } + } +} + +public class WemoDeviceCellView: WemoCellView { private var _device: WemoDevice? private var _ordinal: Int = 0 - private var _selectionOverlayView: UIView = UIView() private var _nameLabel: UILabel = UILabel() private var _ordinalLabel: UILabel = UILabel() private var _indicator: SwitchIndicatorView = SwitchIndicatorView() - static private var disabledBackgroundColor = UIColor(white: 0.2, alpha: 1.0) - static private var enabledBackgroundColor = UIColor(red: 0.0, green: 0.8, blue: 0.0, alpha: 1.0) static private var disabledAnnotationsColor = UIColor.darkGrayColor() static private var enabledAnnotationsColor = UIColor.blackColor() @@ -25,11 +67,6 @@ class WemoDeviceCellView: UICollectionViewCell { { super.init(frame: frame) - self.contentView.backgroundColor = WemoDeviceCellView.disabledBackgroundColor - - _selectionOverlayView.backgroundColor = UIColor.clearColor() - self.contentView.addSubview(_selectionOverlayView) - _nameLabel.font = UIFont(name: "Orbitron-Medium", size: 16.0) _nameLabel.numberOfLines = 3 _nameLabel.textColor = UIColor.whiteColor() @@ -44,22 +81,20 @@ class WemoDeviceCellView: UICollectionViewCell { self.contentView.addSubview(_indicator) } - required init?(coder aDecoder: NSCoder) + required public init?(coder aDecoder: NSCoder) { fatalError("unsupported") } // MARK: Overrides - override func layoutSubviews() + override public func layoutSubviews() { super.layoutSubviews() let bounds = self.contentView.bounds let padding: CGFloat = 10.0 - _selectionOverlayView.frame = bounds - _indicator.frame = CGRect( x: padding, y: padding, @@ -84,19 +119,6 @@ class WemoDeviceCellView: UICollectionViewCell { ) } - override var highlighted: Bool { - didSet - { - if (self.highlighted) { - _selectionOverlayView.backgroundColor = UIColor(white: 0.8, alpha: 1.0) - } else { - UIView.animateWithDuration(1.0, animations: { () -> Void in - self._selectionOverlayView.backgroundColor = UIColor.clearColor() - }) - } - } - } - // MARK: API var device: WemoDevice? @@ -140,14 +162,39 @@ class WemoDeviceCellView: UICollectionViewCell { _indicator.status = toggled if (toggled) { - self.contentView.backgroundColor = WemoDeviceCellView.enabledBackgroundColor + self.contentView.backgroundColor = WemoCellView.enabledBackgroundColor _indicator.foregroundColor = WemoDeviceCellView.enabledAnnotationsColor _ordinalLabel.textColor = WemoDeviceCellView.enabledAnnotationsColor } else { - self.contentView.backgroundColor = WemoDeviceCellView.disabledBackgroundColor + self.contentView.backgroundColor = WemoCellView.disabledBackgroundColor _indicator.foregroundColor = WemoDeviceCellView.disabledAnnotationsColor _ordinalLabel.textColor = WemoDeviceCellView.disabledAnnotationsColor } } } } + +public class WemoActionCellView: WemoCellView { + public var textLabel: UILabel = UILabel() + + override init(frame: CGRect) + { + super.init(frame: frame) + + self.textLabel.font = UIFont(name: "Orbitron-Medium", size: 21.0) + self.textLabel.textColor = UIColor.whiteColor() + self.textLabel.textAlignment = .Center + self.addSubview(self.textLabel) + } + + required public init?(coder aDecoder: NSCoder) + { + fatalError("unsupported") + } + + override public func layoutSubviews() + { + super.layoutSubviews() + self.textLabel.frame = self.bounds + } +}