diff --git a/XIONControlPanel/Controllers/MainViewController.swift b/XIONControlPanel/Controllers/MainViewController.swift index fa5bb4a..2c7a836 100644 --- a/XIONControlPanel/Controllers/MainViewController.swift +++ b/XIONControlPanel/Controllers/MainViewController.swift @@ -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) + } } diff --git a/XIONControlPanel/Controllers/SwitchesViewController.swift b/XIONControlPanel/Controllers/SwitchesViewController.swift index 9361ea0..cb5c343 100644 --- a/XIONControlPanel/Controllers/SwitchesViewController.swift +++ b/XIONControlPanel/Controllers/SwitchesViewController.swift @@ -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) } } diff --git a/XIONControlPanel/Models/WemoDevice.swift b/XIONControlPanel/Models/WemoDevice.swift index 8a5239f..77194e8 100644 --- a/XIONControlPanel/Models/WemoDevice.swift +++ b/XIONControlPanel/Models/WemoDevice.swift @@ -8,7 +8,7 @@ import Foundation -struct WemoDevice { +class WemoDevice { enum State { case Off case On diff --git a/XIONControlPanel/Views/ConnectionStatusView.swift b/XIONControlPanel/Views/ConnectionStatusView.swift index 07d44e7..2d17845 100644 --- a/XIONControlPanel/Views/ConnectionStatusView.swift +++ b/XIONControlPanel/Views/ConnectionStatusView.swift @@ -74,7 +74,8 @@ class ConnectionStatusView: UIView { // MARK: API - var connectivityStatus: Bool { + var connectivityStatus: Bool + { get { return _connectivityStatus diff --git a/XIONControlPanel/Views/SwitchIndicatorView.swift b/XIONControlPanel/Views/SwitchIndicatorView.swift index dbc3eee..726562e 100644 --- a/XIONControlPanel/Views/SwitchIndicatorView.swift +++ b/XIONControlPanel/Views/SwitchIndicatorView.swift @@ -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 diff --git a/XIONControlPanel/Views/WemoDeviceCellView.swift b/XIONControlPanel/Views/WemoDeviceCellView.swift index 38fa15c..2d3db4a 100644 --- a/XIONControlPanel/Views/WemoDeviceCellView.swift +++ b/XIONControlPanel/Views/WemoDeviceCellView.swift @@ -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)