Skip to content

Commit

Permalink
LightTarget#setColor respects power property in in-memory cache.
Browse files Browse the repository at this point in the history
  • Loading branch information
tatey committed Sep 2, 2015
1 parent f3e61d1 commit f0ad29d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
2 changes: 1 addition & 1 deletion Source/Light.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public struct Light: Equatable, Printable {
return Light(id: id, power: power, brightness: brightness, color: color, label: label, connected: connected, group: group, location: location)
}

func lightWithColor(color: Color, andBrightness brightness: Double) -> Light {
func lightWithPower(power: Bool, color: Color, brightness: Double) -> Light {
return Light(id: id, power: power, brightness: brightness, color: color, label: label, connected: connected, group: group, location: location)
}

Expand Down
10 changes: 6 additions & 4 deletions Source/LightTarget.swift
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,19 @@ public class LightTarget {
setColor(color, brightness: brightness, duration: duration, completionHandler: completionHandler)
}

public func setColor(color: Color, brightness: Double, duration: Float = LightTarget.defaultDuration, powerOn: Bool = LightTarget.defaultPowerOn, completionHandler: ((results: [Result], error: NSError?) -> Void)? = nil) {
public func setColor(color: Color, brightness: Double, duration: Float = LightTarget.defaultDuration, power: Bool = LightTarget.defaultPowerOn, completionHandler: ((results: [Result], error: NSError?) -> Void)? = nil) {
let newBrightness = brightness
let oldBrightness = self.brightness
let newColor = color
let oldColor = self.color
client.updateLights(lights.map({ $0.lightWithColor(newColor, andBrightness: newBrightness) }))
client.session.setLightsColor(selector.toQueryStringValue(), color: newColor.toQueryStringValue(newBrightness), duration: duration, powerOn: powerOn) { [weak self] (request, response, results, error) in
let newPower = power
let oldPower = self.power
client.updateLights(lights.map({ $0.lightWithPower(newPower, color: newColor, brightness: newBrightness) }))
client.session.setLightsColor(selector.toQueryStringValue(), color: newColor.toQueryStringValue(newBrightness), duration: duration, powerOn: newPower) { [weak self] (request, response, results, error) in
if let strongSelf = self {
var newLights = strongSelf.lightsByDeterminingConnectivityWithResults(strongSelf.lights, results: results)
if error != nil {
newLights = newLights.map({ $0.lightWithColor(oldColor, andBrightness: oldBrightness) })
newLights = newLights.map({ $0.lightWithPower(power, color: oldColor, brightness: oldBrightness) })
}
strongSelf.client.updateLights(newLights)
}
Expand Down

0 comments on commit f0ad29d

Please sign in to comment.