-
Notifications
You must be signed in to change notification settings - Fork 12
/
index.tsx
55 lines (49 loc) · 1.45 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { NativeModules, Platform } from 'react-native';
const LINKING_ERROR =
`The package 'react-native-live-activity' doesn't seem to be linked. Make sure: \n\n` +
Platform.select({ ios: "- You have run 'pod install'\n", default: '' }) +
'- You rebuilt the app after installing the package\n' +
'- You are not using Expo managed workflow\n';
const LiveActivity_ = NativeModules.LiveActivity
? NativeModules.LiveActivity
: new Proxy(
{},
{
get() {
throw new Error(LINKING_ERROR);
},
}
);
export default class LiveActivityManager<T extends unknown> {
startActivity(data: T): Promise<string | null> {
const dataString = JSON.stringify(data);
return LiveActivity_.startActivity(dataString);
}
async updateActivity(id: string, data: T) {
const dataString = JSON.stringify(data);
return LiveActivity_.updateActivity(id, dataString);
}
async endActivity(id: string) {
const result = await LiveActivity_.endActivity(id);
return result;
}
async listAllActivities(): Promise<
{
id: string;
data: T;
}[]
> {
return LiveActivity_.listAllActivities()
.then((activities: { id: string; data: string }[]) => {
return activities?.map((activity) => {
return {
id: activity.id,
data: JSON.parse(activity.data) as T,
};
});
})
.catch((err: Error) => {
console.error(err);
});
}
}