aio-capacitor 2.2.2 → 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 +55 -24
  2. package/index.js +137 -51
  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';
@@ -65,14 +66,16 @@ export declare class AIOCapacitor {
65
66
  fileName: string;
66
67
  }[])>;
67
68
  openFile: (filePath: string) => Promise<void>;
68
- readFile: (path: string, options?: {
69
+ isFileExist: (path: string, location?: 'Data' | 'Documents' | 'Cache') => Promise<boolean>;
70
+ readFile: (path: string, def: any, options?: {
69
71
  location?: 'Data' | 'Documents' | 'Cache';
70
72
  utf8?: boolean;
73
+ writeDef?: boolean;
71
74
  }) => Promise<any>;
72
- writeFile: (data: string, path: string, options?: {
75
+ writeFile: (data: any, path: string, options?: {
73
76
  location?: 'Data' | 'Documents' | 'Cache';
74
77
  utf8?: boolean;
75
- }) => Promise<any>;
78
+ }) => Promise<string>;
76
79
  save: (data: string | File | Blob, path: string, options?: {
77
80
  location?: 'Data' | 'Documents' | 'Cache';
78
81
  utf8?: boolean;
@@ -131,26 +134,6 @@ declare class Logs {
131
134
  status?: 'error' | 'success' | 'warning' | 'info';
132
135
  }) => I_logItem[];
133
136
  }
134
- export declare class Tracker {
135
- private watchId;
136
- onSuccess: (coord: {
137
- latitude: number;
138
- longitude: number;
139
- }) => void;
140
- onFailed: (message: string) => void;
141
- onLog: ((log: I_logItem) => void) | undefined;
142
- constructor(p: {
143
- onSuccess: (coord: {
144
- latitude: number;
145
- longitude: number;
146
- }) => void;
147
- onFailed: (message: string) => void;
148
- onLog?: (log: I_logItem) => void;
149
- });
150
- start(): void;
151
- stop(): Promise<void>;
152
- isTracking(): boolean;
153
- }
154
137
  export type I_notifItem = {
155
138
  id: number;
156
139
  title: string;
@@ -197,4 +180,52 @@ export declare class NotificationService {
197
180
  }) => Promise<I_notifItem[]>;
198
181
  private save;
199
182
  }
200
- 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({
@@ -198,12 +199,38 @@ export class AIOCapacitor {
198
199
  alert(`Open file error:\n${String(error)}`);
199
200
  }
200
201
  });
201
- this.readFile = (path, options) => __awaiter(this, void 0, void 0, function* () {
202
- const res = yield Filesystem.readFile({ path, directory: this.getDirectory(options === null || options === void 0 ? void 0 : options.location), encoding: (options === null || options === void 0 ? void 0 : options.utf8) ? Encoding.UTF8 : undefined });
203
- return res.data;
202
+ this.isFileExist = (path, location) => __awaiter(this, void 0, void 0, function* () {
203
+ try {
204
+ yield Filesystem.stat({ path, directory: this.getDirectory(location) });
205
+ return true;
206
+ }
207
+ catch (_a) {
208
+ return false;
209
+ }
210
+ });
211
+ this.readFile = (path, def, options) => __awaiter(this, void 0, void 0, function* () {
212
+ const location = options === null || options === void 0 ? void 0 : options.location;
213
+ const isJson = typeof def !== 'string';
214
+ if (!(yield this.isFileExist(path, location))) {
215
+ if (options === null || options === void 0 ? void 0 : options.writeDef) {
216
+ yield this.writeFile(isJson ? JSON.stringify(def) : def, path, { location, utf8: isJson ? true : options === null || options === void 0 ? void 0 : options.utf8 });
217
+ }
218
+ return def;
219
+ }
220
+ const res = yield Filesystem.readFile({
221
+ path, directory: this.getDirectory(location),
222
+ encoding: isJson || (options === null || options === void 0 ? void 0 : options.utf8) ? Encoding.UTF8 : undefined
223
+ });
224
+ return isJson ? JSON.parse(res.data) : res.data;
204
225
  });
205
226
  this.writeFile = (data, path, options) => __awaiter(this, void 0, void 0, function* () {
206
- return Filesystem.writeFile({ path, data, directory: this.getDirectory(options === null || options === void 0 ? void 0 : options.location), encoding: (options === null || options === void 0 ? void 0 : options.utf8) ? Encoding.UTF8 : undefined });
227
+ const res = yield Filesystem.writeFile({
228
+ path,
229
+ data: typeof data === 'string' ? data : JSON.stringify(data),
230
+ directory: this.getDirectory(options === null || options === void 0 ? void 0 : options.location),
231
+ encoding: (options === null || options === void 0 ? void 0 : options.utf8) ? Encoding.UTF8 : undefined
232
+ });
233
+ return res.uri;
207
234
  });
208
235
  this.save = (data, path, options) => __awaiter(this, void 0, void 0, function* () {
209
236
  try {
@@ -433,52 +460,6 @@ class Logs {
433
460
  this.save = p.save;
434
461
  }
435
462
  }
436
- export class Tracker {
437
- constructor(p) {
438
- this.watchId = null;
439
- this.onSuccess = p.onSuccess;
440
- this.onFailed = p.onFailed;
441
- this.onLog = p.onLog || (() => { });
442
- }
443
- start() {
444
- if (this.watchId) {
445
- return;
446
- }
447
- Geolocation.watchPosition({
448
- enableHighAccuracy: true,
449
- timeout: 10000,
450
- maximumAge: 3000
451
- }, (position, err) => {
452
- if (err) {
453
- this.onFailed(err.message || 'Location Error');
454
- return;
455
- }
456
- if (!position) {
457
- return;
458
- }
459
- this.onSuccess({
460
- latitude: position.coords.latitude,
461
- longitude: position.coords.longitude
462
- });
463
- }).then((id) => {
464
- this.watchId = id;
465
- });
466
- }
467
- stop() {
468
- return __awaiter(this, void 0, void 0, function* () {
469
- if (!this.watchId) {
470
- return;
471
- }
472
- yield Geolocation.clearWatch({
473
- id: this.watchId
474
- });
475
- this.watchId = null;
476
- });
477
- }
478
- isTracking() {
479
- return !!this.watchId;
480
- }
481
- }
482
463
  export class NotificationService {
483
464
  constructor(actionCallBack) {
484
465
  this.notificationFile = 'notifications.json';
@@ -693,3 +674,108 @@ export class NotificationService {
693
674
  this.init(actionCallBack);
694
675
  }
695
676
  }
677
+ export class Tracker {
678
+ constructor(p) {
679
+ this.listener = null;
680
+ this.cap = new AIOCapacitor();
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* () {
699
+ try {
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;
707
+ }
708
+ catch (err) {
709
+ alert("ERROR: " + JSON.stringify(err));
710
+ return false;
711
+ }
712
+ });
713
+ this.stop = () => __awaiter(this, void 0, void 0, function* () {
714
+ try {
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;
722
+ }
723
+ catch (err) {
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;
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: '' };
779
+ });
780
+ }
781
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-capacitor",
3
- "version": "2.2.2",
3
+ "version": "2.2.4",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",