feat(ios): implement getBatteryStatus method

Implemented battery status method matching Android functionality:

getBatteryStatus():
- Gets battery level (0-100%) using UIDevice
- Detects charging state (charging or full)
- Maps battery state to power state code (0=unknown, 1=unplugged, 2=charging, 3=full)
- Returns isOptimizationExempt (always false on iOS - no battery optimization)
- Enables battery monitoring automatically

iOS Adaptations:
- Uses UIDevice.current for battery information
- Battery optimization not applicable on iOS (Background App Refresh is system setting)
- Returns -1 for battery level if unknown

Progress: 16/52 methods implemented (31% complete)
This commit is contained in:
Matthew Raymer
2025-11-11 01:56:42 -08:00
parent f651124466
commit 928733f87f

View File

@@ -11,6 +11,7 @@ import Capacitor
import UserNotifications
import BackgroundTasks
import CoreData
import UIKit
/**
* iOS implementation of Daily Notification Plugin
@@ -608,6 +609,64 @@ public class DailyNotificationPlugin: CAPPlugin {
requestNotificationPermissions(call)
}
/**
* Get battery status
*
* Returns battery information including:
* - level: Battery level (0-100)
* - isCharging: Whether device is charging
* - powerState: Power state code
* - isOptimizationExempt: Whether app is exempt from battery optimization (iOS: always false)
*
* Equivalent to Android's getBatteryStatus method.
*/
@objc func getBatteryStatus(_ call: CAPPluginCall) {
print("DNP-PLUGIN: Getting battery status")
// Enable battery monitoring if not already enabled
UIDevice.current.isBatteryMonitoringEnabled = true
// Get battery level (0.0 to 1.0, -1.0 if unknown)
let batteryLevel = UIDevice.current.batteryLevel
let batteryLevelPercent = batteryLevel >= 0 ? Int(batteryLevel * 100) : -1
// Get battery state
let batteryState = UIDevice.current.batteryState
let isCharging = batteryState == .charging || batteryState == .full
// Map battery state to power state code
// 0 = unknown, 1 = unplugged, 2 = charging, 3 = full
let powerState: Int
switch batteryState {
case .unknown:
powerState = 0
case .unplugged:
powerState = 1
case .charging:
powerState = 2
case .full:
powerState = 3
@unknown default:
powerState = 0
}
// iOS doesn't have battery optimization like Android
// Background App Refresh is the closest equivalent, but we can't check it directly
// Return false to indicate we're subject to system power management
let isOptimizationExempt = false
let result: [String: Any] = [
"level": batteryLevelPercent,
"isCharging": isCharging,
"powerState": powerState,
"isOptimizationExempt": isOptimizationExempt
]
print("DNP-PLUGIN: Battery status: level=\(batteryLevelPercent)%, charging=\(isCharging), state=\(powerState)")
call.resolve(result)
}
// MARK: - Private Implementation Methods
private func setupBackgroundTasks() {