Skip to content

Commit

Permalink
Working on UIDevice
Browse files Browse the repository at this point in the history
  • Loading branch information
colemancda committed Jun 24, 2017
1 parent 578e6e3 commit 5f966ec
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 22 deletions.
73 changes: 52 additions & 21 deletions Sources/Cacao/UIDevice.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,27 @@ public final class UIDevice {
/// Before accessing this property, ensure that battery monitoring is enabled.
/// If battery monitoring is not enabled, battery state is `unknown`
/// and the value of this property is –1.0.
public private(set) var batteryLevel: Float = -1.0
public var batteryLevel: Float {

#if os(macOS)
return Mac.batteryLevel
#elseif os(Linux)
return -1
#endif
}

/// A Boolean value indicating whether battery monitoring is enabled (true) or not (false).
public var isBatteryMonitoringEnabled: Bool = false

/// The battery state for the device.
public private(set) var batteryState: UIDeviceBatteryState = .unknown
public var batteryState: UIDeviceBatteryState {

#if os(macOS)
return Mac.batteryState
#elseif os(Linux)
return .unknown
#endif
}

// MARK: - Private

Expand Down Expand Up @@ -223,7 +237,7 @@ public enum UIDeviceBatteryState: Int {
return family?.description ?? hardwareModel
}

static func powerSources() -> [PowerSource] {
static var powerSources: [PowerSource] {

let sourcesInfo = IOPSCopyPowerSourcesInfo().takeRetainedValue()

Expand All @@ -235,9 +249,39 @@ public enum UIDeviceBatteryState: Int {
return []
}

static var batteryState: UIDeviceBatteryState {

guard let powerSource = powerSources.first
else { return .unknown }

switch powerSource.state {

case kIOPSACPowerValue:

// determine charging
if powerSource.currentCapacity == powerSource.maximumCapacity {

return .full

} else {

return .charging
}

case kIOPSBatteryPowerValue:

return .unplugged

default: return .unknown
}
}

static var batteryLevel: Float {

return -1
guard let powerSource = powerSources.first
else { return -1 }

return powerSource.chargedPercentage
}
}
}
Expand Down Expand Up @@ -272,28 +316,15 @@ public enum UIDeviceBatteryState: Int {

struct PowerSource {

enum State: String {

case offLine = "Off Line"
case ac = "AC Power"
case battery = "Battery Power"
}

enum Category: String {

case battery = "InternalBattery"
case ups = "UPS"
}

let identifier: Int
let serialNumber: String
let name: String
let maximumCapacity: Int
let currentCapacity: Int
let charging: Bool
let present: Bool
let state: State?
let category: Category?
let state: String
let category: String

var chargedPercentage: Float {

Expand All @@ -320,8 +351,8 @@ public enum UIDeviceBatteryState: Int {
self.currentCapacity = currentCapacity
self.charging = charging
self.present = present
self.state = State(rawValue: state)
self.category = Category(rawValue: type)
self.state = state
self.category = type
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion Sources/CacaoDemo/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ final class AppDelegate: UIApplicationDelegate {
print("Device: \(UIDevice.current.name)")
print("Model: \(UIDevice.current.model)")
print("System: \(UIDevice.current.systemName) \(UIDevice.current.systemVersion)")
print("Running at \(UIScreen.main.maximumFramesPerSecond) FPS")
print("Battery: \(UIDevice.current.batteryLevel) (\(UIDevice.current.batteryState))")
print("FPS: \(UIScreen.main.maximumFramesPerSecond)")

window = UIWindow(frame: UIScreen.main.bounds)

Expand Down

0 comments on commit 5f966ec

Please sign in to comment.