feat(ios): Enhance battery optimization and notification management
Description: - Add battery status and power state monitoring - Implement adaptive scheduling based on battery levels - Add maintenance worker for background tasks - Enhance logging with structured DailyNotificationLogger - Add configuration management with DailyNotificationConfig - Define constants in DailyNotificationConstants - Improve error handling and recovery mechanisms Testing: - Add comprehensive test coverage for battery optimization - Add test coverage for power state management - Add test coverage for maintenance tasks - Add test coverage for configuration management - Add test coverage for constants validation Documentation: - Add comprehensive file-level documentation - Add method-level documentation - Add test documentation - Add configuration documentation This commit improves the iOS implementation's reliability and battery efficiency by adding robust error handling, logging, and configuration management to make the plugin more maintainable and debuggable.
This commit is contained in:
153
ios/Plugin/DailyNotificationPowerManager.swift
Normal file
153
ios/Plugin/DailyNotificationPowerManager.swift
Normal file
@@ -0,0 +1,153 @@
|
||||
/**
|
||||
* DailyNotificationPowerManager.swift
|
||||
* Daily Notification Plugin for Capacitor
|
||||
*
|
||||
* Manages power state and battery optimization for notifications
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
import UIKit
|
||||
import UserNotifications
|
||||
|
||||
/// Manages power state and battery optimization for the notification plugin
|
||||
public class DailyNotificationPowerManager {
|
||||
/// Shared instance for singleton access
|
||||
public static let shared = DailyNotificationPowerManager()
|
||||
|
||||
private var batteryLevel: Float = 1.0
|
||||
private var isCharging = false
|
||||
private var lastBatteryCheck: Date = Date()
|
||||
private var powerState: UIDevice.BatteryState = .unknown
|
||||
private var adaptiveSchedulingEnabled = true
|
||||
|
||||
private init() {
|
||||
setupBatteryMonitoring()
|
||||
}
|
||||
|
||||
private func setupBatteryMonitoring() {
|
||||
UIDevice.current.isBatteryMonitoringEnabled = true
|
||||
updateBatteryStatus()
|
||||
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(batteryLevelDidChange),
|
||||
name: UIDevice.batteryLevelDidChangeNotification,
|
||||
object: nil
|
||||
)
|
||||
|
||||
NotificationCenter.default.addObserver(
|
||||
self,
|
||||
selector: #selector(batteryStateDidChange),
|
||||
name: UIDevice.batteryStateDidChangeNotification,
|
||||
object: nil
|
||||
)
|
||||
}
|
||||
|
||||
@objc private func batteryLevelDidChange() {
|
||||
updateBatteryStatus()
|
||||
}
|
||||
|
||||
@objc private func batteryStateDidChange() {
|
||||
updateBatteryStatus()
|
||||
}
|
||||
|
||||
private func updateBatteryStatus() {
|
||||
batteryLevel = UIDevice.current.batteryLevel
|
||||
isCharging = UIDevice.current.batteryState == .charging
|
||||
powerState = UIDevice.current.batteryState
|
||||
lastBatteryCheck = Date()
|
||||
|
||||
DailyNotificationLogger.shared.log(
|
||||
.debug,
|
||||
"Battery status updated: \(Int(batteryLevel * 100))% (\(isCharging ? "charging" : "not charging"))"
|
||||
)
|
||||
}
|
||||
|
||||
/// Gets the current battery status
|
||||
/// - Returns: Dictionary containing battery information
|
||||
public func getBatteryStatus() -> [String: Any] {
|
||||
return [
|
||||
"level": Int(batteryLevel * 100),
|
||||
"isCharging": isCharging,
|
||||
"lastCheck": lastBatteryCheck.timeIntervalSince1970,
|
||||
"powerState": getPowerStateString()
|
||||
]
|
||||
}
|
||||
|
||||
/// Gets the current power state
|
||||
/// - Returns: Dictionary containing power state information
|
||||
public func getPowerState() -> [String: Any] {
|
||||
return [
|
||||
"powerState": getPowerStateString(),
|
||||
"adaptiveScheduling": adaptiveSchedulingEnabled,
|
||||
"batteryLevel": Int(batteryLevel * 100),
|
||||
"isCharging": isCharging,
|
||||
"lastCheck": lastBatteryCheck.timeIntervalSince1970
|
||||
]
|
||||
}
|
||||
|
||||
/// Sets whether adaptive scheduling is enabled
|
||||
/// - Parameter enabled: Whether to enable adaptive scheduling
|
||||
public func setAdaptiveScheduling(_ enabled: Bool) {
|
||||
adaptiveSchedulingEnabled = enabled
|
||||
DailyNotificationLogger.shared.log(
|
||||
.info,
|
||||
"Adaptive scheduling \(enabled ? "enabled" : "disabled")"
|
||||
)
|
||||
}
|
||||
|
||||
/// Gets the appropriate scheduling interval based on battery level
|
||||
/// - Returns: TimeInterval for scheduling
|
||||
public func getSchedulingInterval() -> TimeInterval {
|
||||
guard adaptiveSchedulingEnabled else {
|
||||
return DailyNotificationConfig.SchedulingIntervals.normal
|
||||
}
|
||||
|
||||
let batteryPercentage = Int(batteryLevel * 100)
|
||||
|
||||
switch batteryPercentage {
|
||||
case ..<DailyNotificationConfig.BatteryThresholds.critical:
|
||||
return DailyNotificationConfig.SchedulingIntervals.critical
|
||||
case ..<DailyNotificationConfig.BatteryThresholds.low:
|
||||
return DailyNotificationConfig.SchedulingIntervals.low
|
||||
case ..<DailyNotificationConfig.BatteryThresholds.medium:
|
||||
return DailyNotificationConfig.SchedulingIntervals.medium
|
||||
default:
|
||||
return DailyNotificationConfig.SchedulingIntervals.normal
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the appropriate wake lock duration based on battery level
|
||||
/// - Returns: TimeInterval for wake lock
|
||||
public func getWakeLockDuration() -> TimeInterval {
|
||||
let batteryPercentage = Int(batteryLevel * 100)
|
||||
|
||||
switch batteryPercentage {
|
||||
case ..<DailyNotificationConfig.BatteryThresholds.critical:
|
||||
return DailyNotificationConfig.WakeLockDurations.critical
|
||||
case ..<DailyNotificationConfig.BatteryThresholds.low:
|
||||
return DailyNotificationConfig.WakeLockDurations.low
|
||||
default:
|
||||
return DailyNotificationConfig.WakeLockDurations.normal
|
||||
}
|
||||
}
|
||||
|
||||
private func getPowerStateString() -> String {
|
||||
switch powerState {
|
||||
case .charging:
|
||||
return "CHARGING"
|
||||
case .full:
|
||||
return "FULL"
|
||||
case .unplugged:
|
||||
return "UNPLUGGED"
|
||||
case .unknown:
|
||||
return "UNKNOWN"
|
||||
@unknown default:
|
||||
return "UNKNOWN"
|
||||
}
|
||||
}
|
||||
|
||||
deinit {
|
||||
NotificationCenter.default.removeObserver(self)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user