Visualization reaction to devices activating

This commit is contained in:
Charles Magahern
2015-12-31 17:30:23 -08:00
parent 303c620372
commit 4df09f18c5
6 changed files with 93 additions and 14 deletions

View File

@@ -8,7 +8,7 @@
import UIKit
class MainViewController: UIViewController {
class MainViewController: UIViewController, SwitchesViewControllerDelegate {
private var _visualizationController: VisualizationViewController = VisualizationViewController()
private var _switchesController: SwitchesViewController = SwitchesViewController()
private var _headerView: HeaderView = HeaderView()
@@ -24,10 +24,18 @@ class MainViewController: UIViewController {
self.addChildViewController(_visualizationController)
self.view.addSubview(_visualizationController.view)
_switchesController.delegate = self
self.addChildViewController(_switchesController)
self.view.addSubview(_switchesController.view)
self.view.addSubview(_headerView)
// DEBUG
for _ in 0 ..< 10 {
let device = WemoDevice()
device.name = "beatmania IIDX"
self.devices.append(device)
}
}
override func viewDidLayoutSubviews()
@@ -81,4 +89,37 @@ class MainViewController: UIViewController {
{
return true
}
// MARK: API
var devices: [WemoDevice] = []
{
didSet
{
_switchesController.devices = devices
_updateVisualization(false)
}
}
// MARK: SwitchesViewControllerDelegate
func switchesViewControllerDidToggleDevice(controller: SwitchesViewController, device: WemoDevice)
{
_updateVisualization(true)
}
// MARK: Internal
internal func _updateVisualization(animated: Bool)
{
var activatedDevicesCount = 0
for device in self.devices {
if (device.state == .On) {
++activatedDevicesCount
}
}
let percentageActivated = Float(activatedDevicesCount) / Float(self.devices.count)
_visualizationController.setPercentActivated(percentageActivated, animated: animated)
}
}

View File

@@ -10,12 +10,23 @@ import Darwin
import Foundation
import UIKit
protocol SwitchesViewControllerDelegate: class {
func switchesViewControllerDidToggleDevice(controller: SwitchesViewController, device: WemoDevice)
}
extension SwitchesViewControllerDelegate {
func switchesViewControllerDidToggleDevice(controller: SwitchesViewController, device: WemoDevice) {}
}
class SwitchesViewController: UIViewController,
UICollectionViewDataSource,
UICollectionViewDelegateFlowLayout
{
private var _collectionView: UICollectionView = UICollectionView(frame: CGRectZero,
collectionViewLayout: UICollectionViewFlowLayout())
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 collectionViewCellsSpacing: CGFloat = 5.0
@@ -46,11 +57,29 @@ class SwitchesViewController: UIViewController,
_collectionView.frame = bounds
}
// MARK: API
var devices: [WemoDevice] = []
{
didSet
{
var devicesHash: UInt = 0
for device in devices {
devicesHash += UInt(device.serial.hash)
}
if (_currentDevicesHash != devicesHash) {
_collectionView.reloadData()
_currentDevicesHash = devicesHash
}
}
}
// MARK: UICollectionView
func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int
{
return 10
return self.devices.count
}
func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell
@@ -58,10 +87,8 @@ class SwitchesViewController: UIViewController,
let reuseID = SwitchesViewController.collectionViewCellReuseIdentifier
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! WemoDeviceCellView
var device = WemoDevice()
device.name = "beatmania IIDX"
let device = self.devices[indexPath.row]
cell.device = device
cell.ordinal = indexPath.row + 1
return cell
@@ -81,5 +108,10 @@ class SwitchesViewController: UIViewController,
{
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)
}
}

View File

@@ -8,7 +8,7 @@
import Foundation
struct WemoDevice {
class WemoDevice {
enum State {
case Off
case On

View File

@@ -74,7 +74,8 @@ class ConnectionStatusView: UIView {
// MARK: API
var connectivityStatus: Bool {
var connectivityStatus: Bool
{
get
{
return _connectivityStatus

View File

@@ -74,7 +74,8 @@ class SwitchIndicatorView: UIView {
// MARK: API
var status: Bool {
var status: Bool
{
get
{
return _status
@@ -88,7 +89,8 @@ class SwitchIndicatorView: UIView {
}
}
var foregroundColor: UIColor {
var foregroundColor: UIColor
{
get
{
return _foregroundColor

View File

@@ -99,7 +99,8 @@ class WemoDeviceCellView: UICollectionViewCell {
// MARK: API
var device: WemoDevice? {
var device: WemoDevice?
{
get
{
return _device
@@ -112,7 +113,8 @@ class WemoDeviceCellView: UICollectionViewCell {
}
}
var ordinal: Int {
var ordinal: Int
{
get
{
return _ordinal
@@ -125,7 +127,8 @@ class WemoDeviceCellView: UICollectionViewCell {
}
}
var toggled: Bool {
var toggled: Bool
{
get
{
return (_device?.state == .On)