@trycourier/courier-react-native 2.0.0-beta2 → 2.0.0-beta4

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 (40) hide show
  1. package/ios/CourierReactNativeModule.m +43 -4
  2. package/ios/CourierReactNativeModule.swift +87 -79
  3. package/ios/CourierReactNativeViewManager.m +1 -3
  4. package/ios/CourierReactNativeViewManager.swift +4 -17
  5. package/lib/commonjs/hooks/CourierProvider.js +258 -0
  6. package/lib/commonjs/hooks/CourierProvider.js.map +1 -0
  7. package/lib/commonjs/index.js +205 -12
  8. package/lib/commonjs/index.js.map +1 -1
  9. package/lib/commonjs/models/CourierAuthenticationListener.js +21 -0
  10. package/lib/commonjs/models/CourierAuthenticationListener.js.map +1 -0
  11. package/lib/commonjs/models/CourierPushListener.js +15 -0
  12. package/lib/commonjs/models/CourierPushListener.js.map +1 -0
  13. package/lib/commonjs/views/CourierInboxView.js +4 -2
  14. package/lib/commonjs/views/CourierInboxView.js.map +1 -1
  15. package/lib/module/hooks/CourierProvider.js +245 -0
  16. package/lib/module/hooks/CourierProvider.js.map +1 -0
  17. package/lib/module/index.js +166 -13
  18. package/lib/module/index.js.map +1 -1
  19. package/lib/module/models/CourierAuthenticationListener.js +13 -0
  20. package/lib/module/models/CourierAuthenticationListener.js.map +1 -0
  21. package/lib/module/models/CourierPushListener.js +8 -0
  22. package/lib/module/models/CourierPushListener.js.map +1 -0
  23. package/lib/module/views/CourierInboxView.js +4 -2
  24. package/lib/module/views/CourierInboxView.js.map +1 -1
  25. package/lib/typescript/hooks/CourierProvider.d.ts +57 -0
  26. package/lib/typescript/hooks/CourierProvider.d.ts.map +1 -0
  27. package/lib/typescript/index.d.ts +82 -1
  28. package/lib/typescript/index.d.ts.map +1 -1
  29. package/lib/typescript/models/CourierAuthenticationListener.d.ts +7 -0
  30. package/lib/typescript/models/CourierAuthenticationListener.d.ts.map +1 -0
  31. package/lib/typescript/models/CourierPushListener.d.ts +7 -0
  32. package/lib/typescript/models/CourierPushListener.d.ts.map +1 -0
  33. package/lib/typescript/views/CourierInboxView.d.ts +4 -2
  34. package/lib/typescript/views/CourierInboxView.d.ts.map +1 -1
  35. package/package.json +1 -1
  36. package/src/hooks/CourierProvider.tsx +355 -0
  37. package/src/index.tsx +186 -13
  38. package/src/models/CourierAuthenticationListener.tsx +19 -0
  39. package/src/models/CourierPushListener.tsx +13 -0
  40. package/src/views/CourierInboxView.tsx +5 -4
@@ -3,7 +3,11 @@
3
3
  @interface RCT_EXTERN_MODULE(CourierReactNativeModule, NSObject)
4
4
 
5
5
  RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
6
- setDebugMode: (BOOL*)isDebugging
6
+ setDebugMode: (BOOL)isDebugging
7
+ )
8
+
9
+ RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
10
+ iOSForegroundPresentationOptions: (NSDictionary*)params
7
11
  )
8
12
 
