From 9766aab59ffcfba76e93cb265a62fa35436d5722 Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Sun, 6 Mar 2016 16:06:43 -0500 Subject: [PATCH 1/4] feat(plugin): add device orientation plugin --- src/plugins/deviceorientation.ts | 99 ++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 src/plugins/deviceorientation.ts diff --git a/src/plugins/deviceorientation.ts b/src/plugins/deviceorientation.ts new file mode 100644 index 000000000..b5b8c3f5c --- /dev/null +++ b/src/plugins/deviceorientation.ts @@ -0,0 +1,99 @@ +import {Plugin, Cordova} from './plugin'; + +export interface CompassHeading { + + /** + * The heading in degrees from 0-359.99 at a single moment in time. (Number) + */ + magneticHeading : number, + + /** + * The heading relative to the geographic North Pole in degrees 0-359.99 at a single moment in time. A negative value indicates that the true heading can't be determined. (Number) + */ + trueHeading : number, + + /** + * The deviation in degrees between the reported heading and the true heading. (Number) + */ + headingAccuracy : number, + + /** + * The time at which this heading was determined. (DOMTimeStamp) + */ + timestamp : any + +} + +export interface CompassOptions { + + /** + * How often to retrieve the compass heading in milliseconds. (Number) (Default: 100) + */ + frequency : number, + + /** + * The change in degrees required to initiate a watchHeading success callback. When this value is set, frequency is ignored. (Number) + */ + filter : number + +} + +/** + * Requires Cordova plugin: `cordova-plugin-device-orientation`. For more info, please see the [Device Orientation docs](https://github.com/apache/cordova-plugin-device-orientation). + * + * ``` + * cordova plugin add https://github.com/apache/cordova-plugin-device-orientation.git + * ```` + * + * @usage + * ```js + * ``` + */ +@Plugin({ + plugin: 'https://github.com/apache/cordova-plugin-device-orientation', + pluginRef: 'navigator.compass' +}) +export class DeviceOrientation { + + /** + * Get the current compass heading. + * @returns {Promise} + */ + @Cordova() + static getCurrentHeading() : Promise { + // This Promise is replaced by one from the @Cordova decorator that wraps + // the plugin's callbacks. We provide a dummy one here so TypeScript + // knows that the correct return type is Promise, because there's no way + // for it to know the return type from a decorator. + // See https://github.com/Microsoft/TypeScript/issues/4881 + return new Promise((res, rej) => {}); + } + + /** + * Gets the device's current heading at a regular interval + * @param options + * @returns {Promise} + */ + @Cordova({ + successIndex: 0, + errorIndex: 1 + }) + static watchHeading(options? : CompassOptions) : Promise { + // This Promise is replaced by one from the @Cordova decorator that wraps + // the plugin's callbacks. We provide a dummy one here so TypeScript + // knows that the correct return type is Promise, because there's no way + // for it to know the return type from a decorator. + // See https://github.com/Microsoft/TypeScript/issues/4881 + return new Promise((res, rej) => {}); + } + + /** + * Stop watching the compass referenced by the watch ID parameter. + * @param watchID + */ + @Cordova({ + sync: true + }) + static clearWatch(watchID) : void {} + +} From 5944bd13d31bc137457496ac0e604eefbb4529a0 Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Sun, 6 Mar 2016 16:16:04 -0500 Subject: [PATCH 2/4] fix: fix callback order --- src/plugins/deviceorientation.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/plugins/deviceorientation.ts b/src/plugins/deviceorientation.ts index b5b8c3f5c..66159367e 100644 --- a/src/plugins/deviceorientation.ts +++ b/src/plugins/deviceorientation.ts @@ -75,8 +75,7 @@ export class DeviceOrientation { * @returns {Promise} */ @Cordova({ - successIndex: 0, - errorIndex: 1 + callbackOrder: 'reverse' }) static watchHeading(options? : CompassOptions) : Promise { // This Promise is replaced by one from the @Cordova decorator that wraps From 88553595272bfddf20a64bc4eabd20118792a536 Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Tue, 8 Mar 2016 13:26:55 -0500 Subject: [PATCH 3/4] fix(plugin): combine watch and clearwatch functions --- src/plugins/deviceorientation.ts | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/plugins/deviceorientation.ts b/src/plugins/deviceorientation.ts index 66159367e..1f193f658 100644 --- a/src/plugins/deviceorientation.ts +++ b/src/plugins/deviceorientation.ts @@ -1,4 +1,5 @@ import {Plugin, Cordova} from './plugin'; +import {Observable} from "rxjs/Observable"; export interface CompassHeading { @@ -70,29 +71,24 @@ export class DeviceOrientation { } /** - * Gets the device's current heading at a regular interval + * Get the device current heading at a regular interval + * + * Stop the watch by unsubscribing from the observable * @param options - * @returns {Promise} + * @returns {Observable} */ @Cordova({ - callbackOrder: 'reverse' + callbackOrder: 'reverse', + observable: true, + cancelFunction: 'clearWatch' }) - static watchHeading(options? : CompassOptions) : Promise { - // This Promise is replaced by one from the @Cordova decorator that wraps + static watchHeading(options? : CompassOptions) : Observable { + // This Observable is replaced by one from the @Cordova decorator that wraps // the plugin's callbacks. We provide a dummy one here so TypeScript - // knows that the correct return type is Promise, because there's no way + // knows that the correct return type is Observable, because there's no way // for it to know the return type from a decorator. // See https://github.com/Microsoft/TypeScript/issues/4881 - return new Promise((res, rej) => {}); + return new Observable(observer => {}); } - /** - * Stop watching the compass referenced by the watch ID parameter. - * @param watchID - */ - @Cordova({ - sync: true - }) - static clearWatch(watchID) : void {} - } From e053c945eed95cef39d844b248a3f47d1ca1b42e Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Tue, 8 Mar 2016 13:29:03 -0500 Subject: [PATCH 4/4] docs(plugin): add docs --- src/plugins/deviceorientation.ts | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/plugins/deviceorientation.ts b/src/plugins/deviceorientation.ts index 1f193f658..b9d9c749e 100644 --- a/src/plugins/deviceorientation.ts +++ b/src/plugins/deviceorientation.ts @@ -47,7 +47,20 @@ export interface CompassOptions { * ```` * * @usage - * ```js + * ```ts + * // Get the device current compass heading + * DeviceOrientation.getCurrentHeading().then( + * data => console.log(data), + * error => console.log(error) + * ); + * + * // Watch the device compass heading change + * var subscription = DeviceOrientation.watchHeading().subscribe( + * data => console.log(data) + * ); + * + * // Stop watching heading change + * subscription.unsubscribe(); * ``` */ @Plugin({