From 0e8c9223f00f801abeb203c25a79b8f19259a11c Mon Sep 17 00:00:00 2001 From: Volodymyr Buberenko Date: Mon, 21 Oct 2024 12:00:04 +0300 Subject: [PATCH] feat(battery_plus): Add battery save mode check on MacOS (#3332) --- packages/battery_plus/battery_plus/README.md | 2 +- .../integration_test/battery_plus_test.dart | 6 ++-- .../battery_plus/lib/battery_plus.dart | 6 ++-- .../battery_plus/BatteryPlusMacosPlugin.swift | 30 ++++++++++++------- 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/packages/battery_plus/battery_plus/README.md b/packages/battery_plus/battery_plus/README.md index 6dfef1f81d..9e18a59c99 100644 --- a/packages/battery_plus/battery_plus/README.md +++ b/packages/battery_plus/battery_plus/README.md @@ -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); ``` diff --git a/packages/battery_plus/battery_plus/example/integration_test/battery_plus_test.dart b/packages/battery_plus/battery_plus/example/integration_test/battery_plus_test.dart index 41a500e740..04c0855dbf 100644 --- a/packages/battery_plus/battery_plus/example/integration_test/battery_plus_test.dart +++ b/packages/battery_plus/battery_plus/example/integration_test/battery_plus_test.dart @@ -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. diff --git a/packages/battery_plus/battery_plus/lib/battery_plus.dart b/packages/battery_plus/battery_plus/lib/battery_plus.dart index 0ffaff6a61..3f28505414 100644 --- a/packages/battery_plus/battery_plus/lib/battery_plus.dart +++ b/packages/battery_plus/battery_plus/lib/battery_plus.dart @@ -32,14 +32,14 @@ class Battery { return BatteryPlatform.instance; } - /// get battery level + /// Get battery level Future 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 get isInBatterySaveMode { return _platform.isInBatterySaveMode; } diff --git a/packages/battery_plus/battery_plus/macos/battery_plus/Sources/battery_plus/BatteryPlusMacosPlugin.swift b/packages/battery_plus/battery_plus/macos/battery_plus/Sources/battery_plus/BatteryPlusMacosPlugin.swift index 246336c034..357dbca061 100644 --- a/packages/battery_plus/battery_plus/macos/battery_plus/Sources/battery_plus/BatteryPlusMacosPlugin.swift +++ b/packages/battery_plus/battery_plus/macos/battery_plus/Sources/battery_plus/BatteryPlusMacosPlugin.swift @@ -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 @@ -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) + } + } }