Battery status API packaged as a Service
Inject the battery
service in your classes and access its api:
export default class ApplicationController extends Controller {
@service battery;
get isTooLow() {
return !this.battery.isCharging && this.battery.level < 0.2;
}
get iconName() {
let level = this.battery.level;
if (level < 0.02) {
return 'battery-0';
} else if (level < 0.25) {
return 'battery-1';
} else if (level < 0.5) {
return 'battery-2';
} else if (level < 0.75) {
return 'battery-3';
} else {
return 'battery-4';
}
}
}
Or use them directly in the templates:
Because the properties are @tracked
, the templates and getters will update accordingly when the properties change.
The battery service has the following properties:
isSupported
- A Boolean value indicating whether or not the supports the battery status API.isCharging
- A Boolean value indicating whether or not the battery is currently being charged.level
- A number representing the system's battery charge level scaled to a value between 0.0 and 1.0.chargingTime
- A number representing the remaining time in seconds until the battery is fully charged, or 0 if the battery is already fully charged.dischargingTime
- A number representing the remaining time in seconds until the battery is completely discharged and the system will suspend.levelPercentage
- A number representing the system's battery charge level scaled to a value between 0 % and 100 %
- Ember.js v3.16 or above
- Ember CLI v2.13 or above
- Node.js v10 or above
ember install ember-battery
See the Contributing guide for details.
This project is licensed under the MIT License.