mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-01-19 08:32:52 +08:00
Merge pull request #39 from ihadeed/batterystatus
Complete battery status
This commit is contained in:
commit
9aa436a9c7
@ -2,7 +2,7 @@ import {Plugin} from './plugin';
|
||||
import {Observable} from "rxjs/Observable";
|
||||
|
||||
/**
|
||||
*
|
||||
* @name Battery Status
|
||||
*
|
||||
* Requires Cordova plugin: cordova-plugin-batterystatus. For more info, please see the [BatteryStatus plugin docs](https://github.com/apache/cordova-plugin-battery-status).
|
||||
*
|
||||
@ -12,13 +12,16 @@ import {Observable} from "rxjs/Observable";
|
||||
*
|
||||
* @usage
|
||||
* ```js
|
||||
*
|
||||
* BatteryStatus.onChange().subscribe(
|
||||
* // watch change in battery status
|
||||
* let subscription = BatteryStatus.onChange().subscribe(
|
||||
* status => {
|
||||
*
|
||||
* console.log(status.level, status.isPlugged);
|
||||
* }
|
||||
* );
|
||||
*
|
||||
* // stop watch
|
||||
* subscription.unsubscribe();
|
||||
*
|
||||
* ```
|
||||
*/
|
||||
@Plugin({
|
||||
@ -27,15 +30,52 @@ import {Observable} from "rxjs/Observable";
|
||||
export class BatteryStatus {
|
||||
|
||||
/**
|
||||
* Watches the change in battery level
|
||||
* @returns {Observable} Returns an observable that pushes the new battery level
|
||||
* Watch the change in battery level
|
||||
* @returns {Observable} Returns an observable that pushes a status object
|
||||
*/
|
||||
static onChange () : Observable<any> {
|
||||
return new Observable(observer => {
|
||||
let callback = (status : any) => observer.next(status);
|
||||
window.addEventListener("batterystatus", callback, false);
|
||||
return () => window.removeEventListener("batterystatus", callback, false);
|
||||
});
|
||||
static onChange () : Observable<StatusObject> {
|
||||
return getEventObservable("batterylevel");
|
||||
}
|
||||
|
||||
/**
|
||||
* Watch when the battery level goes low
|
||||
* @returns {Observable<StatusObject>} Returns an observable that pushes a status object
|
||||
*/
|
||||
static onLow () : Observable<StatusObject> {
|
||||
return getEventObservable("batterylow");
|
||||
}
|
||||
|
||||
/**
|
||||
* Watch when the battery level goes to critial
|
||||
* @returns {Observable<StatusObject>} Returns an observable that pushes a status object
|
||||
*/
|
||||
static onCritical () : Observable<StatusObject> {
|
||||
return getEventObservable("batterycritical");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
interface StatusObject {
|
||||
/**
|
||||
* The battery charge percentage
|
||||
*/
|
||||
level : number,
|
||||
|
||||
/**
|
||||
* A boolean that indicates whether the device is plugged in
|
||||
*/
|
||||
isPlugged : boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Wrap the event with an observable
|
||||
* @param event
|
||||
* @returns {Observable}
|
||||
*/
|
||||
function getEventObservable (event : string) : Observable<StatusObject> {
|
||||
return new Observable(observer => {
|
||||
let callback = (status : any) => observer.next(status);
|
||||
window.addEventListener(event, callback, false);
|
||||
return () => window.removeEventListener(event, callback, false);
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user