From 3b8566df939773a4b9a1604a4af3a60e24cf9aed Mon Sep 17 00:00:00 2001 From: Simone Colazzo <42938441+kitcarson88@users.noreply.github.com> Date: Sat, 8 Jan 2022 14:21:04 +0100 Subject: [PATCH] feat(magnetometer): add magnetometer wrapper (#3887) * feat(magnetometer): add magnetometer wrapper * fix(magnetomer): add wrapper parameters Co-authored-by: Simone Colazzo --- .../plugins/magnetometer/index.ts | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 src/@awesome-cordova-plugins/plugins/magnetometer/index.ts diff --git a/src/@awesome-cordova-plugins/plugins/magnetometer/index.ts b/src/@awesome-cordova-plugins/plugins/magnetometer/index.ts new file mode 100644 index 000000000..4b83e0f85 --- /dev/null +++ b/src/@awesome-cordova-plugins/plugins/magnetometer/index.ts @@ -0,0 +1,87 @@ +import { Injectable } from '@angular/core'; +import { Plugin, Cordova, AwesomeCordovaNativePlugin } from '@awesome-cordova-plugins/core'; +import { Observable } from 'rxjs'; + +export interface MagnetometerReading { + /** + * X reading of magnetometer. (Number) + */ + x: number; + /** + * Y reading of magnetometer. (Number) + */ + y: number; + /** + * Z reading of magnetometer. (Number) + */ + z: number; + /** + * Calculated total - always positive of magnetometer. (Number) + */ + magnitude: number; +} + +/** + * @name Device eMagnetometer + * @description + * Requires Cordova plugin: `cordova-plugin-magnetometer`. For more info, please see the [Device Orientation docs](https://github.com/sdesalas/cordova-plugin-magnetometer). + * + * @usage + * ```typescript + * // MagnetometerReading is an interface for compass + * import { Magnetometer, MagnetometerReading } from '@awesome-cordova-plugins/device-orientation/ngx'; + * + * constructor(private magnetometer: Magnetometer) { } + * + * ... + * + * // Get the device current compass heading + * this.magnetometer.getReading().then( + * (data: MagnetometerReading) => console.log(data), + * (error: any) => console.log(error) + * ); + * + * // Watch the device compass heading change + * var subscription = this.magnetometer.watchReadings().subscribe( + * (data: MagnetometerReading) => console.log(data) + * ); + * + * // Stop watching heading change + * subscription.unsubscribe(); + * ``` + * @interfaces + * MagnetometerReading + */ +@Plugin({ + pluginName: 'Magnetometer', + plugin: 'cordova-plugin-magnetometer', + pluginRef: 'cordova.plugins.magnetometer', + repo: 'https://github.com/sdesalas/cordova-plugin-magnetometer', + platforms: ['Android', 'iOS'], +}) +@Injectable() +export class Magnetometer extends AwesomeCordovaNativePlugin { + /** + * Get the current compass reading. + * @returns {Promise} + */ + @Cordova() + getReading(): Promise { + return; + } + /** + * Get the device current heading at a regular interval + * + * Stop the watch by unsubscribing from the observable + * @param {DeviceOrientationCompassOptions} [options] Options for compass. Frequency and Filter. Optional + * @returns {Observable} Returns an observable that contains the compass heading + */ + @Cordova({ + callbackOrder: 'reverse', + observable: true, + clearFunction: 'stop', + }) + watchReadings(): Observable { + return; + } +}