Skip to content

Instantly share code, notes, and snippets.

@erica
Created January 2, 2017 00:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erica/c96834bc27a686f664c2a8a40ed16261 to your computer and use it in GitHub Desktop.
Save erica/c96834bc27a686f664c2a8a40ed16261 to your computer and use it in GitHub Desktop.
fileprivate func chargeState(for battery: BatteryService)
-> ChargeState
{
switch (battery, isCalculating(battery)) {
case (.isACPowered, true):
return .calculating(isDischarging: false)
case (.isACPowered, _) where battery.isCharging:
return .charging
case (.isACPowered, _):
return .power //bypasses error
case (_, true):
return .calculating(isDischarging: true)
default:
return .battery
}
}
fileprivate func chargeState(for battery: BatteryService)
--> ChargeState
{
let calculating = isCalculating(battery: battery)
if battery.isACPowered {
if calculating {
return .calculating(isDischarging: false)
} else if battery.isCharging {
return .charging
} else if battery.isCharged {
return .power
} // error here, non-exhaustive tests
} else {
if calculating {
return .calculating(isDischarging: true)
} else {
return .battery
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment