@zulfio/react-native-alarm-kit 1.0.2 → 1.0.3

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.
Files changed (2) hide show
  1. package/ios/AlarmKit.swift +75 -72
  2. package/package.json +1 -1
@@ -1,4 +1,6 @@
1
+ #if canImport(AlarmKit)
1
2
  import AlarmKit
3
+ #endif
2
4
  import SwiftUI
3
5
  import NitroModules
4
6
 
@@ -6,6 +8,7 @@ class AlarmKit: HybridAlarmKitSpec {
6
8
  @available(iOS 15.1, *)
7
9
  public func requestAlarmPermission() throws -> NitroModules.Promise<Bool> {
8
10
  return NitroModules.Promise.async {
11
+ #if canImport(AlarmKit)
9
12
  if #available(iOS 26.0, *) {
10
13
  let manager = AlarmManager.shared
11
14
  let state: AlarmManager.AuthorizationState
@@ -16,22 +19,23 @@ class AlarmKit: HybridAlarmKitSpec {
16
19
  throw error
17
20
  }
18
21
  return state == .authorized
19
- } else {
20
- throw NSError(
21
- domain: "AlarmKitError",
22
- code: 1,
23
- userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
24
- )
25
22
  }
23
+ #endif
24
+ throw NSError(
25
+ domain: "AlarmKitError",
26
+ code: 1,
27
+ userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
28
+ )
26
29
  }
27
30
  }
28
-
31
+
29
32
  @available(iOS 15.1, *)
