-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #899 from glouel/master
Add battery Info
- Loading branch information
Showing
16 changed files
with
376 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// | ||
// Battery.swift | ||
// Aerial | ||
// | ||
// Created by Guillaume Louel on 06/12/2019. | ||
// Copyright © 2019 John Coates. All rights reserved. | ||
// | ||
|
||
import Foundation | ||
|
||
struct Battery { | ||
|
||
// MARK: - Battery detection | ||
static func isUnplugged() -> Bool { | ||
return IOPSGetTimeRemainingEstimate() != kIOPSTimeRemainingUnlimited | ||
} | ||
|
||
static func isCharging() -> Bool { | ||
let timeRemaining: CFTimeInterval = IOPSGetTimeRemainingEstimate() | ||
if timeRemaining == -2.0 { | ||
return true | ||
} else { | ||
return false | ||
} | ||
} | ||
|
||
static func isLow() -> Bool { | ||
return getRemainingPercent() < 20 | ||
} | ||
|
||
static func getRemainingPercent() -> Int { | ||
// Take a snapshot of all the power source info | ||
guard let snapshot = IOPSCopyPowerSourcesInfo()?.takeRetainedValue() | ||
else { return 0 } | ||
|
||
// Pull out a list of power sources | ||
guard let sources: NSArray = IOPSCopyPowerSourcesList(snapshot)?.takeRetainedValue() | ||
else { return 0 } | ||
|
||
// swiftlint:disable:next empty_count | ||
if sources.count > 0 { | ||
// For each power source... | ||
for ps in sources { | ||
// Fetch the information for a given power source out of our snapshot | ||
guard let info: NSDictionary = IOPSGetPowerSourceDescription(snapshot, ps as CFTypeRef)?.takeUnretainedValue() | ||
else { return 0 } | ||
|
||
// Pull out the name and current capacity | ||
if let capacity = info[kIOPSCurrentCapacityKey] as? Int, | ||
let max = info[kIOPSMaxCapacityKey] as? Int { | ||
|
||
return Int(Double(capacity)/Double(max)*100) | ||
} | ||
} | ||
} | ||
|
||
return 0 | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.