forked from kingstinct/react-native-device-activity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitoring.tsx
More file actions
167 lines (149 loc) · 4.63 KB
/
Monitoring.tsx
File metadata and controls
167 lines (149 loc) · 4.63 KB
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import React, { useCallback, useEffect, useMemo } from "react";
import {
Button,
NativeSyntheticEvent,
StyleSheet,
Text,
View,
} from "react-native";
import * as ReactNativeDeviceActivity from "react-native-device-activity";
import {
ActivitySelectionWithMetadata,
DeviceActivityEvent,
} from "react-native-device-activity";
const startMonitoring = (activitySelection: string) => {
const timeLimitMinutes = 1;
const totalEvents = (1 * 60) / timeLimitMinutes;
let events: DeviceActivityEvent[] = [];
// loop over each our of the day
for (let hour = 0; hour < 24; hour++) {
for (let i = 0; i < totalEvents; i++) {
const name = `${(i + 1) * timeLimitMinutes}_minutes_today`;
events.push({
eventName: name,
familyActivitySelection: activitySelection,
threshold: { minute: (i + 1) * timeLimitMinutes },
});
}
ReactNativeDeviceActivity.startMonitoring(
"DeviceActivity.AppLoggedTimeDaily." + hour,
{
intervalStart: { hour, minute: 0, second: 0 },
intervalEnd: { hour, minute: 59, second: 59 },
repeats: true,
},
events,
);
events = [];
}
};
export default function App() {
const [largestEvent, setLargestEvent] = React.useState<null | {
minutesRegistered: number;
registeredAt: Date;
}>(null);
const [familyActivitySelection, setFamilyActivitySelection] = React.useState<
string | null
>(null);
const refreshEvents = useCallback(() => {
const eventsParsed = ReactNativeDeviceActivity.getEvents();
const today = new Date();
const todaysThresholdsReached = eventsParsed.filter(
({ callbackName, lastCalledAt }) =>
callbackName === "eventDidReachThreshold" &&
lastCalledAt.getHours() === today.getHours() &&
lastCalledAt.getDate() === today.getDate() &&
lastCalledAt.getMonth() === today.getMonth() &&
lastCalledAt.getFullYear() === today.getFullYear(),
);
const eventsOccurredToday = todaysThresholdsReached.map((event) => {
const minutesRegistered = parseInt(
event.eventName!.split("_minutes_today")[0],
10,
);
return {
activity: event.activityName,
event: event.eventName,
registeredAt: event.lastCalledAt,
minutesRegistered,
};
});
const largestMinutesRegistered = eventsOccurredToday.reduce(
(acc, event) => {
return event.minutesRegistered > acc.minutesRegistered ? event : acc;
},
{
minutesRegistered: 0,
event: "none" as string | undefined,
registeredAt: new Date(),
},
);
setLargestEvent(largestMinutesRegistered);
}, []);
useEffect(() => {
ReactNativeDeviceActivity.requestAuthorization();
const listener = ReactNativeDeviceActivity.onDeviceActivityMonitorEvent(
(event) => {
refreshEvents();
},
);
return () => {
listener.remove();
};
}, [refreshEvents]);
return useMemo(
() => (
<View style={styles.container}>
<Button
title="Start monitoring"
onPress={() => startMonitoring(familyActivitySelection!)}
/>
<Button
title="Stop monitoring"
onPress={() => ReactNativeDeviceActivity.stopMonitoring()}
/>
<Button title="Get events" onPress={refreshEvents} />
<ReactNativeDeviceActivity.DeviceActivitySelectionView
style={{
width: 200,
height: 200,
backgroundColor: "red",
borderRadius: 10,
borderWidth: 10,
borderColor: "red",
}}
onSelectionChange={(
event: NativeSyntheticEvent<ActivitySelectionWithMetadata>,
) => {
if (
event.nativeEvent.familyActivitySelection !==
familyActivitySelection
) {
setFamilyActivitySelection(
event.nativeEvent.familyActivitySelection,
);
// alert(event.nativeEvent.familyActivitySelection);
}
// console.log(event.nativeEvent.familyActivitySelection);
}}
familyActivitySelection={familyActivitySelection}
>
<View
style={{ backgroundColor: "green", height: 100 }}
pointerEvents="none"
/>
</ReactNativeDeviceActivity.DeviceActivitySelectionView>
<Text>{JSON.stringify(largestEvent, null, 2)}</Text>
</View>
),
[familyActivitySelection, largestEvent],
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
});