react-native-alarms 0.0.1 → 0.0.2

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.
@@ -0,0 +1,112 @@
1
+ /**
2
+ * AlarmService - High-level alarm management API
3
+ * Provides convenient wrapper methods for the native alarm module
4
+ */
5
+
6
+ import type {
7
+ AlarmConfig,
8
+ Alarm,
9
+ AlarmPermission,
10
+ AlarmEvent,
11
+ } from './specs/NativeAlarm';
12
+
13
+ type AlarmEventListener = (alarm: AlarmEvent) => void;
14
+
15
+ /**
16
+ * High-level alarm service for managing alarms
17
+ */
18
+ export class AlarmService {
19
+ /**
20
+ * Initialize the alarm service and set up event listeners
21
+ */
22
+ static initialize(): void;
23
+
24
+ /**
25
+ * Cleanup event listeners
26
+ */
27
+ static cleanup(): void;
28
+
29
+ /**
30
+ * Register a listener for when alarms fire
31
+ * @returns unsubscribe function
32
+ */
33
+ static onAlarmFired(listener: AlarmEventListener): () => void;
34
+
35
+ /**
36
+ * Register a listener for when alarms are dismissed
37
+ * @returns unsubscribe function
38
+ */
39
+ static onAlarmDismissed(listener: AlarmEventListener): () => void;
40
+
41
+ /**
42
+ * Register a listener for when alarms are snoozed
43
+ * @returns unsubscribe function
44
+ */
45
+ static onAlarmSnoozed(listener: AlarmEventListener): () => void;
46
+
47
+ /**
48
+ * Check the current alarm permissions
49
+ */
50
+ static checkPermission(): Promise<AlarmPermission>;
51
+
52
+ /**
53
+ * Request alarm permissions
54
+ */
55
+ static requestPermission(): Promise<AlarmPermission>;
56
+
57
+ /**
58
+ * Schedule a new alarm
59
+ * @returns alarm ID
60
+ */
61
+ static scheduleAlarm(config: AlarmConfig): Promise<string>;
62
+
63
+ /**
64
+ * Update an existing alarm
65
+ * @returns boolean indicating success
66
+ */
67
+ static updateAlarm(alarmId: string, config: AlarmConfig): Promise<boolean>;
68
+
69
+ /**
70
+ * Cancel an alarm
71
+ * @returns boolean if alarm existed
72
+ */
73
+ static cancelAlarm(alarmId: string): Promise<boolean>;
74
+
75
+ /**
76
+ * Cancel all alarms
77
+ */
78
+ static cancelAllAlarms(): Promise<void>;
79
+
80
+ /**
81
+ * Stop the currently ringing alarm
82
+ */
83
+ static stopAlarm(alarmId: string): Promise<void>;
84
+
85
+ /**
86
+ * Snooze the currently ringing alarm
87
+ * @param minutes - snooze duration (defaults to alarm's snoozeMinutes)
88
+ */
89
+ static snoozeAlarm(alarmId: string, minutes?: number): Promise<void>;
90
+
91
+ /**
92
+ * Get the currently ringing alarm (if any)
93
+ */
94
+ static getRingingAlarm(): Promise<Alarm | null>;
95
+
96
+ /**
97
+ * Get a specific alarm by ID
98
+ */
99
+ static getAlarm(alarmId: string): Promise<Alarm | null>;
100
+
101
+ /**
102
+ * Get all scheduled alarms
103
+ */
104
+ static getAllAlarms(): Promise<Alarm[]>;
105
+
106
+ /**
107
+ * Check if an alarm is scheduled
108
+ */
109
+ static isScheduled(alarmId: string): Promise<boolean>;
110
+ }
111
+
112
+ export default AlarmService;
@@ -0,0 +1,196 @@
1
+ /**
2
+ * AlarmService - High-level alarm management API
3
+ *
4
+ * This module provides a convenient wrapper around the native alarm module
5
+ * with additional features like event listeners and state management.
6
+ */
7
+
8
+ import { NativeEventEmitter, NativeModules } from 'react-native';
9
+ import type {
10
+ AlarmConfig,
11
+ Alarm,
12
+ AlarmPermission,
13
+ AlarmEvent,
14
+ } from './specs/NativeAlarm';
15
+
16
+ const { Alarm: NativeAlarm } = NativeModules;
17
+
18
+ // Guard against module not being linked
19
+ const alarm = NativeAlarm ? require('./specs/NativeAlarm').default : null;
20
+
21
+ const alarmEmitter = NativeAlarm ? new NativeEventEmitter(NativeAlarm) : null;
22
+
23
+ type AlarmEventListener = (alarm: AlarmEvent) => void;
24
+
25
+ /**
26
+ * High-level alarm service for managing alarms
27
+ */
28
+ export class AlarmService {
29
+ private static listeners: {
30
+ fired: Set<AlarmEventListener>;
31
+ dismissed: Set<AlarmEventListener>;
32
+ snoozed: Set<AlarmEventListener>;
33
+ } = {
34
+ fired: new Set(),
35
+ dismissed: new Set(),
36
+ snoozed: new Set(),
37
+ };
38
+
39
+ private static subscriptions: any[] = [];
40
+
41
+ /**
42
+ * Initialize the alarm service and set up event listeners
43
+ */
44
+ static initialize() {
45
+ if (!alarmEmitter || !this.subscriptions.length) {
46
+ this.subscriptions = [
47
+ alarmEmitter?.addListener('onAlarmFired', (data: any) => {
48
+ this.listeners.fired.forEach(listener => listener(data as AlarmEvent));
49
+ }),
50
+ alarmEmitter?.addListener('onAlarmDismissed', (data: any) => {
51
+ this.listeners.dismissed.forEach(listener => listener(data as AlarmEvent));
52
+ }),
53
+ alarmEmitter?.addListener('onAlarmSnoozed', (data: any) => {
54
+ this.listeners.snoozed.forEach(listener => listener(data as AlarmEvent));
55
+ }),
56
+ ].filter(Boolean);
57
+ }
58
+ }
59
+
60
+ /**
61
+ * Cleanup event listeners
62
+ */
63
+ static cleanup() {
64
+ this.subscriptions.forEach(sub => sub?.remove());
65
+ this.subscriptions = [];
66
+ }
67
+
68
+ /**
69
+ * Register a listener for when alarms fire
70
+ */
71
+ static onAlarmFired(listener: AlarmEventListener) {
72
+ this.initialize();
73
+ this.listeners.fired.add(listener);
74
+ return () => this.listeners.fired.delete(listener);
75
+ }
76
+
77
+ /**
78
+ * Register a listener for when alarms are dismissed
79
+ */
80
+ static onAlarmDismissed(listener: AlarmEventListener) {
81
+ this.initialize();
82
+ this.listeners.dismissed.add(listener);
83
+ return () => this.listeners.dismissed.delete(listener);
84
+ }
85
+
86
+ /**
87
+ * Register a listener for when alarms are snoozed
88
+ */
89
+ static onAlarmSnoozed(listener: AlarmEventListener) {
90
+ this.initialize();
91
+ this.listeners.snoozed.add(listener);
92
+ return () => this.listeners.snoozed.delete(listener);
93
+ }
94
+
95
+ /**
96
+ * Check the current alarm permissions
97
+ */
98
+ static async checkPermission(): Promise<AlarmPermission> {
99
+ if (!alarm) throw new Error('Alarm module not linked');
100
+ return alarm.checkPermission();
101
+ }
102
+
103
+ /**
104
+ * Request alarm permissions
105
+ */
106
+ static async requestPermission(): Promise<AlarmPermission> {
107
+ if (!alarm) throw new Error('Alarm module not linked');
108
+ return alarm.requestPermission();
109
+ }
110
+
111
+ /**
112
+ * Schedule a new alarm
113
+ */
114
+ static async scheduleAlarm(config: AlarmConfig): Promise<string> {
115
+ if (!alarm) throw new Error('Alarm module not linked');
116
+ return alarm.scheduleAlarm(config);
117
+ }
118
+
119
+ /**
120
+ * Update an existing alarm
121
+ */
122
+ static async updateAlarm(
123
+ alarmId: string,
124
+ config: AlarmConfig,
125
+ ): Promise<boolean> {
126
+ if (!alarm) throw new Error('Alarm module not linked');
127
+ return alarm.updateAlarm(alarmId, config);
128
+ }
129
+
130
+ /**
131
+ * Cancel an alarm
132
+ */
133
+ static async cancelAlarm(alarmId: string): Promise<boolean> {
134
+ if (!alarm) throw new Error('Alarm module not linked');
135
+ return alarm.cancelAlarm(alarmId);
136
+ }
137
+
138
+ /**
139
+ * Cancel all alarms
140
+ */
141
+ static async cancelAllAlarms(): Promise<void> {
142
+ if (!alarm) throw new Error('Alarm module not linked');
143
+ return alarm.cancelAllAlarms();
144
+ }
145
+
146
+ /**
147
+ * Stop the currently ringing alarm
148
+ */
149
+ static async stopAlarm(alarmId: string): Promise<void> {
150
+ if (!alarm) throw new Error('Alarm module not linked');
151
+ return alarm.stopAlarm(alarmId);
152
+ }
153
+
154
+ /**
155
+ * Snooze the currently ringing alarm
156
+ */
157
+ static async snoozeAlarm(alarmId: string, minutes?: number): Promise<void> {
158
+ if (!alarm) throw new Error('Alarm module not linked');
159
+ return alarm.snoozeAlarm(alarmId, minutes ?? 5);
160
+ }
161
+
162
+ /**
163
+ * Get the currently ringing alarm (if any)
164
+ */
165
+ static async getRingingAlarm(): Promise<Alarm | null> {
166
+ if (!alarm) throw new Error('Alarm module not linked');
167
+ return alarm.getRingingAlarm();
168
+ }
169
+
170
+ /**
171
+ * Get a specific alarm by ID
172
+ */
173
+ static async getAlarm(alarmId: string): Promise<Alarm | null> {
174
+ if (!alarm) throw new Error('Alarm module not linked');
175
+ return alarm.getAlarm(alarmId);
176
+ }
177
+
178
+ /**
179
+ * Get all scheduled alarms
180
+ */
181
+ static async getAllAlarms(): Promise<Alarm[]> {
182
+ if (!alarm) throw new Error('Alarm module not linked');
183
+ return alarm.getAllAlarms();
184
+ }
185
+
186
+ /**
187
+ * Check if an alarm is scheduled
188
+ */
189
+ static async isScheduled(alarmId: string): Promise<boolean> {
190
+ if (!alarm) throw new Error('Alarm module not linked');
191
+ return alarm.isScheduled(alarmId);
192
+ }
193
+ }
194
+
195
+ // Export a singleton instance
196
+ export default AlarmService;