aio-capacitor 2.2.3 → 2.2.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/index.d.ts +50 -21
  2. package/index.js +94 -58
  3. package/package.json +1 -1
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { LocationData } from 'aio-capacitor-bglocation';
1
2
  export type I_speak = {
2
3
  text: string;
3
4
  lang?: 'fa-IR' | 'en-US';
@@ -133,26 +134,6 @@ declare class Logs {
133
134
  status?: 'error' | 'success' | 'warning' | 'info';
134
135
  }) => I_logItem[];
135
136
  }
136
- export declare class Tracker {
137
- private watchId;
138
- onSuccess: (coord: {
139
- latitude: number;
140
- longitude: number;
141
- }) => void;
142
- onFailed: (message: string) => void;
143
- onLog: ((log: I_logItem) => void) | undefined;
144
- constructor(p: {
145
- onSuccess: (coord: {
146
- latitude: number;
147
- longitude: number;
148
- }) => void;
149
- onFailed: (message: string) => void;
150
- onLog?: (log: I_logItem) => void;
151
- });
152
- start(): void;
153
- stop(): Promise<void>;
154
- isTracking(): boolean;
155
- }
156
137
  export type I_notifItem = {
157
138
  id: number;
158
139
  title: string;
@@ -199,4 +180,52 @@ export declare class NotificationService {
199
180
  }) => Promise<I_notifItem[]>;
200
181
  private save;
201
182
  }
202
- export {};
183
+ export type { LocationData };
184
+ export declare class Tracker {
185
+ token: string;
186
+ url: string;
187
+ listener: {
188
+ remove: () => Promise<void>;
189
+ } | null;
190
+ cap: AIOCapacitor;
191
+ onAdd: (data: LocationData) => void;
192
+ changeStatus: (p: {
193
+ hasPermission?: boolean;
194
+ active?: boolean;
195
+ pos?: [number, number];
196
+ }) => void;
197
+ interval: any;
198
+ constructor(p: {
199
+ token: string;
200
+ url: string;
201
+ onAdd: (data: LocationData) => void;
202
+ changeStatus: (p: {
203
+ hasPermission?: boolean;
204
+ active?: boolean;
205
+ }) => void;
206
+ });
207
+ init: () => Promise<void>;
208
+ isServiceActive: () => Promise<boolean>;
209
+ getPermissions: () => Promise<boolean>;
210
+ start: () => Promise<boolean>;
211
+ stop: () => Promise<boolean>;
212
+ toggle: () => Promise<boolean>;
213
+ addListener: (callback: (v: LocationData) => void) => Promise<any>;
214
+ getMock(): {
215
+ lat: number;
216
+ lng: number;
217
+ sendSuccess: boolean;
218
+ sendError: string;
219
+ }[];
220
+ getCurrentLocation: () => Promise<{
221
+ success: boolean;
222
+ latitude?: number;
223
+ longitude?: number;
224
+ accuracy?: number;
225
+ source?: "network" | "gps" | "cache";
226
+ error?: string;
227
+ }>;
228
+ changePos: (pos: [number, number]) => Promise<void>;
229
+ goToCurrent: () => Promise<void>;
230
+ destroy: () => void;
231
+ }
package/index.js CHANGED
@@ -20,7 +20,8 @@ import { Share } from '@capacitor/share';
20
20
  import { LocalNotifications } from '@capacitor/local-notifications';
21
21
  import { KeepAwake } from '@capacitor-community/keep-awake';
22
22
  import { FolderPicker } from 'aio-capacitor-folder-picker';
