adds lights UI

This commit is contained in:
2025-06-19 19:25:53 -07:00
parent 397add6c8c
commit dba6754c0c
4 changed files with 357 additions and 105 deletions
@@ -12,10 +12,12 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate, Serv
{
fileprivate var _serverMultiplex: ServerMultiplex = ServerMultiplex()
fileprivate var _visualizationController: VisualizationViewController = VisualizationViewController()
fileprivate var _switchesController: SwitchesViewController = SwitchesViewController()
fileprivate var _headerView: HeaderView = HeaderView()
fileprivate var _updateDevices: Bool = false
fileprivate var _switchesController: SwitchesViewController = SwitchesViewController()
fileprivate var _lightsController: SwitchesViewController = SwitchesViewController()
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
{
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
@@ -46,6 +48,13 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate, Serv
self.addChild(_switchesController)
self.view.addSubview(_switchesController.view)
_lightsController.delegate = self
_lightsController.cellHeightScale = 0.7
_lightsController.actionCellLayout = .horizontal
_lightsController.labelText = "lights"
self.addChild(_lightsController)
self.view.addSubview(_lightsController.view)
self.view.addSubview(_headerView)
_updateConnectivityStatus(.disconnected)
@@ -75,10 +84,18 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate, Serv
x: bodyBounds.origin.x,
y: bodyBounds.origin.y,
width: rint(0.5 * bodyBounds.size.width),
height: bodyBounds.size.height
height: bodyBounds.size.height / 1.5
)
_visualizationController.view.frame = visualizationFrame
let lightsControllerFrame = CGRect(
x: bodyBounds.origin.x,
y: visualizationFrame.maxY,
width: visualizationFrame.width,
height: bodyBounds.height - visualizationFrame.height
)
_lightsController.view.frame = lightsControllerFrame.insetBy(dx: 18.0, dy: 0.0)
var switchesOriginX: CGFloat = 0.0
var switchesWidth: CGFloat = 0.0
if (_visualizationShouldBeVisible()) {
@@ -205,13 +222,16 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate, Serv
func serverMultiplex(_ multiplex: ServerMultiplex, didAddDevices devices: [AnyDevice])
{
_switchesController.devices = Array(multiplex.devices)
_switchesController.devices = multiplex.switchDevices
_lightsController.devices = multiplex.lightDevices
_updateVisualization(false)
}
func serverMultiplex(_ multiplex: ServerMultiplex, devicesStateChanged devices: [AnyDevice])
{
_switchesController.devicesStateChanged(devices)
_switchesController.devicesStateChanged(devices.switches)
_lightsController.devicesStateChanged(devices.lights)
}
func serverMultiplex(_ multiplex: ServerMultiplex, didReceiveAcknowledgementFromServer server: Server)
@@ -229,3 +249,15 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate, Serv
print(error.localizedDescription, to: &stderr)
}
}
extension Array where Element == AnyDevice
{
var lights: [AnyDevice] { filter { $0.type == .light } }
var switches: [AnyDevice] { filter { $0.type == .switch } }
}
extension ServerMultiplex
{
var lightDevices: [AnyDevice] { devices.filter { $0.type == .light } }
var switchDevices: [AnyDevice] { devices.filter { $0.type == .switch } }
}
@@ -20,6 +20,12 @@ class SwitchesViewController: UIViewController,
{
weak var delegate: SwitchesViewControllerDelegate?
public var cellHeightScale: Double = 1.0
public var actionCellLayout: ActionCellLayout = .vertical
public var labelText: String = "switches" {
didSet { _label.text = labelText }
}
fileprivate var _collectionView: UICollectionView = UICollectionView(frame: CGRect.zero,
collectionViewLayout: UICollectionViewFlowLayout())
@@ -27,6 +33,8 @@ class SwitchesViewController: UIViewController,
fileprivate var _currentDevicesHash: Int = 0
fileprivate let _label = UILabel(frame: .zero)
static fileprivate let collectionViewDeviceSwitchCellReuseIdentifier = "DeviceSwitchReuseID"
static fileprivate let collectionViewActionCellReuseIdentifier = "ActionCellReuseID"
static fileprivate let collectionViewCellsSpacing: CGFloat = 5.0
@@ -52,6 +60,12 @@ class SwitchesViewController: UIViewController,
static let count: Int = { return ActionCell.allCases.count }()
}
public enum ActionCellLayout
{
case horizontal
case vertical
}
override init(nibName nibNameOrNil: String?, bundle nibBundleOrNil: Bundle?)
{
super.init(nibName: nibNameOrNil, bundle: nibBundleOrNil)
@@ -84,6 +98,11 @@ class SwitchesViewController: UIViewController,
{
super.viewDidLoad()
_label.text = labelText
_label.font = UIFont(name: "Orbitron-Medium", size: 14.0)
_label.textColor = .white.withAlphaComponent(0.8)
self.view.addSubview(_label)
let deviceCellReuseID = SwitchesViewController.collectionViewDeviceSwitchCellReuseIdentifier
let actionCellReuseID = SwitchesViewController.collectionViewActionCellReuseIdentifier
let layout = _collectionView.collectionViewLayout as! UICollectionViewFlowLayout
@@ -106,7 +125,23 @@ class SwitchesViewController: UIViewController,
super.viewDidLayoutSubviews()
let bounds = self.view.bounds
_collectionView.frame = bounds
let labelSpacing = 8.0
let labelSize = _label.sizeThatFits(bounds.size)
_label.frame = CGRect(
x: 0.0,
y: 0.0,
width: labelSize.width,
height: labelSize.height
)
_collectionView.frame = CGRect(
x: 0.0,
y: _label.frame.maxY + labelSpacing,
width: bounds.width,
height: bounds.height - _label.frame.height - labelSpacing
)
_collectionView.collectionViewLayout.invalidateLayout()
}
@@ -196,9 +231,16 @@ class SwitchesViewController: UIViewController,
let dimensions = floor((collectionView.bounds.size.width / cellsPerRow) - ((spacing * (cellsPerRow - 1.0)) / cellsPerRow))
if (indexPath.section == SwitchesViewController.actionCellsSectionIdentifier) {
return CGSize(width: collectionView.bounds.size.width, height: rint(dimensions / 1.5))
let bounds = collectionView.bounds
let height = rint(dimensions / 1.5) * cellHeightScale
switch actionCellLayout {
case .horizontal:
return CGSize(width: (bounds.width - spacing) / 2.0, height: height)
case .vertical:
return CGSize(width: bounds.size.width, height: height)
}
} else {
return CGSize(width: dimensions, height: dimensions)
return CGSize(width: dimensions, height: dimensions * cellHeightScale)
}
}