Skip to content

Commit

Permalink
feat(battery_plus): Add battery save mode check on MacOS (#3332)
Browse files Browse the repository at this point in the history
  • Loading branch information
vbuberen authored Oct 21, 2024
1 parent d8c95c2 commit 0e8c922
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/battery_plus/battery_plus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ battery.onBatteryStateChanged.listen((BatteryState state) {
});
// Check if device in battery save mode
// Currently available on Android, iOS and Windows platforms only
// Currently available on Android, iOS, MacOS and Windows platforms only
print(await battery.isInBatterySaveMode);
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@ void main() {
Platform.isMacOS ||
Platform.isWindows ||
Platform.isLinux;
final bool isInBatterySaveModeIsImplemented =
Platform.isAndroid || Platform.isIOS || Platform.isWindows;
final bool isInBatterySaveModeIsImplemented = Platform.isAndroid ||
Platform.isIOS ||
Platform.isMacOS ||
Platform.isWindows;

/// Throws [PlatformException] on iOS simulator and Windows.
/// Run on Android only.
Expand Down
6 changes: 3 additions & 3 deletions packages/battery_plus/battery_plus/lib/battery_plus.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ class Battery {
return BatteryPlatform.instance;
}

/// get battery level
/// Get battery level
Future<int> get batteryLevel {
return _platform.batteryLevel;
}

/// check if device is on battery save mode
/// Check if device is on battery save mode
///
/// Currently only implemented on Android, iOS and Windows.
/// Currently only implemented on Android, iOS, MacOS and Windows.
Future<bool> get isInBatterySaveMode {
return _platform.isInBatterySaveMode;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,39 +12,40 @@ public class BatteryPlusMacosPlugin: NSObject, FlutterPlugin {

public static func register(with registrar: FlutterPluginRegistrar) {
let channel = FlutterMethodChannel(name: "dev.fluttercommunity.plus/battery", binaryMessenger: registrar.messenger)

let eventChannel = FlutterEventChannel(name: "dev.fluttercommunity.plus/charging", binaryMessenger: registrar.messenger)

let chargingHandler = BatteryPlusChargingHandler()

let instance = BatteryPlusMacosPlugin(chargingHandler: chargingHandler)
registrar.addMethodCallDelegate(instance, channel: channel)

eventChannel.setStreamHandler(chargingHandler)
}

public func handle(_ call: FlutterMethodCall, result: @escaping FlutterResult) {
switch call.method {
case "getBatteryLevel":
handleGetBatteryLevelMethodCall(result)
case "getBatteryState":
handleGetBatteryStateMethodCall(result)
case "isInBatterySaveMode":
handleIsBatterySaveMode(result)
default:
result(FlutterMethodNotImplemented)
}
}
private func handleGetBatteryLevelMethodCall(_ result: FlutterResult){

private func handleGetBatteryLevelMethodCall(_ result: FlutterResult) {
let level = getBatteryLevel()
if(level != -1){
if (level != -1) {
result(level)

} else {
result("UNAVAILABLE")
}

}

private func getBatteryLevel()-> Int {
let powerSourceSnapshot = IOPSCopyPowerSourcesInfo().takeRetainedValue()
let sources = IOPSCopyPowerSourcesList(powerSourceSnapshot).takeRetainedValue() as Array
Expand All @@ -58,8 +59,17 @@ public class BatteryPlusMacosPlugin: NSObject, FlutterPlugin {
return -1
}

private func handleGetBatteryStateMethodCall(_ result: FlutterResult){
private func handleGetBatteryStateMethodCall(_ result: FlutterResult) {
let state = self.chargingHandler.getBatteryStatus()
result(state);
}

private func handleIsBatterySaveMode(_ result: FlutterResult) {
if #available(macOS 12.0, *) {
result(ProcessInfo.processInfo.isLowPowerModeEnabled)
} else {
// Low Power Mode is not supported on macOS versions prior to 12.0
result(false)
}
}
}

0 comments on commit 0e8c922

Please sign in to comment.