23
- import { FileClass, GenerateId } from 'aio-class';
23
+ import { FileClass, GenerateId, GetRandomNumber } from 'aio-class';
24
+ import { Example } from 'aio-capacitor-bglocation';
24
25
  export class AIOCapacitor {
25
26
  constructor(p) {
26
27
  this.logs = new Logs({
@@ -459,52 +460,6 @@ class Logs {
459
460
  this.save = p.save;
460
461
  }
461
462
  }
462
- export class Tracker {
463
- constructor(p) {
464
- this.watchId = null;
465
- this.onSuccess = p.onSuccess;
466
- this.onFailed = p.onFailed;
467
- this.onLog = p.onLog || (() => { });
468
- }
469
- start() {
470
- if (this.watchId) {
471
- return;
472
- }
473
- Geolocation.watchPosition({
474
- enableHighAccuracy: true,
475
- timeout: 10000,
476
- maximumAge: 3000
477
- }, (position, err) => {
478
- if (err) {
479
- this.onFailed(err.message || 'Location Error');
480
- return;
481
- }
482
- if (!position) {
483
- return;
484
- }
485
- this.onSuccess({
486
- latitude: position.coords.latitude,
487
- longitude: position.coords.longitude
488
- });
489
- }).then((id) => {
490
- this.watchId = id;
491
- });
492
- }
493
- stop() {
494
- return __awaiter(this, void 0, void 0, function* () {
495
- if (!this.watchId) {
496
- return;
497
- }
498
- yield Geolocation.clearWatch({
499
- id: this.watchId
500
- });
501
- this.watchId = null;
502
- });
503
- }
504
- isTracking() {
505
- return !!this.watchId;
506
- }
507
- }
508
463
  export class NotificationService {
509
464
  constructor(actionCallBack) {
510
465
  this.notificationFile = 'notifications.json';
@@ -719,27 +674,108 @@ export class NotificationService {
719
674
  this.init(actionCallBack);
720
675
  }
721
676
  }
722
- class AppStorage {
723
- constructor(props) {
677
+ export class Tracker {
678
+ constructor(p) {
679
+ this.listener = null;
724
680
  this.cap = new AIOCapacitor();
725
- this.save = (data) => __awaiter(this, void 0, void 0, function* () {
681
+ this.init = () => __awaiter(this, void 0, void 0, function* () {
682
+ const hasPermission = yield this.getPermissions();
683
+ this.changeStatus({ hasPermission });
684
+ this.interval = window.setInterval(() => __awaiter(this, void 0, void 0, function* () {
685
+ const active = yield this.isServiceActive();
686
+ this.changeStatus({ active });
687
+ }), 5000);
688
+ });
689
+ this.isServiceActive = () => __awaiter(this, void 0, void 0, function* () {
690
+ const res = yield Example.isServiceActive();
691
+ return res.active;
692
+ });
693
+ this.getPermissions = () => __awaiter(this, void 0, void 0, function* () {
694
+ const a = yield Example.getLocationPermission();
695
+ const b = yield Example.getNotificationPermission();
696
+ return !!a.granted && !!b.granted;
697
+ });
698
+ this.start = () => __awaiter(this, void 0, void 0, function* () {
726
699
  try {
727
- yield this.cap.writeFile(JSON.stringify(data), this.fileName, { location: this.location, utf8: true });
700
+ yield Example.startTracker({ url: this.url, token: this.token });
701
+ if (this.listener !== null) {
702
+ yield this.listener.remove();
703
+ }
704
+ this.listener = yield this.addListener((data) => this.onAdd(data));
705
+ this.changeStatus({ active: true });
706
+ return true;
728
707
  }
729
708
  catch (err) {
730
- alert(`AppStorage save error: ${err.message}`);
709
+ alert("ERROR: " + JSON.stringify(err));
710
+ return false;
731
711
  }
732
712
  });
733
- this.load = (def) => __awaiter(this, void 0, void 0, function* () {
713
+ this.stop = () => __awaiter(this, void 0, void 0, function* () {
734
714
  try {
735
- const data = yield this.cap.readFile(this.fileName, { location: this.location, utf8: true });
715
+ if (this.listener !== null) {
716
+ yield this.listener.remove();
717
+ this.listener = null;
718
+ }
719
+ yield Example.stopTracker();
720
+ this.changeStatus({ active: false });
721
+ return true;
736
722
  }
737
723
  catch (err) {
738
- console.error('storageLoad error:', err);
739
- return def;
724
+ alert("ERROR: " + JSON.stringify(err));
725
+ return false;
726
+ }
727
+ });
728
+ this.toggle = () => __awaiter(this, void 0, void 0, function* () {
729
+ const active = yield this.isServiceActive();
730
+ if (active) {
731
+ return yield this.stop();
732
+ }
733
+ else {
734
+ return yield this.start();
735
+ }
736
+ });
737
+ this.addListener = (callback) => __awaiter(this, void 0, void 0, function* () {
738
+ return yield Example.addListener('tracker', (data) => callback(data));
739
+ });
740
+ this.getCurrentLocation = () => __awaiter(this, void 0, void 0, function* () { return yield this.cap.getLocation(); });
741
+ this.changePos = (pos) => __awaiter(this, void 0, void 0, function* () { return this.changeStatus({ pos }); });
742
+ this.goToCurrent = () => __awaiter(this, void 0, void 0, function* () {
743
+ const { success, error, latitude, longitude } = yield this.getCurrentLocation();
744
+ if (success) {
745
+ this.changeStatus({ pos: [latitude || 0, longitude || 0] });
746
+ }
747
+ else {
748
+ alert(error);
749
+ }
750
+ });
751
+ this.destroy = () => {
752
+ window.clearInterval(this.interval);
753
+ if (this.listener !== null) {
754
+ this.listener.remove();
755
+ this.listener = null;
740
756
  }
757
+ };
758
+ this.token = p.token;
759
+ this.url = p.url;
760
+ this.onAdd = p.onAdd;
761
+ this.changeStatus = p.changeStatus;
762
+ this.init();
763
+ }
764
+ getMock() {
765
+ const testLogs = [
766
+ [35.79163979013101, 51.02188110351563], [35.784955845919164, 51.02874755859376], [35.78384180056804, 51.04797363281251], [35.78161366302545, 51.05072021484376],
767
+ [35.7804995708344, 51.05072021484376], [35.7671292467937, 51.06994628906251], [35.7626719725612, 51.07406616210938], [35.73034926596472, 51.16744995117188],
768
+ [35.7270047886309, 51.17843627929688], [35.72588993164639, 51.20040893554688], [35.70916520463913, 51.31301879882813], [35.70805009803191, 51.33361816406251],
769
+ [35.70805009803191, 51.33499145507813], [35.70470468461301, 51.34597778320313], [35.681282860023565, 51.36932373046876], [35.60092726180499, 51.373443603515625],
770
+ [35.52272633049273, 51.31301879882813], [35.51601984359331, 51.3006591796875], [35.48024245154482, 51.236114501953125], [35.35377612773401, 51.17568969726563],
771
+ [35.01818803935104, 50.88180541992188], [34.95743255328083, 50.86257934570313], [34.862834444032536, 50.855712890625], [34.8211316630575, 50.84197998046876],
772
+ [34.78730308987354, 50.843353271484375], [34.717346725560674, 50.85159301757813], [34.67331156427795, 50.87081909179688], [34.61682196594164, 50.90103149414063],
773
+ [34.61230113572208, 50.90240478515626], [34.55803197344862, 50.95321655273438], [34.49127759454727, 51.02737426757813]
774
+ ];
775
+ return testLogs.map(([lat, lng]) => {
776
+ const rnd = GetRandomNumber(0, 10);
777
+ const sendSuccess = rnd > 3;
778
+ return { lat, lng, sendSuccess, sendError: '' };
741
779
  });
742
- this.fileName = props.fileName;
743
- this.location = props.location;
744
780
  }
745
781
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-capacitor",
3
- "version": "2.2.3",
3
+ "version": "2.2.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",