2015-12-31 15:41:32 -08:00
|
|
|
//
|
|
|
|
|
// SwitchesViewController.swift
|
|
|
|
|
// XIONControlPanel
|
|
|
|
|
//
|
|
|
|
|
// Created by Charles Magahern on 12/30/15.
|
|
|
|
|
// Copyright © 2015 XION. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
import Darwin
|
|
|
|
|
import Foundation
|
|
|
|
|
import UIKit
|
|
|
|
|
|
2015-12-31 17:30:23 -08:00
|
|
|
protocol SwitchesViewControllerDelegate: class {
|
2015-12-31 18:20:50 -08:00
|
|
|
func switchesViewControllerDidToggleDevices(controller: SwitchesViewController, devices: [WemoDevice])
|
2015-12-31 17:30:23 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
extension SwitchesViewControllerDelegate {
|
2015-12-31 18:20:50 -08:00
|
|
|
func switchesViewControllerDidToggleDevices(controller: SwitchesViewController, devices: [WemoDevice]) {}
|
2015-12-31 17:30:23 -08:00
|
|
|
}
|
|
|
|
|
|
2015-12-31 15:41:32 -08:00
|
|
|
class SwitchesViewController: UIViewController,
|
|
|
|
|
UICollectionViewDataSource,
|
|
|
|
|
UICollectionViewDelegateFlowLayout
|
|
|
|
|
{
|
2015-12-31 18:20:50 -08:00
|
|
|
weak var delegate: SwitchesViewControllerDelegate?
|
2015-12-31 17:30:23 -08:00
|
|
|
private var _collectionView: UICollectionView = UICollectionView(frame: CGRectZero,
|
|
|
|
|
collectionViewLayout: UICollectionViewFlowLayout())
|
2015-12-31 23:29:48 -08:00
|
|
|
private var _currentDevicesHash: Int = 0
|
2015-12-31 15:41:32 -08:00
|
|
|
|
2015-12-31 18:20:50 -08:00
|
|
|
static private let collectionViewDeviceSwitchCellReuseIdentifier = "DeviceSwitchReuseID"
|
|
|
|
|
static private let collectionViewActionCellReuseIdentifier = "ActionCellReuseID"
|
2015-12-31 15:41:32 -08:00
|
|
|
static private let collectionViewCellsSpacing: CGFloat = 5.0
|
|
|
|
|
static private let collectionViewCellsPerRow = 3
|
|
|
|
|
|
2015-12-31 18:20:50 -08:00
|
|
|
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
|
|
|
|
|
}()
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 15:41:32 -08:00
|
|
|
override func viewDidLoad()
|
|
|
|
|
{
|
|
|
|
|
super.viewDidLoad()
|
|
|
|
|
|
2015-12-31 18:20:50 -08:00
|
|
|
let deviceCellReuseID = SwitchesViewController.collectionViewDeviceSwitchCellReuseIdentifier
|
|
|
|
|
let actionCellReuseID = SwitchesViewController.collectionViewActionCellReuseIdentifier
|
2015-12-31 15:41:32 -08:00
|
|
|
let layout = _collectionView.collectionViewLayout as! UICollectionViewFlowLayout
|
|
|
|
|
layout.scrollDirection = .Vertical
|
|
|
|
|
layout.minimumInteritemSpacing = SwitchesViewController.collectionViewCellsSpacing
|
2016-01-01 18:38:35 -08:00
|
|
|
layout.minimumLineSpacing = SwitchesViewController.collectionViewCellsSpacing
|
2015-12-31 15:41:32 -08:00
|
|
|
|
|
|
|
|
_collectionView.backgroundColor = UIColor.blackColor()
|
|
|
|
|
_collectionView.delegate = self
|
|
|
|
|
_collectionView.dataSource = self
|
2015-12-31 18:20:50 -08:00
|
|
|
_collectionView.registerClass(WemoDeviceCellView.self, forCellWithReuseIdentifier: deviceCellReuseID)
|
|
|
|
|
_collectionView.registerClass(WemoActionCellView.self, forCellWithReuseIdentifier: actionCellReuseID)
|
2015-12-31 15:41:32 -08:00
|
|
|
self.view.addSubview(_collectionView)
|
2015-12-31 19:51:51 -08:00
|
|
|
|
|
|
|
|
self.devices = []
|
2015-12-31 15:41:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
override func viewDidLayoutSubviews()
|
|
|
|
|
{
|
|
|
|
|
super.viewDidLayoutSubviews()
|
|
|
|
|
|
|
|
|
|
let bounds = self.view.bounds
|
|
|
|
|
_collectionView.frame = bounds
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 17:30:23 -08:00
|
|
|
// MARK: API
|
|
|
|
|
|
|
|
|
|
var devices: [WemoDevice] = []
|
|
|
|
|
{
|
|
|
|
|
didSet
|
|
|
|
|
{
|
2016-01-01 15:48:34 -08:00
|
|
|
// sort devices by name
|
|
|
|
|
self.devices.sortInPlace({ (d1: WemoDevice, d2: WemoDevice) -> Bool in
|
|
|
|
|
return (d1.name.compare(d2.name) == .OrderedAscending)
|
|
|
|
|
})
|
|
|
|
|
|
2015-12-31 23:29:48 -08:00
|
|
|
let hash = self.devices.reduce(0, combine: {$0 ^ $1.hashValue})
|
|
|
|
|
if (hash != _currentDevicesHash) {
|
|
|
|
|
let previousSet = NSOrderedSet(array: oldValue)
|
|
|
|
|
let newSet = NSOrderedSet(array: self.devices)
|
|
|
|
|
var insertedIndexPaths: [NSIndexPath] = []
|
|
|
|
|
var updatedIndexPaths: [NSIndexPath] = []
|
|
|
|
|
var deletedIndexPaths: [NSIndexPath] = []
|
|
|
|
|
|
|
|
|
|
// if we have devices now and we didn't before, or vice versa,
|
|
|
|
|
// we need to update the action cells
|
|
|
|
|
if ((oldValue.count == 0 && self.devices.count != 0) || (self.devices.count == 0 && oldValue.count != 0)) {
|
|
|
|
|
for var actionCellIdx = 0; actionCellIdx < ActionCell.count; ++actionCellIdx {
|
|
|
|
|
let actionCellIndexPath = NSIndexPath(forItem: actionCellIdx, inSection: 0)
|
|
|
|
|
updatedIndexPaths.append(actionCellIndexPath)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find deletes and updates
|
|
|
|
|
for (idx, device) in previousSet.enumerate() {
|
|
|
|
|
let itemIndex = idx + ActionCell.count
|
|
|
|
|
let curIndexPath = NSIndexPath(forItem: itemIndex, inSection: 0)
|
|
|
|
|
|
|
|
|
|
if (!newSet.containsObject(device)) {
|
|
|
|
|
deletedIndexPaths.append(curIndexPath)
|
|
|
|
|
} else if (idx < newSet.count) {
|
|
|
|
|
let deviceInNewSet = newSet.objectAtIndex(idx) as! WemoDevice
|
|
|
|
|
if (deviceInNewSet != (device as! WemoDevice)) {
|
|
|
|
|
updatedIndexPaths.append(curIndexPath)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// find insertions
|
|
|
|
|
for (idx, device) in newSet.enumerate() {
|
|
|
|
|
if (!previousSet.containsObject(device)) {
|
|
|
|
|
let itemIndex = idx + ActionCell.count
|
|
|
|
|
let insertedIndexPath = NSIndexPath(forItem: itemIndex, inSection: 0)
|
|
|
|
|
insertedIndexPaths.append(insertedIndexPath)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
UIView.performWithoutAnimation { () -> Void in
|
|
|
|
|
self._collectionView.performBatchUpdates({ () -> Void in
|
|
|
|
|
self._collectionView.deleteItemsAtIndexPaths(deletedIndexPaths)
|
|
|
|
|
self._collectionView.reloadItemsAtIndexPaths(updatedIndexPaths)
|
|
|
|
|
self._collectionView.insertItemsAtIndexPaths(insertedIndexPaths)
|
|
|
|
|
}, completion: nil)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_currentDevicesHash = hash
|
|
|
|
|
}
|
2015-12-31 17:30:23 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-12-31 15:41:32 -08:00
|
|
|
// MARK: UICollectionView
|
|
|
|
|
|
|
|
|
|
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
|
|
|
|
|
{
|
2015-12-31 18:20:50 -08:00
|
|
|
return self.devices.count + ActionCell.count
|
2015-12-31 15:41:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
|
|
|
|
|
{
|
2015-12-31 18:20:50 -08:00
|
|
|
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
|
2015-12-31 19:51:51 -08:00
|
|
|
cell.enabled = (self.devices.count > 0)
|
2015-12-31 18:20:50 -08:00
|
|
|
|
|
|
|
|
return cell
|
|
|
|
|
} else {
|
|
|
|
|
let reuseID = SwitchesViewController.collectionViewDeviceSwitchCellReuseIdentifier
|
|
|
|
|
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! WemoDeviceCellView
|
|
|
|
|
|
2016-01-01 15:48:34 -08:00
|
|
|
let device = _deviceAtIndexPath(indexPath)
|
|
|
|
|
cell.deviceName = device.name
|
2015-12-31 23:29:48 -08:00
|
|
|
cell.toggled = (device.state == .On)
|
2016-01-01 15:48:34 -08:00
|
|
|
cell.ordinal = indexPath.item - ActionCell.count + 1
|
2015-12-31 18:20:50 -08:00
|
|
|
|
|
|
|
|
return cell
|
|
|
|
|
}
|
2015-12-31 15:41:32 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionView(collectionView: UICollectionView,
|
|
|
|
|
layout collectionViewLayout: UICollectionViewLayout,
|
|
|
|
|
sizeForItemAtIndexPath indexPath: NSIndexPath) -> CGSize
|
|
|
|
|
{
|
|
|
|
|
let spacing = SwitchesViewController.collectionViewCellsSpacing
|
|
|
|
|
let cellsPerRow = CGFloat(SwitchesViewController.collectionViewCellsPerRow)
|
2016-01-03 13:21:50 -08:00
|
|
|
let dimensions = floor((collectionView.bounds.size.width / cellsPerRow) - ((spacing * (cellsPerRow - 1.0)) / cellsPerRow))
|
2015-12-31 18:20:50 -08:00
|
|
|
|
|
|
|
|
if (indexPath.item < ActionCell.count) {
|
|
|
|
|
return CGSize(width: collectionView.bounds.size.width, height: rint(dimensions / 2.0))
|
|
|
|
|
} else {
|
|
|
|
|
return CGSize(width: dimensions, height: dimensions)
|
|
|
|
|
}
|
2015-12-31 15:41:32 -08:00
|
|
|
}
|
2015-12-31 16:57:57 -08:00
|
|
|
|
|
|
|
|
func collectionView(collectionView: UICollectionView, didSelectItemAtIndexPath indexPath: NSIndexPath)
|
|
|
|
|
{
|
2015-12-31 18:20:50 -08:00
|
|
|
if (indexPath.item < ActionCell.count) {
|
|
|
|
|
let tappedActionCell = ActionCell(rawValue: indexPath.item)
|
|
|
|
|
var currentDelay: NSTimeInterval = 0.0
|
2015-12-31 19:51:51 -08:00
|
|
|
|
2015-12-31 18:20:50 -08:00
|
|
|
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
|
|
|
|
|
|
2016-01-01 15:48:34 -08:00
|
|
|
let device = _deviceAtIndexPath(indexPath)
|
2015-12-31 18:20:50 -08:00
|
|
|
device.state = (cell.toggled ? .On : .Off)
|
|
|
|
|
|
|
|
|
|
self.delegate?.switchesViewControllerDidToggleDevices(self, devices: [device])
|
|
|
|
|
}
|
2015-12-31 16:57:57 -08:00
|
|
|
}
|
2015-12-31 19:51:51 -08:00
|
|
|
|
|
|
|
|
func collectionView(collectionView: UICollectionView, shouldHighlightItemAtIndexPath indexPath: NSIndexPath) -> Bool
|
|
|
|
|
{
|
|
|
|
|
if (indexPath.item < ActionCell.count) {
|
|
|
|
|
return (self.devices.count > 0)
|
|
|
|
|
} else {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectionView(collectionView: UICollectionView, shouldSelectItemAtIndexPath indexPath: NSIndexPath) -> Bool
|
|
|
|
|
{
|
|
|
|
|
if (indexPath.item < ActionCell.count) {
|
|
|
|
|
return (self.devices.count > 0)
|
|
|
|
|
} else {
|
|
|
|
|
return true
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-01-01 15:48:34 -08:00
|
|
|
|
|
|
|
|
// MARK: Internal
|
|
|
|
|
|
|
|
|
|
internal func _deviceAtIndexPath(indexPath: NSIndexPath) -> WemoDevice
|
|
|
|
|
{
|
|
|
|
|
let deviceIdx = indexPath.item - ActionCell.count
|
|
|
|
|
let device = self.devices[deviceIdx]
|
|
|
|
|
return device
|
|
|
|
|
}
|
2015-12-31 15:41:32 -08:00
|
|
|
}
|