30
33
  public func scheduleFixedAlarm(title: String, stopBtn: CustomizableAlarmButton, tintColor: String, secondaryBtn: CustomizableAlarmButton?, timestamp: Double?, countdown: AlarmCountdown?) throws -> NitroModules.Promise<String> {
31
34
  return NitroModules.Promise.async {
35
+ #if canImport(AlarmKit)
32
36
  if #available(iOS 26.0, *) {
33
37
  let manager = AlarmManager.shared
34
-
38
+
35
39
  let stopButton = AlarmButton(
36
40
  text: LocalizedStringResource(stringLiteral: stopBtn.text),
37
41
  textColor: Color(StringToColor(hex: stopBtn.textColor)),
@@ -45,7 +49,6 @@ class AlarmKit: HybridAlarmKitSpec {
45
49
  textColor: Color(StringToColor(hex: btn.textColor)),
46
50
  systemImageName: btn.icon
47
51
  )
48
-
49
52
  alertPresentationAlert = AlarmPresentation.Alert(
50
53
  title: LocalizedStringResource(stringLiteral: title),
51
54
  stopButton: stopButton,
@@ -60,25 +63,24 @@ class AlarmKit: HybridAlarmKitSpec {
60
63
  }
61
64
 
62
65
  let presentation = AlarmPresentation(alert: alertPresentationAlert)
63
- let countdown = Alarm.CountdownDuration(preAlert: countdown?.preAlert, postAlert: countdown?.postAlert)
64
-
66
+ let countdownDuration = Alarm.CountdownDuration(preAlert: countdown?.preAlert, postAlert: countdown?.postAlert)
67
+
65
68
  nonisolated struct EmptyMetadata: AlarmMetadata {}
66
69
  let attributes = AlarmAttributes<EmptyMetadata>(presentation: presentation, tintColor: Color(StringToColor(hex: tintColor)))
67
70
 
68
71
  var schedule: Alarm.Schedule? = nil
69
-
70
72
  if let timestamp = timestamp {
71
73
  let date = Date(timeIntervalSince1970: timestamp)
72
74
  schedule = Alarm.Schedule.fixed(date)
73
75
  }
74
-
76
+
75
77
  let configuration = AlarmManager.AlarmConfiguration(
76
- countdownDuration: countdown,
78
+ countdownDuration: countdownDuration,
77
79
  schedule: schedule,
78
80
  attributes: attributes,
79
81
  sound: .default
80
82
  )
81
-
83
+
82
84
  let uuid = UUID()
83
85
  do {
84
86
  _ = try await manager.schedule(id: uuid, configuration: configuration)
@@ -86,14 +88,13 @@ class AlarmKit: HybridAlarmKitSpec {
86
88
  } catch {
87
89
  throw error
88
90
  }
89
- } else {
90
- print("error")
91
- throw NSError(
92
- domain: "AlarmKitError",
93
- code: 1,
94
- userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
95
- )
96
91
  }
92
+ #endif
93
+ throw NSError(
94
+ domain: "AlarmKitError",
95
+ code: 1,
96
+ userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
97
+ )
97
98
  }
98
99
  }
99
100
 
@@ -109,6 +110,7 @@ class AlarmKit: HybridAlarmKitSpec {
109
110
  countdown: AlarmCountdown?
110
111
  ) throws -> NitroModules.Promise<String> {
111
112
  return NitroModules.Promise.async {
113
+ #if canImport(AlarmKit)
112
114
  if #available(iOS 26.0, *) {
113
115
  let manager = AlarmManager.shared
114
116
 
@@ -117,7 +119,6 @@ class AlarmKit: HybridAlarmKitSpec {
117
119
  textColor: Color(StringToColor(hex: stopBtn.textColor)),
118
120
  systemImageName: stopBtn.icon
119
121
  )
120
-
121
122
  let alertPresentationAlert: AlarmPresentation.Alert
122
123
 
123
124
  if let btn = secondaryBtn {
@@ -126,7 +127,6 @@ class AlarmKit: HybridAlarmKitSpec {
126
127
  textColor: Color(StringToColor(hex: btn.textColor)),
127
128
  systemImageName: btn.icon
128
129
  )
129
-
130
130
  alertPresentationAlert = AlarmPresentation.Alert(
131
131
  title: LocalizedStringResource(stringLiteral: title),
132
132
  stopButton: stopButton,
@@ -141,30 +141,29 @@ class AlarmKit: HybridAlarmKitSpec {
141
141
  }
142
142
 
143
143
  let presentation = AlarmPresentation(alert: alertPresentationAlert)
144
- let countdown = Alarm.CountdownDuration(preAlert: countdown?.preAlert, postAlert: countdown?.postAlert)
144
+ let countdownDuration = Alarm.CountdownDuration(preAlert: countdown?.preAlert, postAlert: countdown?.postAlert)
145
145
 
146
146
  nonisolated struct EmptyMetadata: AlarmMetadata {}
147
147
  let attributes = AlarmAttributes<EmptyMetadata>(presentation: presentation, tintColor: Color(StringToColor(hex: tintColor)))
148
148
 
149
149
  let time = Alarm.Schedule.Relative.Time(hour: Int(hour), minute: Int(minute))
150
-
151
150
  let localeWeekdays: [Locale.Weekday] = repeats.map { alarmWeekday in
152
- switch alarmWeekday {
153
- case .monday: return .monday
154
- case .tuesday: return .tuesday
155
- case .wednesday: return .wednesday
156
- case .thursday: return .thursday
157
- case .friday: return .friday
158
- case .saturday: return .saturday
159
- case .sunday: return .sunday
160
- }
151
+ switch alarmWeekday {
152
+ case .monday: return .monday
153
+ case .tuesday: return .tuesday
154
+ case .wednesday: return .wednesday
155
+ case .thursday: return .thursday
156
+ case .friday: return .friday
157
+ case .saturday: return .saturday
158
+ case .sunday: return .sunday
159
+ }
161
160
  }
162
161
  let recurrence = Alarm.Schedule.Relative.Recurrence.weekly(localeWeekdays)
163
162
  let relativeSchedule = Alarm.Schedule.Relative(time: time, repeats: recurrence)
164
163
  let schedule = Alarm.Schedule.relative(relativeSchedule)
165
164
 
166
165
  let configuration = AlarmManager.AlarmConfiguration(
167
- countdownDuration: countdown,
166
+ countdownDuration: countdownDuration,
168
167
  schedule: schedule,
169
168
  attributes: attributes,
170
169
  sound: .default
@@ -177,19 +176,20 @@ class AlarmKit: HybridAlarmKitSpec {
177
176
  } catch {
178
177
  throw error
179
178
  }
180
- } else {
181
- throw NSError(
182
- domain: "AlarmKitError",
183
- code: 1,
184
- userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
185
- )
186
179
  }
180
+ #endif
181
+ throw NSError(
182
+ domain: "AlarmKitError",
183
+ code: 1,
184
+ userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
185
+ )
187
186
  }
188
187
  }
189
188
 
190
189
  @available(iOS 15.1, *)
191
190
  public func cancelAlarm(id: String) throws -> NitroModules.Promise<Bool> {
192
191
  return NitroModules.Promise.async {
192
+ #if canImport(AlarmKit)
193
193
  if #available(iOS 26.0, *) {
194
194
  guard let uuid = UUID(uuidString: id) else {
195
195
  throw NSError(
@@ -204,38 +204,40 @@ class AlarmKit: HybridAlarmKitSpec {
204
204
  } catch {
205
205
  throw error
206
206
  }
207
- } else {
208
- throw NSError(
209
- domain: "AlarmKitError",
210
- code: 1,
211
- userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
212
- )
213
207
  }
208
+ #endif
209
+ throw NSError(
210
+ domain: "AlarmKitError",
211
+ code: 1,
212
+ userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
213
+ )
214
214
  }
215
215
  }
216
216
 
217
217
  @available(iOS 15.1, *)
218
218
  public func cancelAllAlarms() throws -> NitroModules.Promise<Bool> {
219
219
  return NitroModules.Promise.async {
220
+ #if canImport(AlarmKit)
220
221
  if #available(iOS 26.0, *) {
221
222
  let alarms = await AlarmManager.shared.alarms
222
223
  for alarm in alarms {
223
224
  try await AlarmManager.shared.cancel(id: alarm.id)
224
225
  }
225
226
  return true
226
- } else {
227
- throw NSError(
228
- domain: "AlarmKitError",
229
- code: 1,
230
- userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
231
- )
232
227
  }
228
+ #endif
229
+ throw NSError(
230
+ domain: "AlarmKitError",
231
+ code: 1,
232
+ userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
233
+ )
233
234
  }
234
235
  }
235
236
 
236
237
  @available(iOS 15.1, *)
237
238
  public func getAlarm(id: String) throws -> NitroModules.Promise<String?> {
238
239
  return NitroModules.Promise.async {
240
+ #if canImport(AlarmKit)
239
241
  if #available(iOS 26.0, *) {
240
242
  guard let uuid = UUID(uuidString: id) else { return nil }
241
243
  let alarms = await AlarmManager.shared.alarms
@@ -246,45 +248,46 @@ class AlarmKit: HybridAlarmKitSpec {
246
248
  case .snoozed: return "snoozed"
247
249
  default: return "unknown"
248
250
  }
249
- } else {
250
- throw NSError(
251
- domain: "AlarmKitError",
252
- code: 1,
253
- userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
254
- )
255
251
  }
252
+ #endif
253
+ throw NSError(
254
+ domain: "AlarmKitError",
255
+ code: 1,
256
+ userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
257
+ )
256
258
  }
257
259
  }
258
260
 
259
261
  @available(iOS 15.1, *)
260
262
  public func getAllAlarms() throws -> NitroModules.Promise<[String]> {
261
263
  return NitroModules.Promise.async {
264
+ #if canImport(AlarmKit)
262
265
  if #available(iOS 26.0, *) {
263
266
  let alarms = await AlarmManager.shared.alarms
264
267
  return alarms.map { $0.id.uuidString }
265
- } else {
266
- throw NSError(
267
- domain: "AlarmKitError",
268
- code: 1,
269
- userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
270
- )
271
268
  }
269
+ #endif
270
+ throw NSError(
271
+ domain: "AlarmKitError",
272
+ code: 1,
273
+ userInfo: [NSLocalizedDescriptionKey: "AlarmKit requires iOS 26.0 or later"]
274
+ )
272
275
  }
273
276
  }
274
277
  }
275
278
 
276
- func StringToColor (hex:String) -> UIColor {
277
- var cString:String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
279
+ func StringToColor(hex: String) -> UIColor {
280
+ var cString: String = hex.trimmingCharacters(in: .whitespacesAndNewlines).uppercased()
278
281
 
279
- if (cString.hasPrefix("#")) {
282
+ if cString.hasPrefix("#") {
280
283
  cString.remove(at: cString.startIndex)
281
284
  }
282
285
 
283
- if ((cString.count) != 6) {
286
+ if cString.count != 6 {
284
287
  return UIColor.gray
285
288
  }
286
289
 
287
- var rgbValue:UInt64 = 0
290
+ var rgbValue: UInt64 = 0
288
291
  Scanner(string: cString).scanHexInt64(&rgbValue)
289
292
 
290
293
  return UIColor(
@@ -293,4 +296,4 @@ func StringToColor (hex:String) -> UIColor {
293
296
  blue: CGFloat(rgbValue & 0x0000FF) / 255.0,
294
297
  alpha: CGFloat(1.0)
295
298
  )
296
- }
299
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zulfio/react-native-alarm-kit",
3
- "version": "1.0.2",
3
+ "version": "1.0.3",
4
4
  "description": "This library provides a simple and modern interface for working with alarms in your React Native app using Apple’s latest AlarmKit framework.",
5
5
  "main": "./lib/module/index.js",
6
6
  "types": "./lib/typescript/src/index.d.ts",