aio-capacitor 2.2.3 → 2.3.0

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 +51 -34
  2. package/index.js +101 -104
  3. package/package.json +1 -2
package/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import { type LocationData } from 'aio-capacitor-bglocation';
1
2
  export type I_speak = {
2
3
  text: string;
3
4
  lang?: 'fa-IR' | 'en-US';
@@ -53,18 +54,6 @@ export declare class AIOCapacitor {
53
54
  uriToSrc: (uri: string) => Promise<any>;
54
55
  pathToUri: (path: string, directory: Directory) => Promise<any>;
55
56
  pathToSrc: (path: string, directory: Directory) => Promise<any>;
56
- pickFolder: (extensions?: string[]) => Promise<{
57
- files: {
58
- filePath: string;
59
- fileName: string;
60
- }[];
61
- folderPath: string;
62
- }>;
63
- getFolderFiles: (folderPath: string, extensions: string[]) => Promise<false | ({
64
- filePath: string;
65
- fileName: string;
66
- }[])>;
67
- openFile: (filePath: string) => Promise<void>;
68
57
  isFileExist: (path: string, location?: 'Data' | 'Documents' | 'Cache') => Promise<boolean>;
69
58
  readFile: (path: string, def: any, options?: {
70
59
  location?: 'Data' | 'Documents' | 'Cache';
@@ -89,7 +78,6 @@ export declare class AIOCapacitor {
89
78
  success: boolean;
90
79
  error: any;
91
80
  }>;
92
- getExtensions: (type: 'image' | 'audio' | 'video') => string[];
93
81
  shareFile: (file: Blob | File, fileName?: string) => Promise<any>;
94
82
  getLocation: () => Promise<{
95
83
  success: boolean;
@@ -133,26 +121,6 @@ declare class Logs {
133
121
  status?: 'error' | 'success' | 'warning' | 'info';
134
122
  }) => I_logItem[];
135
123
  }
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
124
  export type I_notifItem = {
157
125
  id: number;
158
126
  title: string;
@@ -199,4 +167,53 @@ export declare class NotificationService {
199
167
  }) => Promise<I_notifItem[]>;
200
168
  private save;
201
169
  }
202
- export {};
170
+ export type { LocationData };
171
+ export declare class Tracker {
172
+ token: string;
173
+ url: string;
174
+ listener: {
175
+ remove: () => Promise<void>;
176
+ } | null;
177
+ cap: AIOCapacitor;
178
+ onAdd: (data: LocationData) => void;
179
+ changeStatus: (p: {
180
+ hasPermission?: boolean;
181
+ active?: boolean;
182
+ pos?: [number, number];
183
+ }) => void;
184
+ interval: any;
185
+ isNative: boolean;
186
+ constructor(p: {
187
+ token: string;
188
+ url: string;
189
+ onAdd: (data: LocationData) => void;
190
+ changeStatus: (p: {
191
+ hasPermission?: boolean;
192
+ active?: boolean;
193
+ }) => void;
194
+ });
195
+ init: () => Promise<void>;
196
+ isServiceActive: () => Promise<boolean>;
197
+ getPermissions: () => Promise<boolean>;
198
+ start: () => Promise<boolean>;
199
+ stop: () => Promise<boolean>;
200
+ toggle: () => Promise<boolean>;
201
+ addListener: (callback: (v: LocationData) => void) => Promise<any>;
202
+ getMock(): {
203
+ lat: number;
204
+ lng: number;
205
+ sendSuccess: boolean;
206
+ sendError: string;
207
+ }[];
208
+ getCurrentLocation: () => Promise<{
209
+ success: boolean;
210
+ latitude?: number;
211
+ longitude?: number;
212
+ accuracy?: number;
213
+ source?: "network" | "gps" | "cache";
214
+ error?: string;
215
+ }>;
216
+ changePos: (pos: [number, number]) => Promise<void>;
217
+ goToCurrent: () => Promise<void>;
218
+ destroy: () => void;
219
+ }
package/index.js CHANGED
@@ -19,14 +19,14 @@ import { Haptics, ImpactStyle } from '@capacitor/haptics';
19
19
  import { Share } from '@capacitor/share';
20
20
  import { LocalNotifications } from '@capacitor/local-notifications';
21
21
  import { KeepAwake } from '@capacitor-community/keep-awake';
22
- import { FolderPicker } from 'aio-capacitor-folder-picker';
23
- import { FileClass, GenerateId } from 'aio-class';
22
+ import { FileClass, GenerateId, GetRandomNumber } from 'aio-class';
23
+ import { Example } from 'aio-capacitor-bglocation';
24
24
  export class AIOCapacitor {
25
25
  constructor(p) {
26
26
  this.logs = new Logs({
27
27
  load: () => __awaiter(this, void 0, void 0, function* () {
28
28
  try {
29
- const storageRes = yield this.readFile('logs.json', { location: "Data", utf8: true });
29
+ const storageRes = yield this.readFile('logs.json', [], { location: "Data", utf8: true, writeDef: true });
30
30
  const res = JSON.parse(storageRes);
31
31
  return res;
32
32
  }
@@ -71,7 +71,7 @@ export class AIOCapacitor {
71
71
  console.error('Haptics pattern error:', err);
72
72
  }
73
73
  });
74
- this.vibrateImpact = (...args_1) => __awaiter(this, [...args_1], void 0, function* (style = 'medium') {
74
+ this.vibrateImpact = (style = 'medium') => __awaiter(this, void 0, void 0, function* () {
75
75
  try {
76
76
  const map = {
77
77
  light: ImpactStyle.Light,
@@ -176,28 +176,6 @@ export class AIOCapacitor {
176
176
  this.uriToSrc = (uri) => __awaiter(this, void 0, void 0, function* () { return Capacitor.convertFileSrc(uri); });
177
177
  this.pathToUri = (path, directory) => __awaiter(this, void 0, void 0, function* () { return (yield Filesystem.getUri({ path, directory })).uri; });
178
178
  this.pathToSrc = (path, directory) => __awaiter(this, void 0, void 0, function* () { return yield this.uriToSrc(yield this.pathToUri(path, directory)); });
179
- this.pickFolder = (extensions) => __awaiter(this, void 0, void 0, function* () {
180
- const { folderPath, files } = yield FolderPicker.pickFolder(extensions ? { extensions } : undefined);
181
- return { folderPath, files };
182
- });
183
- this.getFolderFiles = (folderPath, extensions) => __awaiter(this, void 0, void 0, function* () {
184
- try {
185
- const res = yield FolderPicker.getFolderFiles({ folderPath, extensions });
186
- return res.files;
187
- }
188
- catch (error) {
189
- alert("updateFolder error:" + String(error));
190
- return false;
191
- }
192
- });
193
- this.openFile = (filePath) => __awaiter(this, void 0, void 0, function* () {
194
- try {
195
- yield FolderPicker.openFile({ filePath });
196
- }
197
- catch (error) {
198
- alert(`Open file error:\n${String(error)}`);
199
- }
200
- });
201
179
  this.isFileExist = (path, location) => __awaiter(this, void 0, void 0, function* () {
202
180
  try {
203
181
  yield Filesystem.stat({ path, directory: this.getDirectory(location) });
@@ -270,27 +248,7 @@ export class AIOCapacitor {
270
248
  return { success: false, error: (err === null || err === void 0 ? void 0 : err.message) || err };
271
249
  }
272
250
  });
273
- this.getExtensions = (type) => {
274
- const extensions = {
275
- image: [
276
- 'jpg', 'jpeg', 'png', 'gif', 'webp',
277
- 'bmp', 'svg', 'ico', 'tiff', 'tif', 'avif'
278
- ],
279
- audio: [
280
- 'mp3', 'wav', 'flac', 'aac', 'm4a',
281
- 'ogg', 'oga', 'opus', 'wma', 'aiff', 'aif',
282
- 'alac', 'amr', 'mid', 'midi'
283
- ],
284
- video: [
285
- 'mp4', 'webm', 'mkv', 'avi', 'mov',
286
- 'wmv', 'flv', 'm4v', '3gp', '3g2',
287
- 'mpeg', 'mpg', 'mts', 'm2ts', 'ts',
288
- 'ogv', 'vob'
289
- ]
290
- };
291
- return extensions[type];
292
- };
293
- this.shareFile = (file_1, ...args_2) => __awaiter(this, [file_1, ...args_2], void 0, function* (file, fileName = `file_${Date.now()}`) {
251
+ this.shareFile = (file, fileName = `file_${Date.now()}`) => __awaiter(this, void 0, void 0, function* () {
294
252
  try {
295
253
  const res = yield new FileClass().toDataUrl(file);
296
254
  if (!res.success) {
@@ -459,52 +417,6 @@ class Logs {
459
417
  this.save = p.save;
460
418
  }
461
419
  }
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
420
  export class NotificationService {
509
421
  constructor(actionCallBack) {
510
422
  this.notificationFile = 'notifications.json';
@@ -719,27 +631,112 @@ export class NotificationService {
719
631
  this.init(actionCallBack);
720
632
  }
721
633
  }
722
- class AppStorage {
723
- constructor(props) {
634
+ export class Tracker {
635
+ constructor(p) {
636
+ this.listener = null;
724
637
  this.cap = new AIOCapacitor();
725
- this.save = (data) => __awaiter(this, void 0, void 0, function* () {
638
+ this.init = () => __awaiter(this, void 0, void 0, function* () {
639
+ if (!this.isNative) {
640
+ return;
641
+ }
642
+ const hasPermission = yield this.getPermissions();
643
+ this.changeStatus({ hasPermission });
644
+ this.interval = window.setInterval(() => __awaiter(this, void 0, void 0, function* () {
645
+ const active = yield this.isServiceActive();
646
+ this.changeStatus({ active });
647
+ }), 5000);
648
+ });
649
+ this.isServiceActive = () => __awaiter(this, void 0, void 0, function* () {
650
+ const res = yield Example.isServiceActive();
651
+ return res.active;
652
+ });
653
+ this.getPermissions = () => __awaiter(this, void 0, void 0, function* () {
654
+ const a = yield Example.getLocationPermission();
655
+ const b = yield Example.getNotificationPermission();
656
+ return !!a.granted && !!b.granted;
657
+ });
658
+ this.start = () => __awaiter(this, void 0, void 0, function* () {
726
659
  try {
727
- yield this.cap.writeFile(JSON.stringify(data), this.fileName, { location: this.location, utf8: true });
660
+ yield Example.startTracker({ url: this.url, token: this.token });
661
+ if (this.listener !== null) {
662
+ yield this.listener.remove();
663
+ }
664
+ this.listener = yield this.addListener((data) => this.onAdd(data));
665
+ this.changeStatus({ active: true });
666
+ return true;
728
667
  }
729
668
  catch (err) {
730
- alert(`AppStorage save error: ${err.message}`);
669
+ alert("ERROR: " + JSON.stringify(err));
670
+ return false;
731
671
  }
732
672
  });
733
- this.load = (def) => __awaiter(this, void 0, void 0, function* () {
673
+ this.stop = () => __awaiter(this, void 0, void 0, function* () {
734
674
  try {
735
- const data = yield this.cap.readFile(this.fileName, { location: this.location, utf8: true });
675
+ if (this.listener !== null) {
676
+ yield this.listener.remove();
677
+ this.listener = null;
678
+ }
679
+ yield Example.stopTracker();
680
+ this.changeStatus({ active: false });
681
+ return true;
736
682
  }
737
683
  catch (err) {
738
- console.error('storageLoad error:', err);
739
- return def;
684
+ alert("ERROR: " + JSON.stringify(err));
685
+ return false;
686
+ }
687
+ });
688
+ this.toggle = () => __awaiter(this, void 0, void 0, function* () {
689
+ const active = yield this.isServiceActive();
690
+ if (active) {
691
+ return yield this.stop();
692
+ }
693
+ else {
694
+ return yield this.start();
695
+ }
696
+ });
697
+ this.addListener = (callback) => __awaiter(this, void 0, void 0, function* () {
698
+ return yield Example.addListener('tracker', (data) => callback(data));
699
+ });
700
+ this.getCurrentLocation = () => __awaiter(this, void 0, void 0, function* () { return yield this.cap.getLocation(); });
701
+ this.changePos = (pos) => __awaiter(this, void 0, void 0, function* () { return this.changeStatus({ pos }); });
702
+ this.goToCurrent = () => __awaiter(this, void 0, void 0, function* () {
703
+ const { success, error, latitude, longitude } = yield this.getCurrentLocation();
704
+ if (success) {
705
+ this.changeStatus({ pos: [latitude || 0, longitude || 0] });
706
+ }
707
+ else {
708
+ alert(error);
740
709
  }
741
710
  });
742
- this.fileName = props.fileName;
743
- this.location = props.location;
711
+ this.destroy = () => {
712
+ window.clearInterval(this.interval);
713
+ if (this.listener !== null) {
714
+ this.listener.remove();
715
+ this.listener = null;
716
+ }
717
+ };
718
+ this.token = p.token;
719
+ this.url = p.url;
720
+ this.onAdd = p.onAdd;
721
+ this.changeStatus = p.changeStatus;
722
+ this.init();
723
+ this.isNative = this.cap.isNative();
724
+ }
725
+ getMock() {
726
+ const testLogs = [
727
+ [35.79163979013101, 51.02188110351563], [35.784955845919164, 51.02874755859376], [35.78384180056804, 51.04797363281251], [35.78161366302545, 51.05072021484376],
728
+ [35.7804995708344, 51.05072021484376], [35.7671292467937, 51.06994628906251], [35.7626719725612, 51.07406616210938], [35.73034926596472, 51.16744995117188],
729
+ [35.7270047886309, 51.17843627929688], [35.72588993164639, 51.20040893554688], [35.70916520463913, 51.31301879882813], [35.70805009803191, 51.33361816406251],
730
+ [35.70805009803191, 51.33499145507813], [35.70470468461301, 51.34597778320313], [35.681282860023565, 51.36932373046876], [35.60092726180499, 51.373443603515625],
731
+ [35.52272633049273, 51.31301879882813], [35.51601984359331, 51.3006591796875], [35.48024245154482, 51.236114501953125], [35.35377612773401, 51.17568969726563],
732
+ [35.01818803935104, 50.88180541992188], [34.95743255328083, 50.86257934570313], [34.862834444032536, 50.855712890625], [34.8211316630575, 50.84197998046876],
733
+ [34.78730308987354, 50.843353271484375], [34.717346725560674, 50.85159301757813], [34.67331156427795, 50.87081909179688], [34.61682196594164, 50.90103149414063],
734
+ [34.61230113572208, 50.90240478515626], [34.55803197344862, 50.95321655273438], [34.49127759454727, 51.02737426757813]
735
+ ];
736
+ return testLogs.map(([lat, lng]) => {
737
+ const rnd = GetRandomNumber(0, 10);
738
+ const sendSuccess = rnd > 3;
739
+ return { lat, lng, sendSuccess, sendError: '' };
740
+ });
744
741
  }
745
742
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "aio-capacitor",
3
- "version": "2.2.3",
3
+ "version": "2.3.0",
4
4
  "description": "",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -20,7 +20,6 @@
20
20
  "@capacitor/preferences": "latest",
21
21
  "@capacitor/push-notifications": "latest",
22
22
  "@capacitor/share": "latest",
23
- "aio-capacitor-folder-picker":"latest",
24
23
  "aio-class":"latest"
25
24
  },
26
25
  "repository": {