Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hyper-track): add new functions #4128

Merged
merged 1 commit into from
May 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
94 changes: 92 additions & 2 deletions src/@awesome-cordova-plugins/plugins/hyper-track/index.ts
Original file line number Diff line number Diff line change
@@ -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',
Expand Down Expand Up @@ -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
Expand All @@ -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. */
Expand All @@ -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
Expand Down Expand Up @@ -130,6 +175,7 @@ export class HyperTrack {
* @see {@link https://dashboard.hypertrack.com/setup}.
*/
static initialize(publishableKey: string): Promise<HyperTrack> {
console.log(`Hypertrack Ionic plugin version ${hypertrackIonicPluginVersion}`)
return new Promise((resolve, reject) => {
new HyperTrackPlugin()
.initialize(publishableKey)
Expand Down Expand Up @@ -286,5 +332,49 @@ export class HyperTrack {
});
}

/**
* Resolves latest device location that was sent by the SDK.
* Only available for Android platform.
* */
getLatestLocation(): Promise<LocationResult> {
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<LocationResult> {
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) {}
}