ServerMultiplex: Introduces a server mutiplexer

- Also moves all the existing Wemo* stuff over to support the multiplexer
- Moves MainViewController to be the owner of the multiplexer, and its delegate
This commit is contained in:
2020-03-14 18:33:02 -07:00
parent f1f86ced29
commit 918818cd99
8 changed files with 335 additions and 83 deletions
@@ -12,12 +12,7 @@ import UIKit
protocol SwitchesViewControllerDelegate: class
{
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [WemoDevice])
}
extension SwitchesViewControllerDelegate
{
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [WemoDevice]) {}
func switchesViewControllerDidToggleDevices(_ controller: SwitchesViewController, devices: [AnyDevice])
}
class SwitchesViewController: UIViewController,
@@ -117,12 +112,12 @@ class SwitchesViewController: UIViewController,
// MARK: API
var devices: [WemoDevice] = []
var devices: [AnyDevice] = []
{
didSet
{
// sort devices by name
self.devices.sort(by: { (d1: WemoDevice, d2: WemoDevice) -> Bool in
self.devices.sort(by: { (d1: Device, d2: Device) -> Bool in
return (d1.name.compare(d2.name, options: .caseInsensitive, range: nil, locale: nil) == .orderedAscending)
})
@@ -132,6 +127,19 @@ class SwitchesViewController: UIViewController,
}
}
public func devicesStateChanged(_ changedDevices: [AnyDevice])
{
var snapshot = _collectionViewDataSource.snapshot()
changedDevices.forEach { changedDevice in
if let existingDevice = (self.devices.first { $0.hashValue == changedDevice.hashValue }) {
existingDevice.state = changedDevice.state
snapshot.reloadItems([ existingDevice.hashValue ])
}
}
_collectionViewDataSource.apply(snapshot, animatingDifferences: true)
}
// MARK: UICollectionView
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath, identifier: Int) -> UICollectionViewCell?