"ALL ON" and "ALL OFF" switches

This commit is contained in:
Charles Magahern
2015-12-31 18:20:50 -08:00
parent 4df09f18c5
commit e85e9173e5
5 changed files with 189 additions and 74 deletions

View File

@@ -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)
}
}
}

View File

@@ -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])
}
}
}

View File

@@ -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

View File

@@ -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
}
}