9
13
  RCT_EXTERN_METHOD(
@@ -19,8 +23,39 @@ RCT_EXTERN_METHOD(
19
23
  withRejecter: (RCTPromiseRejectBlock)reject
20
24
  )
21
25
 
26
+ RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
27
+ getUserId
28
+ )
29
+
30
+ RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
31
+ addAuthenticationListener
32
+ )
33
+
34
+ RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
35
+ removeAuthenticationListener: (NSString*)listenerId
36
+ )
37
+
38
+ RCT_EXTERN_METHOD(
39
+ getNotificationPermissionStatus: (RCTPromiseResolveBlock)resolve
40
+ withRejecter: (RCTPromiseRejectBlock)reject
41
+ )
42
+
22
43
  RCT_EXTERN_METHOD(
23
- getUserId: (RCTPromiseResolveBlock)resolve
44
+ requestNotificationPermission: (RCTPromiseResolveBlock)resolve
45
+ withRejecter: (RCTPromiseRejectBlock)reject
46
+ )
47
+
48
+ RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
49
+ getApnsToken
50
+ )
51
+
52
+ RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
53
+ getFcmToken
54
+ )
55
+
56
+ RCT_EXTERN_METHOD(
57
+ setFcmToken: (NSString*)token
58
+ withResolver: (RCTPromiseResolveBlock)resolve
24
59
  withRejecter: (RCTPromiseRejectBlock)reject
25
60
  )
26
61
 
@@ -42,7 +77,7 @@ RCT_EXTERN_METHOD(
42
77
  )
43
78
 
44
79
  RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
45
- addInboxListener: (NSString*)listenerId
80
+ addInboxListener
46
81
  )
47
82
 
48
83
  RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
@@ -60,7 +95,11 @@ RCT_EXTERN_METHOD(
60
95
  )
61
96
 
62
97
  RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
63
- setInboxPaginationLimit: (nonnull NSNumber*)limit
98
+ setInboxPaginationLimit: (double)limit
99
+ )
100
+
101
+ RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(
102
+ registerPushNotificationClickedOnKilledState
64
103
  )
65
104
 
66
105
  @end
