From 046d17e574eba569478795a754c35e1bffcc9f3d Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Thu, 10 Mar 2016 15:14:55 -0500 Subject: [PATCH 1/4] docs(batterystatus): improve docs --- src/plugins/batterystatus.ts | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/src/plugins/batterystatus.ts b/src/plugins/batterystatus.ts index 1a0dc28de..ea739af4e 100644 --- a/src/plugins/batterystatus.ts +++ b/src/plugins/batterystatus.ts @@ -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); * } * ); * + * // stop watch + * subscription.unsubscribe(); + * * ``` */ @Plugin({ @@ -27,7 +30,7 @@ import {Observable} from "rxjs/Observable"; 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 */ static onChange () : Observable { @@ -38,4 +41,4 @@ export class BatteryStatus { }); } -} +} \ No newline at end of file From e776541ca070cb68558c13b724cebb0237f1f3ab Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Thu, 10 Mar 2016 15:17:51 -0500 Subject: [PATCH 2/4] refactor(batterystatus): add interface for battery status object --- src/plugins/batterystatus.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/plugins/batterystatus.ts b/src/plugins/batterystatus.ts index ea739af4e..81c393977 100644 --- a/src/plugins/batterystatus.ts +++ b/src/plugins/batterystatus.ts @@ -31,9 +31,9 @@ export class BatteryStatus { /** * 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 { + static onChange () : Observable { return new Observable(observer => { let callback = (status : any) => observer.next(status); window.addEventListener("batterystatus", callback, false); @@ -41,4 +41,16 @@ export class BatteryStatus { }); } +} + +interface StatusObject { + /** + * The battery charge percentage + */ + level : number, + + /** + * A boolean that indicates whether the device is plugged in + */ + isPlugged : boolean } \ No newline at end of file From e7a09d901864790b0bfec6c984f49f034ca4dc7f Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Thu, 10 Mar 2016 15:24:04 -0500 Subject: [PATCH 3/4] feat(batterystatus): complete functionality --- src/plugins/batterystatus.ts | 31 ++++++++++++++++++++++++++++--- 1 file changed, 28 insertions(+), 3 deletions(-) diff --git a/src/plugins/batterystatus.ts b/src/plugins/batterystatus.ts index 81c393977..9912ed58e 100644 --- a/src/plugins/batterystatus.ts +++ b/src/plugins/batterystatus.ts @@ -15,7 +15,7 @@ import {Observable} from "rxjs/Observable"; * // watch change in battery status * let subscription = BatteryStatus.onChange().subscribe( * status => { - * console.log(status); + * console.log(status.level, status.isPlugged); * } * ); * @@ -34,10 +34,35 @@ export class BatteryStatus { * @returns {Observable} Returns an observable that pushes a status object */ static onChange () : Observable { + return BatteryStatus.getObservable("batterylevel"); + } + + /** + * Watch when the battery level goes low + * @returns {Observable} Returns an observable that pushes a status object + */ + static onLow () : Observable { + return BatteryStatus.getObservable("batterylow"); + } + + /** + * Watch when the battery level goes to critial + * @returns {Observable} Returns an observable that pushes a status object + */ + static onCritical () : Observable { + return BatteryStatus.getObservable("batterycritical"); + } + + /** + * Wrap the event with an observable + * @param event + * @returns {Observable} + */ + static getObservable (event : string) : Observable { return new Observable(observer => { let callback = (status : any) => observer.next(status); - window.addEventListener("batterystatus", callback, false); - return () => window.removeEventListener("batterystatus", callback, false); + window.addEventListener(event, callback, false); + return () => window.removeEventListener(event, callback, false); }); } From 35bfe40d9e9d00887c49f9aa7763b112a2373f37 Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Thu, 10 Mar 2016 15:27:01 -0500 Subject: [PATCH 4/4] refactor(batterystatus): extract getObservable() from class extracted the getObservable() function from class so it doesn't show as a method (in docs and auto-complete) --- src/plugins/batterystatus.ts | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/plugins/batterystatus.ts b/src/plugins/batterystatus.ts index 9912ed58e..510eed59c 100644 --- a/src/plugins/batterystatus.ts +++ b/src/plugins/batterystatus.ts @@ -34,7 +34,7 @@ export class BatteryStatus { * @returns {Observable} Returns an observable that pushes a status object */ static onChange () : Observable { - return BatteryStatus.getObservable("batterylevel"); + return getEventObservable("batterylevel"); } /** @@ -42,7 +42,7 @@ export class BatteryStatus { * @returns {Observable} Returns an observable that pushes a status object */ static onLow () : Observable { - return BatteryStatus.getObservable("batterylow"); + return getEventObservable("batterylow"); } /** @@ -50,20 +50,7 @@ export class BatteryStatus { * @returns {Observable} Returns an observable that pushes a status object */ static onCritical () : Observable { - return BatteryStatus.getObservable("batterycritical"); - } - - /** - * Wrap the event with an observable - * @param event - * @returns {Observable} - */ - static getObservable (event : string) : Observable { - return new Observable(observer => { - let callback = (status : any) => observer.next(status); - window.addEventListener(event, callback, false); - return () => window.removeEventListener(event, callback, false); - }); + return getEventObservable("batterycritical"); } } @@ -78,4 +65,17 @@ interface StatusObject { * 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 { + return new Observable(observer => { + let callback = (status : any) => observer.next(status); + window.addEventListener(event, callback, false); + return () => window.removeEventListener(event, callback, false); + }); } \ No newline at end of file