diff --git a/src/@awesome-cordova-plugins/plugins/hyper-track/index.ts b/src/@awesome-cordova-plugins/plugins/hyper-track/index.ts index 66d25b1490..398c4384ea 100644 --- a/src/@awesome-cordova-plugins/plugins/hyper-track/index.ts +++ b/src/@awesome-cordova-plugins/plugins/hyper-track/index.ts @@ -1,6 +1,8 @@ import { Injectable } from '@angular/core'; import { AwesomeCordovaNativePlugin, Cordova, Plugin } from '@awesome-cordova-plugins/core'; +const hypertrackIonicPluginVersion = "0.2.0" +// Minimal cordova-plugin-hypertrack-v3 version: 0.5.0 @Plugin({ pluginName: 'cordova-plugin-hypertrack-v3', plugin: 'cordova-plugin-hypertrack-v3', @@ -39,6 +41,9 @@ interface FailureHandler { interface SuccessHandler { (): any; } +interface LocationReceiver { + (location: CordovaLatestLocationResult): any; +} // SDK instance that exposed from Cordova utilizes usage of callbacks, so we // wrap it with adapter to avoid mix of callbacks and Promises @@ -64,17 +69,23 @@ interface HyperTrackCordova { syncDeviceSettings(success: SuccessHandler, error: FailureHandler): void; start(success: SuccessHandler, error: FailureHandler): void; stop(success: SuccessHandler, error: FailureHandler): void; + getLatestLocation(success: LocationReceiver, error: FailureHandler): void; + getCurrentLocation(success: LocationReceiver, error: FailureHandler): void; } export class CoordinatesValidationError extends Error {} /** Wrapper class for passing spatial geoposition as a geotag's expected location */ export class Coordinates { - constructor(latitude: number, longitude: number) { + constructor(public latitude: number, public longitude: number) { if (latitude < -90.0 || latitude > 90.0 || longitude < -180.0 || longitude > 180.0) { - throw new CoordinatesValidationError('latitude and longitude should be of correct valaues'); + throw new CoordinatesValidationError('latitude and longitude should be of correct values'); } } + + public toString = (): string => { + return JSON.stringify(this); + } } /** A blocker is an obstacle that needs to be resolved to achieve reliable tracking. */ @@ -89,6 +100,40 @@ export interface Blocker { resolve: () => void; } +export type CordovaLatestLocationResult = { + type: "location", + location: Coordinates, +} | { + type: "outage", + outage: { + code: number, + name: keyof typeof Outage + } +} + +export type LocationResult = { + type: LocationResultType.LOCATION, + value: Coordinates +} | +{ + type: LocationResultType.OUTAGE, + value: Outage +} + +export enum LocationResultType { + LOCATION, OUTAGE +} + +export enum Outage { + MISSING_LOCATION_PERMISSION, + MISSING_ACTIVITY_PERMISSION, + LOCATION_SERVICE_DISABLED, + NOT_TRACKING, + START_HAS_NOT_FINISHED, + NO_GPS_SIGNAL, + RESTART_REQUIRED +} + /** * @usage * ```typescript @@ -130,6 +175,7 @@ export class HyperTrack { * @see {@link https://dashboard.hypertrack.com/setup}. */ static initialize(publishableKey: string): Promise { + console.log(`Hypertrack Ionic plugin version ${hypertrackIonicPluginVersion}`) return new Promise((resolve, reject) => { new HyperTrackPlugin() .initialize(publishableKey) @@ -286,5 +332,49 @@ export class HyperTrack { }); } + /** + * Resolves latest device location that was sent by the SDK. + * Only available for Android platform. + * */ + getLatestLocation(): Promise { + return new Promise((resolve, reject) => { + this.cordovaInstanceHandle.getLatestLocation( + locationResult => resolve(this.handleLocationResult(locationResult)), + err => reject(err) + ); + }); + } + + /** + * Resolves latest device location from system location provider. + * Only available for Android platform. + * */ + getCurrentLocation(): Promise { + return new Promise((resolve, reject) => { + this.cordovaInstanceHandle.getCurrentLocation( + locationResult => resolve(this.handleLocationResult(locationResult)), + err => reject(err) + ); + }); + } + + private handleLocationResult(locationResult: CordovaLatestLocationResult): LocationResult { + switch (locationResult.type) { + case "location": { + return { + type: LocationResultType.LOCATION, + value: locationResult.location + } + } + case "outage": { + const outage = Outage[locationResult.outage.name] + return { + type: LocationResultType.OUTAGE, + value: outage + } + } + } + } + private constructor(private cordovaInstanceHandle: HyperTrackCordova) {} }