@@ -4,9 +4,19 @@ import Courier_iOS
4
4
  class CourierReactNativeModule: RCTEventEmitter {
5
5
 
6
6
  private static let COURIER_ERROR_TAG = "Courier iOS SDK Error"
7
- internal static let COURIER_PUSH_NOTIFICATION_CLICKED_EVENT = "pushNotificationClicked"
8
- internal static let COURIER_PUSH_NOTIFICATION_DELIVERED_EVENT = "pushNotificationDelivered"
9
- private static let COURIER_PUSH_NOTIFICATION_DEBUG_LOG_EVENT = "courierDebugEvent"
7
+
8
+ class LogEvents {
9
+ internal static let DEBUG_LOG_EVENT = "courierDebugEvent"
10
+ }
11
+
12
+ class AuthEvents {
13
+ internal static let USER_CHANGED = "courierAuthUserChanged"
14
+ }
15
+
16
+ class PushEvents {
17
+ internal static let CLICKED_EVENT = "pushNotificationClicked"
18
+ internal static let DELIVERED_EVENT = "pushNotificationDelivered"
19
+ }
10
20
 
11
21
  class InboxEvents {
12
22
  internal static let INITIAL_LOADING = "inboxInitialLoad"
@@ -14,6 +24,7 @@ class CourierReactNativeModule: RCTEventEmitter {
14
24
  internal static let MESSAGES_CHANGED = "inboxMessagesChanged"
15
25
  }
16
26
 
27
+ private var authListeners = [String: CourierAuthenticationListener]()
17
28
  private var inboxListeners = [String: CourierInboxListener]()
18
29
 
19
30
  private var hasListeners = false
@@ -37,12 +48,6 @@ class CourierReactNativeModule: RCTEventEmitter {
37
48
 
38
49
  }
39
50
 
40
- @objc(setDebugMode:)
41
- func setDebugMode(isDebugging: Bool) -> String {
42
- Courier.shared.isDebugging = isDebugging
43
- return String(describing: Courier.shared.isDebugging)
44
- }
45
-
46
51
  override func startObserving() {
47
52
 
48
53
  hasListeners = true
@@ -50,7 +55,7 @@ class CourierReactNativeModule: RCTEventEmitter {
50
55
  // setup listeners
51
56
  Courier.shared.logListener = { log in
52
57
  self.sendEvent(
53
- withName: CourierReactNativeModule.COURIER_PUSH_NOTIFICATION_DEBUG_LOG_EVENT,
58
+ withName: CourierReactNativeModule.LogEvents.DEBUG_LOG_EVENT,
54
59
  body: log
55
60
  )
56
61
  }
@@ -69,19 +74,25 @@ class CourierReactNativeModule: RCTEventEmitter {
69
74
  notificationCenter.addObserver(
70
75
  self,
71
76
  selector: #selector(pushNotificationClicked),
72
- name: Notification.Name(rawValue: CourierReactNativeModule.COURIER_PUSH_NOTIFICATION_CLICKED_EVENT),
77
+ name: Notification.Name(rawValue: CourierReactNativeModule.PushEvents.CLICKED_EVENT),
73
78
  object: nil
74
79
  )
75
80
 
76
81
  notificationCenter.addObserver(
77
82
  self,
78
83
  selector: #selector(pushNotificationDelivered),
79
- name: Notification.Name(rawValue: CourierReactNativeModule.COURIER_PUSH_NOTIFICATION_DELIVERED_EVENT),
84
+ name: Notification.Name(rawValue: CourierReactNativeModule.PushEvents.DELIVERED_EVENT),
80
85
  object: nil
81
86
  )
82
87
 
83
88
  }
84
89
 
90
+ @objc(setDebugMode:)
91
+ func setDebugMode(isDebugging: Bool) -> String {
92
+ Courier.shared.isDebugging = isDebugging
93
+ return String(describing: Courier.shared.isDebugging)
94
+ }
95
+
85
96
  private func sendMessage(name: String, message: [AnyHashable: Any]?) {
86
97
 
87
98
  guard let message = message else {
@@ -103,7 +114,7 @@ class CourierReactNativeModule: RCTEventEmitter {
103
114
 
104
115
  lastClickedMessage = notification.userInfo
105
116
  sendMessage(
106
- name: CourierReactNativeModule.COURIER_PUSH_NOTIFICATION_CLICKED_EVENT,
117
+ name: CourierReactNativeModule.PushEvents.CLICKED_EVENT,
107
118
  message: lastClickedMessage
108
119
  )
109
120
 
@@ -112,19 +123,23 @@ class CourierReactNativeModule: RCTEventEmitter {
112
123
  @objc private func pushNotificationDelivered(notification: Notification) {
113
124
 
114
125
  sendMessage(
115
- name: CourierReactNativeModule.COURIER_PUSH_NOTIFICATION_DELIVERED_EVENT,
126
+ name: CourierReactNativeModule.PushEvents.DELIVERED_EVENT,
116
127
  message: notification.userInfo
117
128
  )
118
129
 
119
130
  }
120
131
 
121
- @objc func registerPushNotificationClickedOnKilledState() {
132
+ @objc func registerPushNotificationClickedOnKilledState() -> String {
133
+
134
+ let event = CourierReactNativeModule.PushEvents.CLICKED_EVENT
122
135
 
123
136
  sendMessage(
124
- name: CourierReactNativeModule.COURIER_PUSH_NOTIFICATION_CLICKED_EVENT,
137
+ name: event,
125
138
  message: lastClickedMessage
126
139
  )
127
140
 
141
+ return event
142
+
128
143
  }
129
144
 
130
145
  @objc(getNotificationPermissionStatus: withRejecter:)
@@ -176,19 +191,42 @@ class CourierReactNativeModule: RCTEventEmitter {
176
191
 
177
192
  }
178
193
 
179
- @objc(getUserId: withRejecter:)
180
- func getUserId(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
194
+ @objc func getUserId() -> String? {
195
+ return Courier.shared.userId
196
+ }
197
+
198
+ @objc func addAuthenticationListener() -> String {
181
199
 
182
- let userId = Courier.shared.userId
183
- resolve(userId)
200
+ let listener = Courier.shared.addAuthenticationListener { [weak self] userId in
201
+
202
+ self?.sendEvent(
203
+ withName: CourierReactNativeModule.AuthEvents.USER_CHANGED,
204
+ body: userId
205
+ )
206
+
207
+ }
208
+
209
+ // Create an id and add the listener to the dictionary
210
+ let id = UUID().uuidString
211
+ authListeners[id] = listener
212
+
213
+ return id
184
214
 
185
215
  }
186
-
187
- @objc(getFcmToken: withRejecter:)
188
- func getFcmToken(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
216
+
217
+ @objc(removeAuthenticationListener:)
218
+ func removeAuthenticationListener(listenerId: NSString) -> String {
219
+
220
+ let id = listenerId as String
221
+
222
+ // Remove the listener
223
+ let listener = authListeners[id]
224
+ listener?.remove()
189
225
 
190
- let token = Courier.shared.fcmToken
191
- resolve(token)
226
+ // Remove from dictionary
227
+ authListeners.removeValue(forKey: id)
228
+
229
+ return id
192
230
 
193
231
  }
194
232
 
@@ -206,41 +244,17 @@ class CourierReactNativeModule: RCTEventEmitter {
206
244
  )
207
245
 
208
246
  }
209
-
210
- @objc(getApnsToken: withRejecter:)
211
- func getApnsToken(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
212
-
213
- let token = Courier.shared.apnsToken
214
- resolve(token)
215
-
247
+
248
+ @objc func getFcmToken() -> String? {
249
+ return Courier.shared.fcmToken
216
250
  }
217
251
 
218
- @objc(sendPush: withUserId: withTitle: withBody: withProviders: withResolver: withRejecter:)
219
- func sendPush(authKey: NSString, userId: NSString, title: NSString, body: NSString, providers: NSArray, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
220
-
221
- guard let courierProviders = providers as? [String] else {
222
- reject("No provider supported", CourierReactNativeModule.COURIER_ERROR_TAG, nil)
223
- return
224
- }
225
-
226
- // Courier.shared.sendPush(
227
- // authKey: authKey as String,
228
- // userId: userId as String,
229
- // title: title as String,
230
- // message: body as String,
231
- // providers: courierProviders,
232
- // onSuccess: { requestId in
233
- // resolve(requestId)
234
- // },
235
- // onFailure: { error in
236
- // reject(String(describing: error), CourierReactNative.COURIER_ERROR_TAG, nil)
237
- // }
238
- // )
239
-
252
+ @objc func getApnsToken() -> String? {
253
+ return Courier.shared.apnsToken
240
254
  }
241
255
 
242
256
  @objc(iOSForegroundPresentationOptions:)
243
- func iOSForegroundPresentationOptions(params: NSDictionary) {
257
+ func iOSForegroundPresentationOptions(params: NSDictionary) -> String {
244
258
 
245
259
  let rawValue = params.toPresentationOptions().rawValue
246
260
  NotificationCenter.default.post(
@@ -249,13 +263,7 @@ class CourierReactNativeModule: RCTEventEmitter {
249
263
  userInfo: ["options": rawValue]
250
264
  )
251
265
 
252
- }
253
-
254
- @objc(setDebugMode: withResolver: withRejecter:)
255
- func setDebugMode(isDebugging: Bool, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
256
-
257
- Courier.shared.isDebugging = isDebugging
258
- resolve(Courier.shared.isDebugging)
266
+ return String(describing: rawValue)
259
267
 
260
268
  }
261
269
 
@@ -303,8 +311,7 @@ class CourierReactNativeModule: RCTEventEmitter {
303
311
 
304
312
  }
305
313
 
306
- @objc(addInboxListener:)
307
- func addInboxListener(listenerId: NSString?) -> String {
314
+ @objc func addInboxListener() -> String {
308
315
 
309
316
  let listener = Courier.shared.addInboxListener(
310
317
  onInitialLoad: { [weak self] in
@@ -348,15 +355,6 @@ class CourierReactNativeModule: RCTEventEmitter {
348
355
 
349
356
  }
350
357
 
351
- @objc(refreshInbox: withRejecter:)
352
- func refreshInbox(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
353
-
354
- Courier.shared.refreshInbox {
355
- resolve(nil)
356
- }
357
-
358
- }
359
-
360
358
  @objc(removeInboxListener:)
361
359
  func removeInboxListener(listenerId: NSString) -> String {
362
360
 
@@ -373,6 +371,15 @@ class CourierReactNativeModule: RCTEventEmitter {
373
371
 
374
372
  }
375
373
 
374
+ @objc(refreshInbox: withRejecter:)
375
+ func refreshInbox(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
376
+
377
+ Courier.shared.refreshInbox {
378
+ resolve(nil)
379
+ }
380
+
381
+ }
382
+
376
383
  @objc(fetchNextPageOfMessages: withRejecter:)
377
384
  func fetchNextPageOfMessages(resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) -> Void {
378
385
 
@@ -388,16 +395,17 @@ class CourierReactNativeModule: RCTEventEmitter {
388
395
  }
389
396
 
390
397
  @objc(setInboxPaginationLimit:)
391
- func setInboxPaginationLimit(limit: NSNumber) -> String {
392
- Courier.shared.inboxPaginationLimit = limit.intValue
393
- return String(describing: limit.intValue)
398
+ func setInboxPaginationLimit(limit: Double) -> String {
399
+ Courier.shared.inboxPaginationLimit = Int(limit)
400
+ return String(describing: Courier.shared.inboxPaginationLimit)
394
401
  }
395
402
 
396
403
  override func supportedEvents() -> [String]! {
397
404
  return [
398
- CourierReactNativeModule.COURIER_PUSH_NOTIFICATION_CLICKED_EVENT,
399
- CourierReactNativeModule.COURIER_PUSH_NOTIFICATION_DELIVERED_EVENT,
400
- CourierReactNativeModule.COURIER_PUSH_NOTIFICATION_DEBUG_LOG_EVENT,
405
+ CourierReactNativeModule.LogEvents.DEBUG_LOG_EVENT,
406
+ CourierReactNativeModule.AuthEvents.USER_CHANGED,
407
+ CourierReactNativeModule.PushEvents.CLICKED_EVENT,
408
+ CourierReactNativeModule.PushEvents.DELIVERED_EVENT,
401
409
  CourierReactNativeModule.InboxEvents.INITIAL_LOADING,
402
410
  CourierReactNativeModule.InboxEvents.ERROR,
403
411
  CourierReactNativeModule.InboxEvents.MESSAGES_CHANGED
@@ -2,9 +2,7 @@
2
2
 
3
3
  @interface RCT_EXTERN_MODULE(CourierReactNativeViewManager, RCTViewManager)
4
4
 
5
- RCT_EXPORT_VIEW_PROPERTY(lightTheme, NSDictionary)
6
-
7
- RCT_EXPORT_VIEW_PROPERTY(darkTheme, NSDictionary)
5
+ RCT_EXPORT_VIEW_PROPERTY(theme, NSDictionary)
8
6
 
9
7
  RCT_EXPORT_VIEW_PROPERTY(onClickInboxMessageAtIndex, RCTBubblingEventBlock)
10
8
 
@@ -15,13 +15,7 @@ class CourierReactNativeViewManager: RCTViewManager {
15
15
 
16
16
  class CourierReactNativeView : UIView {
17
17
 
18
- @objc var lightTheme: NSDictionary? = [:] {
19
- didSet {
20
- refreshInbox()
21
- }
22
- }
23
-
24
- @objc var darkTheme: NSDictionary? = [:] {
18
+ @objc var theme: NSDictionary? = [:] {
25
19
  didSet {
26
20
  refreshInbox()
27
21
  }
@@ -33,16 +27,6 @@ class CourierReactNativeView : UIView {
33
27
 
34
28
  @objc var onScrollInbox: RCTBubblingEventBlock? = nil
35
29
 
36
- override init(frame: CGRect) {
37
- super.init(frame: frame)
38
- refreshInbox()
39
- }
40
-
41
- required init?(coder: NSCoder) {
42
- super.init(coder: coder)
43
- refreshInbox()
44
- }
45
-
46
30
  private func refreshInbox() {
47
31
 
48
32
  // Disable animations
@@ -53,6 +37,9 @@ class CourierReactNativeView : UIView {
53
37
  $0.removeFromSuperview()
54
38
  }
55
39
 
40
+ let lightTheme = theme?["light"] as? NSDictionary
41
+ let darkTheme = theme?["dark"] as? NSDictionary
42
+
56
43
  // Create the view
57
44
  let courierInbox = CourierInbox(
58
45
  lightTheme: dictionaryToTheme(dictionary: lightTheme) ?? .defaultLight,
@@ -0,0 +1,258 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.useCourierPush = exports.useCourierInbox = exports.useCourierAuth = exports.CourierProvider = void 0;
7
+ var _react = _interopRequireWildcard(require("react"));
8
+ var _ = _interopRequireDefault(require(".."));
9
+ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
10
+ function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); }
11
+ function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
12
+ let authListener = undefined;
13
+ let pushListener = undefined;
14
+ let inboxListener = undefined;
15
+ const CourierContext = /*#__PURE__*/(0, _react.createContext)(undefined);
16
+ const CourierProvider = _ref => {
17
+ let {
18
+ children
19
+ } = _ref;
20
+ // Auth
21
+ const [auth_userId, auth_setUserId] = (0, _react.useState)(undefined);
22
+ const [auth_isLoading, auth_setIsLoading] = (0, _react.useState)(false);
23
+ const [auth_error, auth_setError] = (0, _react.useState)(undefined);
24
+
25
+ // Push
26
+ const [push_pushNotificationDelivered, inbox_setPushNotificationDelivered] = (0, _react.useState)(undefined);
27
+ const [push_pushNotificationClicked, inbox_setPushNotificationClicked] = (0, _react.useState)(undefined);
28
+ const [push_apnsToken, push_setApnsToken] = (0, _react.useState)(undefined);
29
+ const [push_fcmToken, push_setFcmToken] = (0, _react.useState)(undefined);
30
+ const [push_notificationPermission, push_setNotificationPermission] = (0, _react.useState)(undefined);
31
+
32
+ // Inbox
33
+ const [inbox_isLoading, inbox_setIsLoading] = (0, _react.useState)(false);
34
+ const [inbox_isRefreshing, inbox_setIsRefreshing] = (0, _react.useState)(false);
35
+ const [inbox_error, inbox_setError] = (0, _react.useState)(undefined);
36
+ const [inbox_messages, inbox_setMessages] = (0, _react.useState)([]);
37
+ const [inbox_unreadMessageCount, inbox_setUnreadMessageCount] = (0, _react.useState)(0);
38
+ const [inbox_totalMessageCount, inbox_setTotalMessageCount] = (0, _react.useState)(0);
39
+ const [inbox_canPaginate, inbox_setCanPaginate] = (0, _react.useState)(false);
40
+ (0, _react.useEffect)(() => {
41
+ // Get the initial values
42
+ const userId = _.default.shared.userId;
43
+ auth_setUserId(userId);
44
+
45
+ // Permissions
46
+ syncNotificationPermissions();
47
+
48
+ // Push tokens
49
+ syncTokens();
50
+ }, []);
51
+ (0, _react.useEffect)(() => {
52
+ return () => {
53
+ var _authListener, _pushListener, _inboxListener;
54
+ (_authListener = authListener) === null || _authListener === void 0 ? void 0 : _authListener.remove();
55
+ (_pushListener = pushListener) === null || _pushListener === void 0 ? void 0 : _pushListener.remove();
56
+ (_inboxListener = inboxListener) === null || _inboxListener === void 0 ? void 0 : _inboxListener.remove();
57
+ };
58
+ }, []);
59
+ const startAuth = () => {
60
+ if (!authListener) {
61
+ authListener = _.default.shared.addAuthenticationListener({
62
+ onUserChanged: userId => auth_setUserId(userId)
63
+ });
64
+ }
65
+ };
66
+ const startPush = () => {
67
+ if (!pushListener) {
68
+ pushListener = _.default.shared.addPushNotificationListener({
69
+ onPushNotificationDelivered: push => inbox_setPushNotificationDelivered(push),
70
+ onPushNotificationClicked: push => inbox_setPushNotificationClicked(push)
71
+ });
72
+ }
73
+ };
74
+ const startInbox = () => {
75
+ if (!inboxListener) {
76
+ inboxListener = _.default.shared.addInboxListener({
77
+ onInitialLoad: () => {
78
+ inbox_setIsLoading(true);
79
+ inbox_setError(undefined);
80
+ },
81
+ onError: error => {
82
+ inbox_setIsLoading(false);
83
+ inbox_setError(error);
84
+ },
85
+ onMessagesChanged: (messages, unreadMessageCount, totalMessageCount, canPaginate) => {
86
+ inbox_setIsLoading(false);
87
+ inbox_setError(undefined);
88
+ inbox_setMessages(messages);
89
+ inbox_setUnreadMessageCount(unreadMessageCount);
90
+ inbox_setTotalMessageCount(totalMessageCount);
91
+ inbox_setCanPaginate(canPaginate);
92
+ }
93
+ });
94
+ }
95
+ };
96
+ const signIn = async props => {
97
+ auth_setIsLoading(true);
98
+ auth_setError(undefined);
99
+ try {
100
+ await _.default.shared.signIn(props);
101
+ } catch (error) {
102
+ auth_setError(error);
103
+ }
104
+ auth_setIsLoading(false);
105
+ };
106
+ const signOut = async () => {
107
+ auth_setIsLoading(true);
108
+ auth_setError(undefined);
109
+ try {
110
+ await _.default.shared.signOut();
111
+ } catch (error) {
112
+ auth_setError(error);
113
+ }
114
+ auth_setIsLoading(false);
115
+ };
116
+ const syncNotificationPermissions = async () => {
117
+ const status = await _.default.shared.getNotificationPermissionStatus();
118
+ push_setNotificationPermission(status);
119
+ };
120
+ const syncTokens = () => {
121
+ // APNS
122
+ const apnsToken = _.default.shared.apnsToken;
123
+ push_setApnsToken(apnsToken);
124
+ const fcmToken = _.default.shared.fcmToken;
125
+ push_setFcmToken(fcmToken);
126
+ };
127
+ const requestNotificationPermission = async () => {
128
+ const status = await _.default.shared.requestNotificationPermission();
129
+ push_setNotificationPermission(status);
130
+ };
131
+ const iOSForegroundPresentationOptions = options => {
132
+ _.default.shared.iOSForegroundPresentationOptions({
133
+ options: options
134
+ });
135
+ };
136
+ const setPaginationLimit = limit => {
137
+ _.default.shared.setInboxPaginationLimit({
138
+ limit
139
+ });
140
+ };
141
+ const fetchNextPageOfMessages = () => {
142
+ return _.default.shared.fetchNextPageOfMessages();
143
+ };
144
+ const refresh = async () => {
145
+ inbox_setIsRefreshing(true);
146
+ await _.default.shared.refreshInbox();
147
+ inbox_setIsRefreshing(false);
148
+ };
149
+ const readAllMessages = () => {
150
+ // Skip if no user is found
151
+ if (!_.default.shared.userId) {
152
+ return Promise.resolve();
153
+ }
154
+
155
+ // Read the messages
156
+ return _.default.shared.readAllInboxMessages();
157
+ };
158
+ const readMessage = messageId => {
159
+ return _.default.shared.readMessage({
160
+ messageId
161
+ });
162
+ };
163
+ const unreadMessage = messageId => {
164
+ return _.default.shared.unreadMessage({
165
+ messageId
166
+ });
167
+ };
168
+ return /*#__PURE__*/_react.default.createElement(CourierContext.Provider, {
169
+ value: {
170
+ auth: {
171
+ start: startAuth,
172
+ isLoading: auth_isLoading,
173
+ error: auth_error,
174
+ userId: auth_userId,
175
+ signIn,
176
+ signOut
177
+ },
178
+ push: {
179
+ start: startPush,
180
+ delivered: push_pushNotificationDelivered,
181
+ clicked: push_pushNotificationClicked,
182
+ tokens: {
183
+ fcm: push_fcmToken,
184
+ apns: push_apnsToken
185
+ },
186
+ notificationPermissionStatus: push_notificationPermission,
187
+ requestNotificationPermission
188
+ },
189
+ inbox: {
190
+ start: startInbox,
191
+ isLoading: inbox_isLoading,
192
+ error: inbox_error,
193
+ messages: inbox_messages,
194
+ unreadMessageCount: inbox_unreadMessageCount,
195
+ totalMessageCount: inbox_totalMessageCount,
196
+ canPaginate: inbox_canPaginate,
197
+ iOSForegroundPresentationOptions,
198
+ setPaginationLimit,
199
+ fetchNextPageOfMessages,
200
+ refresh,
201
+ isRefreshing: inbox_isRefreshing,
202
+ readAllMessages,
203
+ readMessage,
204
+ unreadMessage
205
+ }
206
+ }
207
+ }, children);
208
+ };
209
+
210
+ // Auth Hook
211
+ exports.CourierProvider = CourierProvider;
212
+ const useCourierAuth = () => {
213
+ const context = (0, _react.useContext)(CourierContext);
214
+ if (!context) {
215
+ throw new Error('useCourierAuth must be used within an CourierProvider');
216
+ }
217
+ context.auth.start();
218
+ return context.auth;
219
+ };
220
+
221
+ // Push Hook
222
+ exports.useCourierAuth = useCourierAuth;
223
+ const useCourierPush = () => {
224
+ const context = (0, _react.useContext)(CourierContext);
225
+ if (!context) {
226
+ throw new Error('useCourierPush must be used within an CourierProvider');
227
+ }
228
+ context.push.start();
229
+ return context.push;
230
+ };
231
+ exports.useCourierPush = useCourierPush;
232
+ // Inbox Hook
233
+ const useCourierInbox = function () {
234
+ var _props$iOS, _context$inbox3;
235
+ let props = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
236
+ const context = (0, _react.useContext)(CourierContext);
237
+ if (!context) {
238
+ throw new Error('useCourierInbox must be used within an CourierProvider');
239
+ }
240
+
241
+ // Set the initial pagination limit if needed
242
+ const limit = props.paginationLimit;
243
+ if (limit) {
244
+ var _context$inbox;
245
+ (_context$inbox = context.inbox) === null || _context$inbox === void 0 ? void 0 : _context$inbox.setPaginationLimit(limit);
246
+ }
247
+
248
+ // Set the presentation options
249
+ const options = (_props$iOS = props.iOS) === null || _props$iOS === void 0 ? void 0 : _props$iOS.foregroundPresentationOptions;
250
+ if (options) {
251
+ var _context$inbox2;
252
+ (_context$inbox2 = context.inbox) === null || _context$inbox2 === void 0 ? void 0 : _context$inbox2.iOSForegroundPresentationOptions(options);
253
+ }
254
+ (_context$inbox3 = context.inbox) === null || _context$inbox3 === void 0 ? void 0 : _context$inbox3.start();
255
+ return context.inbox;
256
+ };
257
+ exports.useCourierInbox = useCourierInbox;
258
+ //# sourceMappingURL=CourierProvider.js.map