mirror of
https://github.com/danielsogl/awesome-cordova-plugins.git
synced 2025-02-07 23:03:19 +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";
|
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).
|
* 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
|
* @usage
|
||||||
* ```js
|
* ```js
|
||||||
*
|
* // watch change in battery status
|
||||||
* BatteryStatus.onChange().subscribe(
|
* let subscription = BatteryStatus.onChange().subscribe(
|
||||||
* status => {
|
* status => {
|
||||||
*
|
* console.log(status.level, status.isPlugged);
|
||||||
* }
|
* }
|
||||||
* );
|
* );
|
||||||
*
|
*
|
||||||
|
* // stop watch
|
||||||
|
* subscription.unsubscribe();
|
||||||
|
*
|
||||||
* ```
|
* ```
|
||||||
*/
|
*/
|
||||||
@Plugin({
|
@Plugin({
|
||||||
@ -27,15 +30,52 @@ import {Observable} from "rxjs/Observable";
|
|||||||
export class BatteryStatus {
|
export class BatteryStatus {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Watches the change in battery level
|
* Watch the change in battery level
|
||||||
* @returns {Observable} Returns an observable that pushes the new battery level
|
* @returns {Observable} Returns an observable that pushes a status object
|
||||||
*/
|
*/
|
||||||
static onChange () : Observable<any> {
|
static onChange () : Observable<StatusObject> {
|
||||||
return new Observable(observer => {
|
return getEventObservable("batterylevel");
|
||||||
let callback = (status : any) => observer.next(status);
|
}
|
||||||
window.addEventListener("batterystatus", callback, false);
|
|
||||||
return () => window.removeEventListener("batterystatus", callback, false);
|
/**
|
||||||
});
|
* 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