From a4df21e99cb914ddb1226d4493b004381c516e9d Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Sun, 6 Mar 2016 15:43:13 -0500 Subject: [PATCH 1/4] feat(plugin): add device motion plugin --- src/plugins/devicemotion.ts | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 src/plugins/devicemotion.ts diff --git a/src/plugins/devicemotion.ts b/src/plugins/devicemotion.ts new file mode 100644 index 000000000..a49bb1044 --- /dev/null +++ b/src/plugins/devicemotion.ts @@ -0,0 +1,26 @@ +import {Plugin, Cordova} from './plugin'; + +/** + * Requires Cordova plugin: `cordova-plugin-device-motion`. For more info, please see the [Device Motion docs](https://github.com/apache/cordova-plugin-device-motion). + * + * ``` + * cordova plugin add https://github.com/apache/cordova-plugin-device-motion.git + * ```` + * + * @usage + * ```js + * ``` + */ +@Plugin({ + plugin: 'https://github.com/apache/cordova-plugin-device-motion.git', + pluginRef: 'navigator.accelerometer' +}) +export class DeviceMotion { + + // 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 + +} From 7f7ba3b52abc523c96114eb749608d303f7e1635 Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Sun, 6 Mar 2016 15:54:55 -0500 Subject: [PATCH 2/4] feat(plugin): add functionality to device motion plugin --- src/plugins/devicemotion.ts | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) diff --git a/src/plugins/devicemotion.ts b/src/plugins/devicemotion.ts index a49bb1044..4ea6ad3d7 100644 --- a/src/plugins/devicemotion.ts +++ b/src/plugins/devicemotion.ts @@ -1,5 +1,38 @@ import {Plugin, Cordova} from './plugin'; +export interface accelerationData { + + /** + * Amount of acceleration on the x-axis. (in m/s^2) + */ + x : number, + + /** + * Amount of acceleration on the y-axis. (in m/s^2) + */ + y : number, + + /** + * Amount of acceleration on the z-axis. (in m/s^2) + */ + z : number, + + /** + * Creation timestamp in milliseconds. + */ + timestamp : any + +} + +export interface accelerometerOptions { + + /** + * Requested period of calls to accelerometerSuccess with acceleration data in Milliseconds. Default: 10000 + */ + frequency? : number + +} + /** * Requires Cordova plugin: `cordova-plugin-device-motion`. For more info, please see the [Device Motion docs](https://github.com/apache/cordova-plugin-device-motion). * @@ -17,10 +50,45 @@ import {Plugin, Cordova} from './plugin'; }) export class DeviceMotion { + /** + * Get the current acceleration along the x, y, and z axes. + * @returns {Promise} Returns object with x, y, z, and timestamp properties + */ + @Cordova() + static getCurrentAcceleration () : 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) => {}); + } + + /** + * + * @param options + * @returns {Observable} + */ + @Cordova({ + successIndex: 0, + errorIndex: 1 + }) + static watchAcceleration (options? : accelerometerOptions) : 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 Acceleration referenced by the watchID parameter. + * @param watchID The ID returned by watchAcceleration method + */ + @Cordova({ + sync: true + }) + static clearWatch(watchID : any) : void {} } From 5fda6d49cc6ccb6be3772522b356bc87ec957fad Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Sun, 6 Mar 2016 16:17:07 -0500 Subject: [PATCH 3/4] fix: fix callback order --- src/plugins/devicemotion.ts | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/plugins/devicemotion.ts b/src/plugins/devicemotion.ts index 4ea6ad3d7..6f9b0158f 100644 --- a/src/plugins/devicemotion.ts +++ b/src/plugins/devicemotion.ts @@ -70,8 +70,7 @@ export class DeviceMotion { * @returns {Observable} */ @Cordova({ - successIndex: 0, - errorIndex: 1 + callbackOrder: 'reverse' }) static watchAcceleration (options? : accelerometerOptions) : Promise { // This Promise is replaced by one from the @Cordova decorator that wraps From d17f62d82f29528ccbafc00ed2d38c6e0e3462ed Mon Sep 17 00:00:00 2001 From: Ibrahim Hadeed Date: Tue, 8 Mar 2016 13:18:06 -0500 Subject: [PATCH 4/4] fix(devicemotion): combine watch and clear into one function convert the watch function to an observable function that handles cancellation too --- src/plugins/devicemotion.ts | 53 +++++++++++++++++++++++++------------ 1 file changed, 36 insertions(+), 17 deletions(-) diff --git a/src/plugins/devicemotion.ts b/src/plugins/devicemotion.ts index 6f9b0158f..4196080bf 100644 --- a/src/plugins/devicemotion.ts +++ b/src/plugins/devicemotion.ts @@ -1,4 +1,5 @@ import {Plugin, Cordova} from './plugin'; +import {Observable} from "rxjs/Observable"; export interface accelerationData { @@ -36,12 +37,27 @@ export interface accelerometerOptions { /** * Requires Cordova plugin: `cordova-plugin-device-motion`. For more info, please see the [Device Motion docs](https://github.com/apache/cordova-plugin-device-motion). * - * ``` + * ```shell * cordova plugin add https://github.com/apache/cordova-plugin-device-motion.git * ```` * * @usage - * ```js + * ```ts + * + * // Get the device current acceleration + * DeviceMotion.getCurrentAcceleration().then( + * acceleration => console.log(acceleration), + * error => console.log(error) + * ); + * + * // Watch device acceleration + * var subscription = DeviceMotion.watchPosition().subscribe(acceleration => { + * console.log(acceleration); + * }); + * + * // Stop watch + * subscription.unsubscribe(); + * * ``` */ @Plugin({ @@ -52,6 +68,7 @@ export class DeviceMotion { /** * Get the current acceleration along the x, y, and z axes. + * * @returns {Promise} Returns object with x, y, z, and timestamp properties */ @Cordova() @@ -65,29 +82,31 @@ export class DeviceMotion { } /** + * Watch the device acceleration. Clear the watch by unsubscribing from the observable. * + * ```ts + * // Watch device acceleration + * var subscription = DeviceMotion.watchPosition().subscribe(acceleration => { + * console.log(acceleration); + * }); + * + * // Stop watch + * subscription.unsubscribe(); + * ``` * @param options * @returns {Observable} */ @Cordova({ - callbackOrder: 'reverse' + callbackOrder: 'reverse', + observable: true, + clearFunction: 'clearWatch' }) - static watchAcceleration (options? : accelerometerOptions) : Promise { - // This Promise is replaced by one from the @Cordova decorator that wraps + static watchAcceleration (options? : accelerometerOptions) : 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 PrObservableomise, 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 Acceleration referenced by the watchID parameter. - * @param watchID The ID returned by watchAcceleration method - */ - @Cordova({ - sync: true - }) - static clearWatch(watchID : any) : void {} - }