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/ios/AsleepModule.swift
CHANGED
|
@@ -21,6 +21,18 @@ public class AsleepModule: Module {
|
|
|
21
21
|
Events("onUserDeleted")
|
|
22
22
|
|
|
23
23
|
Events("onDebugLog")
|
|
24
|
+
Events("onSetupDidComplete")
|
|
25
|
+
Events("onSetupDidFail")
|
|
26
|
+
Events("onSetupInProgress")
|
|
27
|
+
|
|
28
|
+
Function("setup") { (apiKey: String, baseUrl: String?, callbackUrl: String?, service: String?, enableODA: Bool?) in
|
|
29
|
+
Asleep.setup(apiKey: apiKey,
|
|
30
|
+
baseUrl: URL(string: baseUrl ?? ""),
|
|
31
|
+
callbackUrl: URL(string: callbackUrl ?? ""),
|
|
32
|
+
service: service,
|
|
33
|
+
enableODA: enableODA ?? false,
|
|
34
|
+
delegate: self)
|
|
35
|
+
}
|
|
24
36
|
|
|
25
37
|
Function("initAsleepConfig") { (apiKey: String, userId: String?, baseUrl: String?, callbackUrl: String?) in
|
|
26
38
|
Asleep.initAsleepConfig(apiKey: apiKey,
|
|
@@ -102,6 +114,20 @@ public class AsleepModule: Module {
|
|
|
102
114
|
}
|
|
103
115
|
}
|
|
104
116
|
|
|
117
|
+
extension AsleepModule: AsleepSetupDelegate {
|
|
118
|
+
public func setupDidComplete() {
|
|
119
|
+
sendEvent("onSetupDidComplete", [:])
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
public func setupDidFail(error: Asleep.AsleepError) {
|
|
123
|
+
sendEvent("onSetupDidFail", ["error": error.localizedDescription])
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
public func setupInProgress(progress: Int) {
|
|
127
|
+
sendEvent("onSetupInProgress", ["progress": progress])
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
105
131
|
extension AsleepModule: AsleepConfigDelegate {
|
|
106
132
|
public func userDidJoin(userId: String, config: Asleep.Config) {
|
|
107
133
|
self.config = config
|
|
@@ -125,7 +151,22 @@ extension AsleepModule: AsleepSleepTrackingManagerDelegate {
|
|
|
125
151
|
}
|
|
126
152
|
|
|
127
153
|
public func didCreate() {
|
|
128
|
-
|
|
154
|
+
attemptGetSessionId(retriesLeft: 5, retryInterval: 1.0)
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
private func attemptGetSessionId(retriesLeft: Int, retryInterval: TimeInterval) {
|
|
158
|
+
if let sessionId = trackingManager?.getTrackingStatus().sessionId {
|
|
159
|
+
sendEvent("onTrackingCreated", ["sessionId": sessionId])
|
|
160
|
+
return
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
if retriesLeft > 0 {
|
|
164
|
+
DispatchQueue.global().asyncAfter(deadline: .now() + retryInterval) { [weak self] in
|
|
165
|
+
self?.attemptGetSessionId(retriesLeft: retriesLeft - 1, retryInterval: retryInterval)
|
|
166
|
+
}
|
|
167
|
+
} else {
|
|
168
|
+
sendEvent("onTrackingCreated", [:])
|
|
169
|
+
}
|
|
129
170
|
}
|
|
130
171
|
|
|
131
172
|
public func didUpload(sequence: Int) {
|
|
@@ -198,4 +239,4 @@ extension Asleep.Model.SleepSession {
|
|
|
198
239
|
|
|
199
240
|
return dict
|
|
200
241
|
}
|
|
201
|
-
}
|
|
242
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "react-native-asleep",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.8",
|
|
4
4
|
"description": "Asleep SDK for React Native",
|
|
5
5
|
"main": "build/index.js",
|
|
6
6
|
"types": "build/index.d.ts",
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
"peerDependencies": {
|
|
38
38
|
"expo": "*",
|
|
39
39
|
"react": "*",
|
|
40
|
-
"react-native": "*"
|
|
40
|
+
"react-native": "*",
|
|
41
|
+
"zustand": "^5.0.1"
|
|
41
42
|
}
|
|
42
43
|
}
|
package/src/Asleep.types.ts
CHANGED
|
@@ -5,6 +5,14 @@ export type AsleepConfig = {
|
|
|
5
5
|
callbackUrl?: string;
|
|
6
6
|
};
|
|
7
7
|
|
|
8
|
+
export type AsleepSetupConfig = {
|
|
9
|
+
apiKey: string;
|
|
10
|
+
baseUrl?: string;
|
|
11
|
+
callbackUrl?: string;
|
|
12
|
+
service?: string;
|
|
13
|
+
enableODA?: boolean;
|
|
14
|
+
};
|
|
15
|
+
|
|
8
16
|
export type AsleepReport = {
|
|
9
17
|
timezone: string;
|
|
10
18
|
session: {
|
|
@@ -73,7 +81,7 @@ export type AsleepSession = {
|
|
|
73
81
|
};
|
|
74
82
|
|
|
75
83
|
export type AsleepEventType = {
|
|
76
|
-
onTrackingCreated:
|
|
84
|
+
onTrackingCreated: { sessionId?: string };
|
|
77
85
|
onTrackingUploaded: { sequence: number };
|
|
78
86
|
onTrackingClosed: { sessionId: string };
|
|
79
87
|
onTrackingFailed: undefined;
|
|
@@ -84,4 +92,7 @@ export type AsleepEventType = {
|
|
|
84
92
|
onUserJoinFailed: undefined;
|
|
85
93
|
onUserDeleted: undefined;
|
|
86
94
|
onDebugLog: { message: string };
|
|
95
|
+
onSetupDidComplete: undefined;
|
|
96
|
+
onSetupDidFail: { error: string };
|
|
97
|
+
onSetupInProgress: { progress: number };
|
|
87
98
|
};
|
|
@@ -0,0 +1,334 @@
|
|
|
1
|
+
import { create } from "zustand";
|
|
2
|
+
import { subscribeWithSelector } from "zustand/middleware";
|
|
3
|
+
import { EventEmitter } from "expo-modules-core";
|
|
4
|
+
import { Alert, Platform } from "react-native";
|
|
5
|
+
import {
|
|
6
|
+
AsleepConfig,
|
|
7
|
+
AsleepSetupConfig,
|
|
8
|
+
AsleepEventType,
|
|
9
|
+
AsleepReport,
|
|
10
|
+
AsleepSession,
|
|
11
|
+
} from "./Asleep.types";
|
|
12
|
+
import AsleepModule from "./AsleepModule";
|
|
13
|
+
|
|
14
|
+
const emitter = new EventEmitter(AsleepModule);
|
|
15
|
+
|
|
16
|
+
export interface AsleepState {
|
|
17
|
+
didClose: boolean;
|
|
18
|
+
isTracking: boolean;
|
|
19
|
+
error: string | null;
|
|
20
|
+
userId: string | null;
|
|
21
|
+
sessionId: string | null;
|
|
22
|
+
showDebugLog: boolean;
|
|
23
|
+
log: string;
|
|
24
|
+
|
|
25
|
+
// actions
|
|
26
|
+
setup: (config: AsleepSetupConfig) => Promise<void>;
|
|
27
|
+
initAsleepConfig: (config: AsleepConfig) => Promise<void>;
|
|
28
|
+
startTracking: () => Promise<void>;
|
|
29
|
+
stopTracking: () => Promise<void>;
|
|
30
|
+
getReport: (sessionId: string) => Promise<AsleepReport | null>;
|
|
31
|
+
getReportList: (fromDate: string, toDate: string) => Promise<AsleepSession[]>;
|
|
32
|
+
requestMicrophonePermission: () => Promise<boolean>;
|
|
33
|
+
setCustomNotification: (title: string, text: string) => Promise<void>;
|
|
34
|
+
enableLog: (print: boolean) => void;
|
|
35
|
+
addEventListener: <K extends keyof AsleepEventType>(
|
|
36
|
+
eventType: K,
|
|
37
|
+
listener: (data: AsleepEventType[K]) => void
|
|
38
|
+
) => () => void;
|
|
39
|
+
|
|
40
|
+
// internal actions
|
|
41
|
+
setError: (error: string | null) => void;
|
|
42
|
+
setUserId: (userId: string | null) => void;
|
|
43
|
+
setSessionId: (sessionId: string | null) => void;
|
|
44
|
+
setIsTracking: (isTracking: boolean) => void;
|
|
45
|
+
setDidClose: (didClose: boolean) => void;
|
|
46
|
+
addLog: (log: string) => void;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
const convertKeysToCamelCase = (obj: any): any => {
|
|
50
|
+
if (Array.isArray(obj)) {
|
|
51
|
+
return obj.map(convertKeysToCamelCase);
|
|
52
|
+
} else if (obj !== null && obj.constructor === Object) {
|
|
53
|
+
return Object.keys(obj).reduce((acc, key) => {
|
|
54
|
+
const camelKey = key.replace(/_([a-z])/g, (g) => g[1].toUpperCase());
|
|
55
|
+
acc[camelKey] = convertKeysToCamelCase(obj[key]);
|
|
56
|
+
return acc;
|
|
57
|
+
}, {} as any);
|
|
58
|
+
}
|
|
59
|
+
return obj;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export const useAsleepStore = create<AsleepState>()(
|
|
63
|
+
subscribeWithSelector((set, get) => ({
|
|
64
|
+
// initial state
|
|
65
|
+
didClose: false,
|
|
66
|
+
isTracking: AsleepModule.isTracking ? AsleepModule.isTracking() : false,
|
|
67
|
+
error: null,
|
|
68
|
+
userId: null,
|
|
69
|
+
sessionId: null,
|
|
70
|
+
showDebugLog: false,
|
|
71
|
+
log: "",
|
|
72
|
+
|
|
73
|
+
// actions
|
|
74
|
+
setup: async (config: AsleepSetupConfig) => {
|
|
75
|
+
try {
|
|
76
|
+
const { addLog } = get();
|
|
77
|
+
addLog("[setup] Start");
|
|
78
|
+
|
|
79
|
+
await AsleepModule.setup(
|
|
80
|
+
config.apiKey,
|
|
81
|
+
config.baseUrl,
|
|
82
|
+
config.callbackUrl,
|
|
83
|
+
config.service,
|
|
84
|
+
config.enableODA
|
|
85
|
+
);
|
|
86
|
+
|
|
87
|
+
addLog("[setup] Success");
|
|
88
|
+
} catch (error: any) {
|
|
89
|
+
console.error("setup error:", error);
|
|
90
|
+
set({ error: error.message });
|
|
91
|
+
throw error;
|
|
92
|
+
}
|
|
93
|
+
},
|
|
94
|
+
|
|
95
|
+
initAsleepConfig: async (config: AsleepConfig) => {
|
|
96
|
+
try {
|
|
97
|
+
const { addLog } = get();
|
|
98
|
+
addLog("[initAsleepConfig] Start");
|
|
99
|
+
|
|
100
|
+
const result = await AsleepModule.initAsleepConfig(
|
|
101
|
+
config.apiKey,
|
|
102
|
+
config.userId,
|
|
103
|
+
config.baseUrl,
|
|
104
|
+
config.callbackUrl
|
|
105
|
+
);
|
|
106
|
+
|
|
107
|
+
addLog("[initAsleepConfig] Success");
|
|
108
|
+
return result;
|
|
109
|
+
} catch (error: any) {
|
|
110
|
+
console.error("initAsleepConfig error:", error);
|
|
111
|
+
set({ error: error.message });
|
|
112
|
+
throw error;
|
|
113
|
+
}
|
|
114
|
+
},
|
|
115
|
+
|
|
116
|
+
startTracking: async () => {
|
|
117
|
+
try {
|
|
118
|
+
const { requestMicrophonePermission, addLog } = get();
|
|
119
|
+
addLog("[startTracking] Start");
|
|
120
|
+
|
|
121
|
+
const permission = await requestMicrophonePermission();
|
|
122
|
+
if (!permission) {
|
|
123
|
+
Alert.alert("Microphone permission denied");
|
|
124
|
+
throw new Error("Microphone permission denied");
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
set({ didClose: false, isTracking: true });
|
|
128
|
+
await AsleepModule.startTracking();
|
|
129
|
+
|
|
130
|
+
addLog("[startTracking] Success");
|
|
131
|
+
} catch (error: any) {
|
|
132
|
+
console.error("startTracking error:", error);
|
|
133
|
+
set({ error: error.message, isTracking: false });
|
|
134
|
+
throw error;
|
|
135
|
+
}
|
|
136
|
+
},
|
|
137
|
+
|
|
138
|
+
stopTracking: async () => {
|
|
139
|
+
try {
|
|
140
|
+
const { addLog } = get();
|
|
141
|
+
addLog("[stopTracking] Start");
|
|
142
|
+
|
|
143
|
+
const sessionId = await AsleepModule.stopTracking();
|
|
144
|
+
set({
|
|
145
|
+
didClose: true,
|
|
146
|
+
sessionId,
|
|
147
|
+
isTracking: false,
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
addLog(`[stopTracking] Success - sessionId: ${sessionId}`);
|
|
151
|
+
} catch (error: any) {
|
|
152
|
+
console.error("stopTracking error:", error);
|
|
153
|
+
set({ error: error.message });
|
|
154
|
+
throw error;
|
|
155
|
+
}
|
|
156
|
+
},
|
|
157
|
+
|
|
158
|
+
getReport: async (sessionId: string) => {
|
|
159
|
+
try {
|
|
160
|
+
const { addLog } = get();
|
|
161
|
+
addLog(`[getReport] sessionId: ${sessionId}`);
|
|
162
|
+
|
|
163
|
+
const report = await AsleepModule.getReport(sessionId);
|
|
164
|
+
const convertedReport = convertKeysToCamelCase(report);
|
|
165
|
+
|
|
166
|
+
addLog("[getReport] Success");
|
|
167
|
+
return convertedReport;
|
|
168
|
+
} catch (error: any) {
|
|
169
|
+
console.error("getReport error:", error);
|
|
170
|
+
set({ error: error.message });
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
},
|
|
174
|
+
|
|
175
|
+
getReportList: async (fromDate: string, toDate: string) => {
|
|
176
|
+
try {
|
|
177
|
+
const { addLog } = get();
|
|
178
|
+
addLog(`[getReportList] fromDate: ${fromDate}, toDate: ${toDate}`);
|
|
179
|
+
|
|
180
|
+
const reportList = await AsleepModule.getReportList(fromDate, toDate);
|
|
181
|
+
const convertedList = reportList.map(convertKeysToCamelCase);
|
|
182
|
+
|
|
183
|
+
addLog("[getReportList] Success");
|
|
184
|
+
return convertedList;
|
|
185
|
+
} catch (error: any) {
|
|
186
|
+
console.error("getReportList error:", error);
|
|
187
|
+
set({ error: error.message });
|
|
188
|
+
return [];
|
|
189
|
+
}
|
|
190
|
+
},
|
|
191
|
+
|
|
192
|
+
requestMicrophonePermission: async () => {
|
|
193
|
+
return AsleepModule.requestMicrophonePermission();
|
|
194
|
+
},
|
|
195
|
+
|
|
196
|
+
setCustomNotification: async (title: string, text: string) => {
|
|
197
|
+
if (Platform.OS === "android") {
|
|
198
|
+
await AsleepModule.setCustomNotification(title, text);
|
|
199
|
+
} else {
|
|
200
|
+
console.warn("setCustomNotification is not supported on this platform");
|
|
201
|
+
}
|
|
202
|
+
},
|
|
203
|
+
|
|
204
|
+
enableLog: (print: boolean) => {
|
|
205
|
+
set({ showDebugLog: print });
|
|
206
|
+
},
|
|
207
|
+
|
|
208
|
+
addEventListener: <K extends keyof AsleepEventType>(
|
|
209
|
+
eventType: K,
|
|
210
|
+
listener: (data: AsleepEventType[K]) => void
|
|
211
|
+
) => {
|
|
212
|
+
const subscription = emitter.addListener(eventType, listener);
|
|
213
|
+
return () => subscription.remove();
|
|
214
|
+
},
|
|
215
|
+
|
|
216
|
+
// internal actions
|
|
217
|
+
setError: (error) => set({ error }),
|
|
218
|
+
setUserId: (userId) => set({ userId }),
|
|
219
|
+
setSessionId: (sessionId) => set({ sessionId }),
|
|
220
|
+
setIsTracking: (isTracking) => set({ isTracking }),
|
|
221
|
+
setDidClose: (didClose) => set({ didClose }),
|
|
222
|
+
|
|
223
|
+
addLog: (log: string) => {
|
|
224
|
+
const { showDebugLog } = get();
|
|
225
|
+
const now = new Date();
|
|
226
|
+
const dateString = `${now.getHours().toString().padStart(2, "0")}:${now
|
|
227
|
+
.getMinutes()
|
|
228
|
+
.toString()
|
|
229
|
+
.padStart(2, "0")}:${now.getSeconds().toString().padStart(2, "0")}`;
|
|
230
|
+
|
|
231
|
+
const formattedLog = `[${dateString}]${log}`;
|
|
232
|
+
|
|
233
|
+
if (showDebugLog) {
|
|
234
|
+
console.log(`[Asleep]${formattedLog}`);
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
set({ log: formattedLog });
|
|
238
|
+
},
|
|
239
|
+
}))
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
// initialize event listeners
|
|
243
|
+
export const initializeAsleepListeners = () => {
|
|
244
|
+
const store = useAsleepStore.getState();
|
|
245
|
+
const {
|
|
246
|
+
addLog,
|
|
247
|
+
setUserId,
|
|
248
|
+
setSessionId,
|
|
249
|
+
setIsTracking,
|
|
250
|
+
setError,
|
|
251
|
+
setDidClose,
|
|
252
|
+
} = store;
|
|
253
|
+
|
|
254
|
+
// event handlers
|
|
255
|
+
const handlers = {
|
|
256
|
+
onUserJoined: (data: any) => {
|
|
257
|
+
setUserId(data.userId);
|
|
258
|
+
addLog(`[onUserJoined] userId: ${data.userId}`);
|
|
259
|
+
},
|
|
260
|
+
onUserJoinFailed: (error: any) => {
|
|
261
|
+
const errorString = JSON.stringify(error);
|
|
262
|
+
setError(errorString);
|
|
263
|
+
addLog(`[onUserJoinFailed] error: ${errorString}`);
|
|
264
|
+
},
|
|
265
|
+
onUserDeleted: (data: any) => {
|
|
266
|
+
setUserId(null);
|
|
267
|
+
addLog(`[onUserDeleted] userId: ${data.userId}`);
|
|
268
|
+
},
|
|
269
|
+
onTrackingCreated: (data: any) => {
|
|
270
|
+
setIsTracking(true);
|
|
271
|
+
if (data && data.sessionId) {
|
|
272
|
+
setSessionId(data.sessionId);
|
|
273
|
+
}
|
|
274
|
+
addLog(
|
|
275
|
+
`[onTrackingCreated]${
|
|
276
|
+
data?.sessionId ? ` sessionId: ${data.sessionId}` : ""
|
|
277
|
+
}`
|
|
278
|
+
);
|
|
279
|
+
},
|
|
280
|
+
onTrackingUploaded: (data: any) => {
|
|
281
|
+
addLog(`[onTrackingUploaded] sequence: ${data.sequence}`);
|
|
282
|
+
},
|
|
283
|
+
onTrackingClosed: (data: { sessionId: string }) => {
|
|
284
|
+
setSessionId(data.sessionId);
|
|
285
|
+
setDidClose(true);
|
|
286
|
+
setIsTracking(false);
|
|
287
|
+
addLog(`[onTrackingClosed] sessionId: ${data.sessionId}`);
|
|
288
|
+
},
|
|
289
|
+
onTrackingFailed: (error: any) => {
|
|
290
|
+
const errorString = JSON.stringify(error);
|
|
291
|
+
setError(errorString);
|
|
292
|
+
setIsTracking(false);
|
|
293
|
+
addLog(`[onTrackingFailed] error: ${errorString}`);
|
|
294
|
+
},
|
|
295
|
+
onTrackingInterrupted: () => {
|
|
296
|
+
setIsTracking(false);
|
|
297
|
+
addLog(`[onTrackingInterrupted]`);
|
|
298
|
+
},
|
|
299
|
+
onTrackingResumed: () => {
|
|
300
|
+
setIsTracking(true);
|
|
301
|
+
addLog(`[onTrackingResumed]`);
|
|
302
|
+
},
|
|
303
|
+
onMicPermissionDenied: () => {
|
|
304
|
+
addLog(`[onMicPermissionDenied]`);
|
|
305
|
+
},
|
|
306
|
+
onDebugLog: (data: any) => {
|
|
307
|
+
addLog(`[onDebugLog] message: ${data.message}`);
|
|
308
|
+
},
|
|
309
|
+
onSetupDidComplete: () => {
|
|
310
|
+
addLog(`[onSetupDidComplete]`);
|
|
311
|
+
},
|
|
312
|
+
onSetupDidFail: (data: any) => {
|
|
313
|
+
const errorString = JSON.stringify(data);
|
|
314
|
+
setError(errorString);
|
|
315
|
+
addLog(`[onSetupDidFail] error: ${errorString}`);
|
|
316
|
+
},
|
|
317
|
+
onSetupInProgress: (data: any) => {
|
|
318
|
+
addLog(`[onSetupInProgress] progress: ${data.progress}%`);
|
|
319
|
+
},
|
|
320
|
+
};
|
|
321
|
+
|
|
322
|
+
// register all event listeners
|
|
323
|
+
const subscriptions: (() => void)[] = [];
|
|
324
|
+
|
|
325
|
+
Object.entries(handlers).forEach(([eventType, handler]) => {
|
|
326
|
+
const subscription = emitter.addListener(eventType, handler);
|
|
327
|
+
subscriptions.push(() => subscription.remove());
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
// return cleanup function
|
|
331
|
+
return () => {
|
|
332
|
+
subscriptions.forEach((unsubscribe) => unsubscribe());
|
|
333
|
+
};
|
|
334
|
+
};
|