Files
Xaibatsu-Control-Panel/XIONControlPanel/Controllers/MainViewController.swift
T

128 lines
3.6 KiB
Swift
Raw Normal View History

2015-12-30 02:51:22 -08:00
//
// MainViewController.swift
// XIONControlPanel
//
// Created by Charles Magahern on 12/29/15.
// Copyright © 2015 XION. All rights reserved.
//
import UIKit
class MainViewController: UIViewController, SwitchesViewControllerDelegate {
2015-12-30 18:47:13 -08:00
private var _visualizationController: VisualizationViewController = VisualizationViewController()
2015-12-31 15:41:32 -08:00
private var _switchesController: SwitchesViewController = SwitchesViewController()
2015-12-30 18:47:13 -08:00
private var _headerView: HeaderView = HeaderView()
2015-12-30 02:51:22 -08:00
// MARK: Overrides
2015-12-30 02:51:22 -08:00
override func viewDidLoad()
{
super.viewDidLoad()
self.view.backgroundColor = UIColor.blackColor()
2015-12-30 18:47:13 -08:00
self.addChildViewController(_visualizationController)
self.view.addSubview(_visualizationController.view)
_switchesController.delegate = self
2015-12-31 15:41:32 -08:00
self.addChildViewController(_switchesController)
self.view.addSubview(_switchesController.view)
2015-12-30 18:47:13 -08:00
self.view.addSubview(_headerView)
// DEBUG
for _ in 0 ..< 10 {
let device = WemoDevice()
device.name = "beatmania IIDX"
self.devices.append(device)
}
2015-12-30 02:51:22 -08:00
}
override func viewDidLayoutSubviews()
{
super.viewDidLayoutSubviews()
let bounds = self.view.bounds
2015-12-30 18:47:13 -08:00
let headerBounds = CGRect(
x: 0.0,
y: 0.0,
width: bounds.size.width,
height: rint(bounds.size.height / 10.0)
)
let bodyBounds = CGRect(
x: 0.0,
y: CGRectGetMaxY(headerBounds),
width: bounds.size.width,
height: bounds.size.height - headerBounds.size.height
)
_headerView.frame = headerBounds
let visualizationFrame = CGRect(
x: bodyBounds.origin.x,
y: bodyBounds.origin.y,
2015-12-31 15:41:32 -08:00
width: rint(0.6 * bodyBounds.size.width),
2015-12-30 18:47:13 -08:00
height: bodyBounds.size.height
)
_visualizationController.view.frame = visualizationFrame
2015-12-31 15:41:32 -08:00
let switchesControllerFrame = CGRect(
2015-12-30 18:47:13 -08:00
x: CGRectGetMaxX(visualizationFrame),
y: bodyBounds.origin.y,
width: bodyBounds.size.width - visualizationFrame.size.width,
height: bodyBounds.size.height
)
2015-12-31 15:41:32 -08:00
_switchesController.view.frame = switchesControllerFrame
2015-12-30 02:51:22 -08:00
}
2015-12-30 18:47:13 -08:00
override func viewDidAppear(animated: Bool)
{
2015-12-31 10:55:54 -08:00
_headerView.xionLogoView.beginAnimating()
}
2015-12-30 18:47:13 -08:00
override func viewDidDisappear(animated: Bool)
{
2015-12-31 10:55:54 -08:00
_headerView.xionLogoView.stopAnimating()
2015-12-30 18:47:13 -08:00
}
override func prefersStatusBarHidden() -> Bool
{
return true
}
// MARK: API
var devices: [WemoDevice] = []
{
didSet
{
_switchesController.devices = devices
_updateVisualization(false)
}
}
// MARK: SwitchesViewControllerDelegate
2015-12-31 18:20:50 -08:00
func switchesViewControllerDidToggleDevices(controller: SwitchesViewController, devices: [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)
2015-12-31 18:20:50 -08:00
if (percentageActivated != _visualizationController.percentActivated) {
_visualizationController.setPercentActivated(percentageActivated, animated: animated)
}
}
2015-12-30 02:51:22 -08:00
}