react-native-asleep 0.0.7 → 0.0.8
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.
- package/License.md +18 -0
- package/README.md +389 -18
- package/android/build.gradle +1 -0
- package/android/src/main/java/ai/asleep/reactnative/AsleepModule.kt +38 -1
- package/build/Asleep.types.d.ts +17 -1
- package/build/Asleep.types.d.ts.map +1 -1
- package/build/Asleep.types.js.map +1 -1
- package/build/AsleepStore.d.ts +37 -0
- package/build/AsleepStore.d.ts.map +1 -0
- package/build/AsleepStore.js +240 -0
- package/build/AsleepStore.js.map +1 -0
- package/build/index.d.ts +28 -3
- package/build/index.d.ts.map +1 -1
- package/build/index.js +32 -166
- package/build/index.js.map +1 -1
- package/ios/AsleepModule.swift +43 -2
- package/package.json +3 -2
- package/src/Asleep.types.ts +12 -1
- package/src/AsleepStore.ts +334 -0
- package/src/index.ts +71 -218
package/src/index.ts
CHANGED
|
@@ -3,11 +3,13 @@ import { useCallback, useEffect, useState } from "react";
|
|
|
3
3
|
import { Alert, Platform } from "react-native";
|
|
4
4
|
import {
|
|
5
5
|
AsleepConfig,
|
|
6
|
+
AsleepSetupConfig,
|
|
6
7
|
AsleepEventType,
|
|
7
8
|
AsleepReport,
|
|
8
9
|
AsleepSession,
|
|
9
10
|
} from "./Asleep.types";
|
|
10
11
|
import AsleepModule from "./AsleepModule";
|
|
12
|
+
import { useAsleepStore, initializeAsleepListeners } from "./AsleepStore";
|
|
11
13
|
|
|
12
14
|
const emitter = new EventEmitter(AsleepModule);
|
|
13
15
|
|
|
@@ -16,6 +18,21 @@ class Asleep {
|
|
|
16
18
|
[K in keyof AsleepEventType]?: ((data: AsleepEventType[K]) => void)[];
|
|
17
19
|
} = {};
|
|
18
20
|
|
|
21
|
+
setup = async (config: AsleepSetupConfig): Promise<void> => {
|
|
22
|
+
try {
|
|
23
|
+
await AsleepModule.setup(
|
|
24
|
+
config.apiKey,
|
|
25
|
+
config.baseUrl,
|
|
26
|
+
config.callbackUrl,
|
|
27
|
+
config.service,
|
|
28
|
+
config.enableODA
|
|
29
|
+
);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
console.error("setup error:", error);
|
|
32
|
+
throw error;
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
|
|
19
36
|
initAsleepConfig = async (config: AsleepConfig): Promise<void> => {
|
|
20
37
|
try {
|
|
21
38
|
const result = await AsleepModule.initAsleepConfig(
|
|
@@ -113,226 +130,26 @@ class Asleep {
|
|
|
113
130
|
}
|
|
114
131
|
|
|
115
132
|
export const useAsleep = () => {
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
}, []);
|
|
133
|
-
|
|
134
|
-
const startTracking = useCallback(async () => {
|
|
135
|
-
try {
|
|
136
|
-
console.log("[useAsleep] startTracking");
|
|
137
|
-
setDidClose(false);
|
|
138
|
-
setIsTracking(true);
|
|
139
|
-
console.log("[useAsleep] startTracking isTracking: ", isTracking);
|
|
140
|
-
await asleep.startTracking();
|
|
141
|
-
} catch (err: any) {
|
|
142
|
-
console.error("startTracking error:", err);
|
|
143
|
-
setError(err.message);
|
|
144
|
-
}
|
|
145
|
-
}, []);
|
|
146
|
-
|
|
147
|
-
const stopTracking = useCallback(async () => {
|
|
148
|
-
try {
|
|
149
|
-
console.log("[useAsleep] stopTracking");
|
|
150
|
-
const sessionId = await asleep.stopTracking();
|
|
151
|
-
setDidClose(true);
|
|
152
|
-
setSessionId(sessionId);
|
|
153
|
-
setIsTracking(false);
|
|
154
|
-
} catch (err: any) {
|
|
155
|
-
console.error("stopTracking error:", err);
|
|
156
|
-
setError(err.message);
|
|
157
|
-
}
|
|
158
|
-
}, []);
|
|
159
|
-
|
|
160
|
-
const getReport = async (sessionId: string): Promise<AsleepReport | null> => {
|
|
161
|
-
try {
|
|
162
|
-
console.log("[useAsleep] getReport sessionId: ", sessionId);
|
|
163
|
-
const report = await asleep.getReport(sessionId);
|
|
164
|
-
return report;
|
|
165
|
-
} catch (err: any) {
|
|
166
|
-
console.error("getReport error:", err);
|
|
167
|
-
setError(err.message);
|
|
168
|
-
return null;
|
|
169
|
-
}
|
|
170
|
-
};
|
|
171
|
-
|
|
172
|
-
const getReportList = async (
|
|
173
|
-
fromDate: string,
|
|
174
|
-
toDate: string
|
|
175
|
-
): Promise<AsleepSession[]> => {
|
|
176
|
-
try {
|
|
177
|
-
console.log(
|
|
178
|
-
`[useAsleep] getReportList fromDate: ${fromDate}, toDate: ${toDate}`
|
|
179
|
-
);
|
|
180
|
-
const reportList = await asleep.getReportList(fromDate, toDate);
|
|
181
|
-
return reportList;
|
|
182
|
-
} catch (err: any) {
|
|
183
|
-
console.error("getReportList error:", err);
|
|
184
|
-
setError(err.message);
|
|
185
|
-
return [];
|
|
186
|
-
}
|
|
187
|
-
};
|
|
188
|
-
|
|
189
|
-
const addEventListener = useCallback(
|
|
190
|
-
<K extends keyof AsleepEventType>(
|
|
191
|
-
eventType: K,
|
|
192
|
-
listener: (data: AsleepEventType[K]) => void
|
|
193
|
-
) => {
|
|
194
|
-
const subscription = asleep.addEventListener(eventType, listener);
|
|
195
|
-
return () => subscription.remove();
|
|
196
|
-
},
|
|
197
|
-
[]
|
|
198
|
-
);
|
|
199
|
-
|
|
200
|
-
const enableLog = (print: boolean = true) => {
|
|
201
|
-
setShowDebugLog(print);
|
|
202
|
-
};
|
|
203
|
-
|
|
204
|
-
const setCustomNotification = useCallback(
|
|
205
|
-
async (title: string, text: string) => {
|
|
206
|
-
if (Platform.OS === "android") {
|
|
207
|
-
await AsleepModule.setCustomNotification(title, text);
|
|
208
|
-
} else {
|
|
209
|
-
console.warn("setCustomNotification is not supported on this platform");
|
|
210
|
-
}
|
|
211
|
-
},
|
|
212
|
-
[]
|
|
213
|
-
);
|
|
214
|
-
|
|
215
|
-
const addLog = useCallback(
|
|
216
|
-
(log: string) => {
|
|
217
|
-
const now = new Date();
|
|
218
|
-
const dateString = `${now.getHours().toString().padStart(2, "0")}:${now
|
|
219
|
-
.getMinutes()
|
|
220
|
-
.toString()
|
|
221
|
-
.padStart(2, "0")}:${now.getSeconds().toString().padStart(2, "0")}`;
|
|
222
|
-
|
|
223
|
-
if (showDebugLog) {
|
|
224
|
-
console.log(`[Asleep][${dateString}]${log}`);
|
|
225
|
-
}
|
|
226
|
-
setLog(`[${dateString}]${log}`);
|
|
227
|
-
},
|
|
228
|
-
[showDebugLog]
|
|
229
|
-
);
|
|
133
|
+
const {
|
|
134
|
+
didClose,
|
|
135
|
+
isTracking,
|
|
136
|
+
error,
|
|
137
|
+
userId,
|
|
138
|
+
sessionId,
|
|
139
|
+
log,
|
|
140
|
+
setup,
|
|
141
|
+
initAsleepConfig,
|
|
142
|
+
startTracking,
|
|
143
|
+
stopTracking,
|
|
144
|
+
getReport,
|
|
145
|
+
getReportList,
|
|
146
|
+
enableLog,
|
|
147
|
+
setCustomNotification,
|
|
148
|
+
} = useAsleepStore();
|
|
230
149
|
|
|
231
150
|
useEffect(() => {
|
|
232
|
-
const
|
|
233
|
-
|
|
234
|
-
addLog(`[onUserJoined] userId: ${data.userId}`);
|
|
235
|
-
};
|
|
236
|
-
const onUserJoinFailed = (error: any) => {
|
|
237
|
-
const errorString = JSON.stringify(error);
|
|
238
|
-
setError(errorString);
|
|
239
|
-
addLog(`[onUserJoinFailed] error: ${errorString}`);
|
|
240
|
-
};
|
|
241
|
-
const onUserDeleted = (data: any) => {
|
|
242
|
-
setUserId(null);
|
|
243
|
-
addLog(`[onUserDeleted] userId: ${data.userId}`);
|
|
244
|
-
};
|
|
245
|
-
const onTrackingCreated = (data: any) => {
|
|
246
|
-
setIsTracking(true);
|
|
247
|
-
if (data && data.sessionId) {
|
|
248
|
-
setSessionId(data.sessionId);
|
|
249
|
-
}
|
|
250
|
-
addLog(
|
|
251
|
-
`[onTrackingCreated]${data?.sessionId ? ` sessionId: ${data.sessionId}` : ""}`
|
|
252
|
-
);
|
|
253
|
-
};
|
|
254
|
-
const onTrackingUploaded = (data: any) => {
|
|
255
|
-
addLog(`[onTrackingUploaded] sequence: ${data.sequence}`);
|
|
256
|
-
};
|
|
257
|
-
const onTrackingClosed = (data: { sessionId: string }) => {
|
|
258
|
-
setSessionId(data.sessionId);
|
|
259
|
-
setDidClose(true);
|
|
260
|
-
setIsTracking(false);
|
|
261
|
-
addLog(`[onTrackingClosed] sessionId: ${data.sessionId}`);
|
|
262
|
-
};
|
|
263
|
-
const onTrackingFailed = (error: any) => {
|
|
264
|
-
const errorString = JSON.stringify(error);
|
|
265
|
-
setError(errorString);
|
|
266
|
-
setIsTracking(false);
|
|
267
|
-
addLog(`[onTrackingFailed] error: ${errorString}`);
|
|
268
|
-
};
|
|
269
|
-
const onTrackingInterrupted = () => {
|
|
270
|
-
setIsTracking(false);
|
|
271
|
-
addLog(`[onTrackingInterrupted]`);
|
|
272
|
-
};
|
|
273
|
-
const onTrackingResumed = () => {
|
|
274
|
-
setIsTracking(true);
|
|
275
|
-
addLog(`[onTrackingResumed]`);
|
|
276
|
-
};
|
|
277
|
-
const onMicPermissionDenied = () => {
|
|
278
|
-
addLog(`[onMicPermissionDenied]`);
|
|
279
|
-
};
|
|
280
|
-
const onDebugLog = (data: any) => {
|
|
281
|
-
addLog(`[onDebugLog] message: ${data.message}`);
|
|
282
|
-
};
|
|
283
|
-
|
|
284
|
-
const userJoinedListener = addEventListener("onUserJoined", onUserJoined);
|
|
285
|
-
const userJoinFailedListener = addEventListener(
|
|
286
|
-
"onUserJoinFailed",
|
|
287
|
-
onUserJoinFailed
|
|
288
|
-
);
|
|
289
|
-
const userDeletedListener = addEventListener(
|
|
290
|
-
"onUserDeleted",
|
|
291
|
-
onUserDeleted
|
|
292
|
-
);
|
|
293
|
-
const trackingCreatedListener = addEventListener(
|
|
294
|
-
"onTrackingCreated",
|
|
295
|
-
onTrackingCreated
|
|
296
|
-
);
|
|
297
|
-
const trackingUploadedListener = addEventListener(
|
|
298
|
-
"onTrackingUploaded",
|
|
299
|
-
onTrackingUploaded
|
|
300
|
-
);
|
|
301
|
-
const trackingClosedListener = addEventListener(
|
|
302
|
-
"onTrackingClosed",
|
|
303
|
-
onTrackingClosed
|
|
304
|
-
);
|
|
305
|
-
const trackingFailedListener = addEventListener(
|
|
306
|
-
"onTrackingFailed",
|
|
307
|
-
onTrackingFailed
|
|
308
|
-
);
|
|
309
|
-
const trackingInterruptedListener = addEventListener(
|
|
310
|
-
"onTrackingInterrupted",
|
|
311
|
-
onTrackingInterrupted
|
|
312
|
-
);
|
|
313
|
-
const trackingResumedListener = addEventListener(
|
|
314
|
-
"onTrackingResumed",
|
|
315
|
-
onTrackingResumed
|
|
316
|
-
);
|
|
317
|
-
const micPermissionDeniedListener = addEventListener(
|
|
318
|
-
"onMicPermissionDenied",
|
|
319
|
-
onMicPermissionDenied
|
|
320
|
-
);
|
|
321
|
-
const debugLogListener = addEventListener("onDebugLog", onDebugLog);
|
|
322
|
-
|
|
323
|
-
return () => {
|
|
324
|
-
userJoinedListener();
|
|
325
|
-
userJoinFailedListener();
|
|
326
|
-
userDeletedListener();
|
|
327
|
-
trackingCreatedListener();
|
|
328
|
-
trackingUploadedListener();
|
|
329
|
-
trackingClosedListener();
|
|
330
|
-
trackingFailedListener();
|
|
331
|
-
trackingInterruptedListener();
|
|
332
|
-
trackingResumedListener();
|
|
333
|
-
micPermissionDeniedListener();
|
|
334
|
-
debugLogListener();
|
|
335
|
-
};
|
|
151
|
+
const cleanup = initializeAsleepListeners();
|
|
152
|
+
return cleanup;
|
|
336
153
|
}, []);
|
|
337
154
|
|
|
338
155
|
return {
|
|
@@ -343,6 +160,7 @@ export const useAsleep = () => {
|
|
|
343
160
|
log,
|
|
344
161
|
enableLog,
|
|
345
162
|
setCustomNotification,
|
|
163
|
+
setup,
|
|
346
164
|
initAsleepConfig,
|
|
347
165
|
startTracking,
|
|
348
166
|
stopTracking,
|
|
@@ -352,11 +170,46 @@ export const useAsleep = () => {
|
|
|
352
170
|
};
|
|
353
171
|
};
|
|
354
172
|
|
|
173
|
+
export const asleepStore = useAsleepStore;
|
|
174
|
+
|
|
175
|
+
export const AsleepSDK = {
|
|
176
|
+
setup: (config: AsleepSetupConfig) => useAsleepStore.getState().setup(config),
|
|
177
|
+
|
|
178
|
+
initAsleepConfig: (config: AsleepConfig) =>
|
|
179
|
+
useAsleepStore.getState().initAsleepConfig(config),
|
|
180
|
+
|
|
181
|
+
startTracking: () => useAsleepStore.getState().startTracking(),
|
|
182
|
+
|
|
183
|
+
stopTracking: () => useAsleepStore.getState().stopTracking(),
|
|
184
|
+
|
|
185
|
+
getReport: (sessionId: string) =>
|
|
186
|
+
useAsleepStore.getState().getReport(sessionId),
|
|
187
|
+
|
|
188
|
+
getReportList: (fromDate: string, toDate: string) =>
|
|
189
|
+
useAsleepStore.getState().getReportList(fromDate, toDate),
|
|
190
|
+
|
|
191
|
+
isTracking: () => useAsleepStore.getState().isTracking,
|
|
192
|
+
|
|
193
|
+
getUserId: () => useAsleepStore.getState().userId,
|
|
194
|
+
|
|
195
|
+
getSessionId: () => useAsleepStore.getState().sessionId,
|
|
196
|
+
|
|
197
|
+
enableLog: (print: boolean) => useAsleepStore.getState().enableLog(print),
|
|
198
|
+
|
|
199
|
+
setCustomNotification: (title: string, text: string) =>
|
|
200
|
+
useAsleepStore.getState().setCustomNotification(title, text),
|
|
201
|
+
|
|
202
|
+
initialize: () => {
|
|
203
|
+
return initializeAsleepListeners();
|
|
204
|
+
},
|
|
205
|
+
};
|
|
206
|
+
|
|
355
207
|
const asleep = new Asleep();
|
|
356
208
|
export default asleep;
|
|
357
209
|
|
|
358
210
|
export type {
|
|
359
211
|
AsleepConfig,
|
|
212
|
+
AsleepSetupConfig,
|
|
360
213
|
AsleepEventType,
|
|
361
214
|
AsleepReport,
|
|
362
215
|
AsleepSession,
|