Files
Xaibatsu-Control-Panel/XIONControlPanel/Controllers/SwitchesViewController.swift

269 lines
10 KiB
Swift
Raw Normal View History

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
2016-07-24 00:14:04 -07:00
protocol SwitchesViewControllerDelegate: class
{
2017-05-13 00:40:03 -07:00
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [WemoDevice])
}
2016-07-24 00:14:04 -07:00
extension SwitchesViewControllerDelegate
{
2017-05-13 00:40:03 -07:00
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [WemoDevice]) {}
}
2015-12-31 15:41:32 -08:00
class SwitchesViewController: UIViewController,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
{
2018-09-13 19:25:55 -07:00
weak var delegate: SwitchesViewControllerDelegate?
2017-05-13 00:40:03 -07:00
fileprivate var _collectionView: UICollectionView = UICollectionView(frame: CGRect.zero,
collectionViewLayout: UICollectionViewFlowLayout())
2017-05-13 00:40:03 -07:00
fileprivate var _currentDevicesHash: Int = 0
2015-12-31 15:41:32 -08:00
2017-05-13 00:40:03 -07:00
static fileprivate let collectionViewDeviceSwitchCellReuseIdentifier = "DeviceSwitchReuseID"
static fileprivate let collectionViewActionCellReuseIdentifier = "ActionCellReuseID"
static fileprivate let collectionViewCellsSpacing: CGFloat = 5.0
2015-12-31 15:41:32 -08:00
2017-05-13 00:40:03 -07:00
fileprivate enum ActionCell: Int
2016-07-24 00:14:04 -07:00
{
2017-05-13 00:40:03 -07:00
case allOn
case allOff
2015-12-31 18:20:50 -08:00
func name() -> String
{
switch self {
2017-05-13 00:40:03 -07:00
case .allOn:
2015-12-31 18:20:50 -08:00
return "All On"
2017-05-13 00:40:03 -07:00
case .allOff:
2015-12-31 18:20:50 -08:00
return "All Off"
}
}
static let count: Int = {
var max = 0
while let _ = ActionCell(rawValue: max) { max += 1 }
2015-12-31 18:20:50 -08:00
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
2017-05-13 00:40:03 -07:00
layout.scrollDirection = .vertical
2015-12-31 15:41:32 -08:00
layout.minimumInteritemSpacing = SwitchesViewController.collectionViewCellsSpacing
2016-01-01 18:38:35 -08:00
layout.minimumLineSpacing = SwitchesViewController.collectionViewCellsSpacing
2015-12-31 15:41:32 -08:00
2017-05-13 00:40:03 -07:00
_collectionView.backgroundColor = UIColor.black
2015-12-31 15:41:32 -08:00
_collectionView.delegate = self
_collectionView.dataSource = self
2017-05-13 00:40:03 -07:00
_collectionView.register(WemoDeviceCellView.self, forCellWithReuseIdentifier: deviceCellReuseID)
_collectionView.register(WemoActionCellView.self, forCellWithReuseIdentifier: actionCellReuseID)
2015-12-31 15:41:32 -08:00
self.view.addSubview(_collectionView)
self.devices = []
2015-12-31 15:41:32 -08:00
}
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
let bounds = self.view.bounds
_collectionView.frame = bounds
}
// MARK: API
var devices: [WemoDevice] = []
{
didSet
{
2016-01-01 15:48:34 -08:00
// sort devices by name
2017-05-13 00:40:03 -07:00
self.devices.sort(by: { (d1: WemoDevice, d2: WemoDevice) -> Bool in
return (d1.name.compare(d2.name) == .orderedAscending)
2016-01-01 15:48:34 -08:00
})
2017-05-13 00:40:03 -07:00
let hash = self.devices.reduce(0, {$0 ^ $1.hashValue})
if (hash != _currentDevicesHash) {
let previousSet = NSOrderedSet(array: oldValue)
let newSet = NSOrderedSet(array: self.devices)
2017-05-13 00:40:03 -07:00
var insertedIndexPaths: [IndexPath] = []
var updatedIndexPaths: [IndexPath] = []
var deletedIndexPaths: [IndexPath] = []
// 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 actionCellIdx in 0 ..< ActionCell.count {
2017-05-13 00:40:03 -07:00
let actionCellIndexPath = IndexPath(item: actionCellIdx, section: 0)
updatedIndexPaths.append(actionCellIndexPath)
}
}
// find deletes and updates
2017-05-13 00:40:03 -07:00
for (idx, device) in previousSet.enumerated() {
let itemIndex = idx + ActionCell.count
2017-05-13 00:40:03 -07:00
let curIndexPath = IndexPath(item: itemIndex, section: 0)
2017-05-13 00:40:03 -07:00
if (!newSet.contains(device)) {
deletedIndexPaths.append(curIndexPath)
} else if (idx < newSet.count) {
2017-05-13 00:40:03 -07:00
let deviceInNewSet = newSet.object(at: idx) as! WemoDevice
if (deviceInNewSet != (device as! WemoDevice)) {
updatedIndexPaths.append(curIndexPath)
}
}
}
// find insertions
2017-05-13 00:40:03 -07:00
for (idx, device) in newSet.enumerated() {
if (!previousSet.contains(device)) {
let itemIndex = idx + ActionCell.count
2017-05-13 00:40:03 -07:00
let insertedIndexPath = IndexPath(item: itemIndex, section: 0)
insertedIndexPaths.append(insertedIndexPath)
}
}
UIView.performWithoutAnimation { () -> Void in
self._collectionView.performBatchUpdates({ () -> Void in
2017-05-13 00:40:03 -07:00
self._collectionView.deleteItems(at: deletedIndexPaths)
self._collectionView.reloadItems(at: updatedIndexPaths)
self._collectionView.insertItems(at: insertedIndexPaths)
}, completion: nil)
}
_currentDevicesHash = hash
}
}
}
2015-12-31 15:41:32 -08:00
// MARK: UICollectionView
2017-05-13 00:40:03 -07:00
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
2015-12-31 15:41:32 -08:00
{
2015-12-31 18:20:50 -08:00
return self.devices.count + ActionCell.count
2015-12-31 15:41:32 -08:00
}
2017-05-13 00:40:03 -07:00
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell
2015-12-31 15:41:32 -08:00
{
2015-12-31 18:20:50 -08:00
if (indexPath.item < ActionCell.count) {
let reuseID = SwitchesViewController.collectionViewActionCellReuseIdentifier
2017-05-13 00:40:03 -07:00
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseID, for: indexPath) as! WemoActionCellView
cell.textLabel.text = ActionCell(rawValue: indexPath.item)?.name().uppercased()
cell.enabled = (self.devices.count > 0)
2015-12-31 18:20:50 -08:00
return cell
} else {
let reuseID = SwitchesViewController.collectionViewDeviceSwitchCellReuseIdentifier
2017-05-13 00:40:03 -07:00
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: reuseID, for: indexPath) as! WemoDeviceCellView
2015-12-31 18:20:50 -08:00
2016-01-01 15:48:34 -08:00
let device = _deviceAtIndexPath(indexPath)
cell.deviceName = device.name
2017-05-13 00:40:03 -07: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
}
2017-05-13 00:40:03 -07:00
func collectionView(_ collectionView: UICollectionView,
2015-12-31 15:41:32 -08:00
layout collectionViewLayout: UICollectionViewLayout,
2017-05-13 00:40:03 -07:00
sizeForItemAt indexPath: IndexPath) -> CGSize
2015-12-31 15:41:32 -08:00
{
let spacing = SwitchesViewController.collectionViewCellsSpacing
2016-07-24 00:14:04 -07:00
let bounds = collectionView.bounds
var cellsPerRow: CGFloat = 0.0
2015-12-31 18:20:50 -08:00
2016-07-24 00:14:04 -07:00
switch (self.traitCollection.horizontalSizeClass) {
2017-05-13 00:40:03 -07:00
case .regular where (bounds.size.width >= 400.0),
.compact where (bounds.size.width >= 400.0):
2016-07-24 00:14:04 -07:00
cellsPerRow = 3.0
break
2017-05-13 00:40:03 -07:00
case .compact:
2016-07-24 00:14:04 -07:00
cellsPerRow = 2.0
default:
cellsPerRow = 2.0
}
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
2017-05-13 00:40:03 -07:00
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
2015-12-31 16:57:57 -08:00
{
2015-12-31 18:20:50 -08:00
if (indexPath.item < ActionCell.count) {
let tappedActionCell = ActionCell(rawValue: indexPath.item)
2017-05-13 00:40:03 -07:00
var currentDelay: TimeInterval = 0.0
2017-05-13 00:40:03 -07:00
for i in ActionCell.count ..< collectionView.numberOfItems(inSection: indexPath.section) {
if let cell = collectionView.cellForItem(at: IndexPath(item: i, section: indexPath.section)) as? WemoDeviceCellView {
let animOptions = UIViewAnimationOptions([.allowUserInteraction])
2017-05-13 00:40:03 -07:00
UIView.animate(withDuration: 0.3, delay: currentDelay, options: animOptions, animations: {
cell.toggled = (tappedActionCell == .allOn)
}, completion: nil)
currentDelay += 0.05
}
2015-12-31 18:20:50 -08:00
}
for device in self.devices {
2017-05-13 00:40:03 -07:00
device.state = (tappedActionCell == .allOn ? .on : .off)
2015-12-31 18:20:50 -08:00
}
self.delegate?.switchesViewControllerDidToggleDevices(self, devices: self.devices)
} else {
2017-05-13 00:40:03 -07:00
let cell = collectionView.cellForItem(at: indexPath) as! WemoDeviceCellView
2015-12-31 18:20:50 -08:00
cell.toggled = !cell.toggled
2016-01-01 15:48:34 -08:00
let device = _deviceAtIndexPath(indexPath)
2017-05-13 00:40:03 -07:00
device.state = (cell.toggled ? .on : .off)
2015-12-31 18:20:50 -08:00
self.delegate?.switchesViewControllerDidToggleDevices(self, devices: [device])
}
2015-12-31 16:57:57 -08:00
}
2017-05-13 00:40:03 -07:00
func collectionView(_ collectionView: UICollectionView, shouldHighlightItemAt indexPath: IndexPath) -> Bool
{
if (indexPath.item < ActionCell.count) {
return (self.devices.count > 0)
} else {
return true
}
}
2017-05-13 00:40:03 -07:00
func collectionView(_ collectionView: UICollectionView, shouldSelectItemAt indexPath: IndexPath) -> Bool
{
if (indexPath.item < ActionCell.count) {
return (self.devices.count > 0)
} else {
return true
}
}
2016-01-01 15:48:34 -08:00
// MARK: Internal
2017-05-13 00:40:03 -07:00
internal func _deviceAtIndexPath(_ indexPath: IndexPath) -> WemoDevice
2016-01-01 15:48:34 -08:00
{
let deviceIdx = indexPath.item - ActionCell.count
let device = self.devices[deviceIdx]
return device
}
2015-12-31 15:41:32 -08:00
}