Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/mfmalho/ember-battery

:battery: Battery status API packaged as a Service
https://github.com/mfmalho/ember-battery

battery-information battery-level ember ember-addon

Last synced: 2 days ago
JSON representation

:battery: Battery status API packaged as a Service

Awesome Lists containing this project

README

        

🔋 ember-battery [![Ember Observer Score](https://emberobserver.com/badges/ember-battery.svg)](https://emberobserver.com/addons/ember-battery)
==============================================================================

Battery status API packaged as a Service

## Usage

Inject the `battery` service in your classes and access its api:

```js
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:

```hbs
{{#if this.battery.isSupported}}

{{this.battery.levelPercentage}}% {{fa-icon this.iconName}}

{{#if this.battery.isCharging}}

Your battery is charging!


{{/if}}

{{#if this.isTooLow}}

Your battery is too low!


{{/if}}

Discharging Time: {{this.battery.dischargingTime}}


Charging Time: {{this.battery.chargingTime}}


{{else}}

Your browser does not support battery status API.


{{/if}}

```

Because the properties are `@tracked`, the templates and getters will update accordingly when the properties change.

## API

The battery service has the following properties:

* `isSupported` - A Boolean value indicating whether or not the [supports the battery status API](https://caniuse.com/battery-status).
* `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 %

Compatibility
------------------------------------------------------------------------------

* Ember.js v3.16 or above
* Ember CLI v2.13 or above
* Node.js v10 or above

Installation
------------------------------------------------------------------------------

```
ember install ember-battery
```

Contributing
------------------------------------------------------------------------------

See the [Contributing](CONTRIBUTING.md) guide for details.

License
------------------------------------------------------------------------------

This project is licensed under the [MIT License](LICENSE.md).