Fix miscellaneous bugs

This commit is contained in:
Charles Magahern
2016-01-01 15:48:34 -08:00
parent 80696898ef
commit 29b70d32ce
4 changed files with 26 additions and 36 deletions

View File

@@ -130,6 +130,7 @@ class MainViewController: UIViewController, SwitchesViewControllerDelegate {
_updateVisualization(true)
for device in devices {
// WARNING: don't commit
_server.toggleDevice(device, state: device.state, completion: { (error: NSError?) -> Void in })
}
}

View File

@@ -88,20 +88,13 @@ class SwitchesViewController: UIViewController,
{
didSet
{
let hash = self.devices.reduce(0, combine: {$0 ^ $1.hashValue})
if (hash != _currentDevicesHash) {
// sort by name
// sort devices by name
self.devices.sortInPlace({ (d1: WemoDevice, d2: WemoDevice) -> Bool in
return (d1.name.compare(d2.name) == .OrderedAscending)
})
// replace "Dance Dance Revolution" names with "DDR" to save space
for device in self.devices {
if let range = device.name.rangeOfString("Dance Dance Revolution") {
device.name.replaceRange(range, with: "DDR")
}
}
let hash = self.devices.reduce(0, combine: {$0 ^ $1.hashValue})
if (hash != _currentDevicesHash) {
let previousSet = NSOrderedSet(array: oldValue)
let newSet = NSOrderedSet(array: self.devices)
var insertedIndexPaths: [NSIndexPath] = []
@@ -174,11 +167,10 @@ class SwitchesViewController: UIViewController,
let reuseID = SwitchesViewController.collectionViewDeviceSwitchCellReuseIdentifier
let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseID, forIndexPath: indexPath) as! WemoDeviceCellView
let deviceIdx = indexPath.item - ActionCell.count
let device = self.devices[deviceIdx]
cell.device = device
let device = _deviceAtIndexPath(indexPath)
cell.deviceName = device.name
cell.toggled = (device.state == .On)
cell.ordinal = deviceIdx + 1
cell.ordinal = indexPath.item - ActionCell.count + 1
return cell
}
@@ -224,8 +216,7 @@ class SwitchesViewController: UIViewController,
let cell = collectionView.cellForItemAtIndexPath(indexPath) as! WemoDeviceCellView
cell.toggled = !cell.toggled
let deviceIdx = indexPath.item - ActionCell.count
let device = self.devices[deviceIdx]
let device = _deviceAtIndexPath(indexPath)
device.state = (cell.toggled ? .On : .Off)
self.delegate?.switchesViewControllerDidToggleDevices(self, devices: [device])
@@ -249,4 +240,13 @@ class SwitchesViewController: UIViewController,
return true
}
}
// MARK: Internal
internal func _deviceAtIndexPath(indexPath: NSIndexPath) -> WemoDevice
{
let deviceIdx = indexPath.item - ActionCell.count
let device = self.devices[deviceIdx]
return device
}
}

View File

@@ -93,6 +93,8 @@ class WemoServer {
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "POST"
print("Toggle: \(url!.absoluteString)")
let task = _urlSession.dataTaskWithRequest(request) { (data: NSData?, response: NSURLResponse?, error: NSError?) -> Void in
var clientError: NSError? = nil

View File

@@ -54,7 +54,6 @@ public class WemoCellView: UICollectionViewCell {
}
public class WemoDeviceCellView: WemoCellView {
private var _device: WemoDevice?
private var _ordinal: Int = 0
private var _nameLabel: UILabel = UILabel()
private var _ordinalLabel: UILabel = UILabel()
@@ -123,17 +122,11 @@ public class WemoDeviceCellView: WemoCellView {
// MARK: API
var device: WemoDevice?
var deviceName: String = ""
{
get
didSet
{
return _device
}
set(device)
{
_device = device
_nameLabel.text = _device?.name.uppercaseString
_nameLabel.text = self.deviceName.uppercaseString
self.setNeedsLayout()
}
}
@@ -153,16 +146,10 @@ public class WemoDeviceCellView: WemoCellView {
}
}
var toggled: Bool
var toggled: Bool = false
{
get
didSet
{
return (_device?.state == .On)
}
set(toggled)
{
_device?.state = (toggled ? .On : .Off)
_indicator.status = toggled
if (toggled) {