fluttercommunity / fluttercommunity/plus_plugins
[Request]: Support tracking all battery aspects
- Lenguaje dominante
- Dart
- Estrellas
- 1.9k
- Forks
- 1.3k
- Métricas de merge de PR
- Sin PR fusionados en 30 d
Descripción
### Plugin
battery_plus
### Use case
At the moment, there is no option to track changes in battery level or any other aspect. However, for many apps, it would be quite reasonable to react to such changes, by displaying a snackbar message or sending analytics data.
iOS provides support for this functionality:
[Documentation for level notification](https://developer.apple.com/documentation/uikit/uidevice/batteryleveldidchangenotification?language=objc)
[Documentation for power save mode notification](https://developer.apple.com/documentation/foundation/nsprocessinfopowerstatedidchangenotification)
```swift
Publishers.Merge3(
NotificationCenter.default.publisher(for: UIDevice.batteryLevelDidChangeNotification),
NotificationCenter.default.publisher(for: UIDevice.batteryStateDidChangeNotification),
NotificationCenter.default.publisher(for: NSNotification.Name.NSProcessInfoPowerStateDidChange)
)
.sink { _ in self.onBatteryInfoChanged() }
```
Android also supports monitoring battery level changes:
[Documentation](https://developer.android.com/training/monitoring-device-state/battery-monitoring)
[Power save mode changed notification](https://developer.android.com/reference/android/os/PowerManager#ACTION_POWER_SAVE_MODE_CHANGED)
```kotlin
applicationContext?.registerReceiver(
this,
IntentFilter().apply {
addAction(Intent.ACTION_BATTERY_CHANGED)
addAction(Intent.ACTION_BATTERY_LOW)
addAction(PowerManager.ACTION_POWER_SAVE_MODE_CHANGED)
}
)
```
I’m not entirely sure about other platforms, but I would expect similar functionality to be available.
### Proposal
```dart
class BatteryInfo {
final double level;
final bool isLow;
final bool isInBatterySaveMode;
final BatteryState state;
}
battery.onBatteryInfoChanged.listen((BatteryInfo info) {
// Do something with new info
});
```
I actually already have an implementation for iOS and Android and would be happy to contribute if this proposal is considered useful.
@mhadaily cc
Guía de contribución
Línea de trabajo
Revisa el plugin battery_plus y las API de notificaciones de iOS y Android enlazadas en el issue, comenzando por la implementación existente para iOS y Android a la que hace referencia el autor. Confirma las plataformas compatibles y los campos de BatteryInfo y, después, verifica que los cambios en el nivel de batería, el estado de la batería y el ahorro de energía se entreguen a través del stream propuesto.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- dart, flutter, kotlin, swift
- Área
- mobile
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 4/5
- Tiempo estimado
- 3-5 días
- Estado de actividad
- Tranquilo
- Claridad
- Bastante claro
- Aptitud para principiantes
- 48/100