@trycourier/courier-react-native 2.1.0 → 2.2.0

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 (36) hide show
  1. package/android/build.gradle +2 -2
  2. package/android/src/main/java/com/courierreactnative/CourierReactNativeActivity.kt +0 -3
  3. package/android/src/main/java/com/courierreactnative/CourierReactNativeModule.kt +51 -22
  4. package/android/src/main/java/com/courierreactnative/CourierReactNativeViewManager.kt +84 -18
  5. package/courier-react-native.podspec +1 -1
  6. package/ios/CourierReactNativeModule.m +5 -9
  7. package/ios/CourierReactNativeModule.swift +8 -11
  8. package/ios/CourierReactNativeViewManager.swift +122 -41
  9. package/lib/commonjs/index.js +54 -34
  10. package/lib/commonjs/index.js.map +1 -1
  11. package/lib/commonjs/models/CourierPushProvider.js +16 -0
  12. package/lib/commonjs/models/CourierPushProvider.js.map +1 -0
  13. package/lib/module/index.js +13 -17
  14. package/lib/module/index.js.map +1 -1
  15. package/lib/module/models/CourierPushProvider.js +9 -0
  16. package/lib/module/models/CourierPushProvider.js.map +1 -0
  17. package/lib/typescript/index.d.ts +16 -8
  18. package/lib/typescript/index.d.ts.map +1 -1
  19. package/lib/typescript/models/CourierInboxTheme.d.ts +24 -8
  20. package/lib/typescript/models/CourierInboxTheme.d.ts.map +1 -1
  21. package/lib/typescript/models/CourierPushProvider.d.ts +8 -0
  22. package/lib/typescript/models/CourierPushProvider.d.ts.map +1 -0
  23. package/lib/typescript/views/CourierInboxView.d.ts +1 -1
  24. package/lib/typescript/views/CourierInboxView.d.ts.map +1 -1
  25. package/package.json +1 -1
  26. package/src/index.tsx +16 -20
  27. package/src/models/CourierInboxTheme.tsx +28 -8
  28. package/src/models/CourierPushProvider.tsx +7 -0
  29. package/src/views/CourierInboxView.tsx +1 -1
  30. package/lib/commonjs/hooks/CourierProvider.js +0 -254
  31. package/lib/commonjs/hooks/CourierProvider.js.map +0 -1
  32. package/lib/module/hooks/CourierProvider.js +0 -241
  33. package/lib/module/hooks/CourierProvider.js.map +0 -1
  34. package/lib/typescript/hooks/CourierProvider.d.ts +0 -57
  35. package/lib/typescript/hooks/CourierProvider.d.ts.map +0 -1
  36. package/src/hooks/CourierProvider.tsx +0 -356
@@ -1,356 +0,0 @@
1
- import React, { createContext, ReactNode, useContext, useEffect, useState } from 'react';
2
- import { InboxMessage } from '../models/InboxMessage';
3
- import Courier, { CourierPushListener, CourierInboxListener, CourierAuthenticationListener, iOSForegroundPresentationOptions } from '..';
4
-
5
- let authListener: CourierAuthenticationListener | undefined = undefined
6
- let pushListener: CourierPushListener | undefined = undefined
7
- let inboxListener: CourierInboxListener | undefined = undefined
8
-
9
- interface CourierAuthContext {
10
- start: () => void;
11
- isLoading: boolean;
12
- error?: string;
13
- userId?: string;
14
- signIn: (props: { accessToken: string; clientKey?: string; userId: string }) => Promise<void>;
15
- signOut: () => Promise<void>;
16
- }
17
-
18
- interface CourierPushContext {
19
- start: () => void;
20
- delivered: any;
21
- clicked: any;
22
- tokens: {
23
- apns?: string;
24
- fcm?: string;
25
- };
26
- iOSForegroundPresentationOptions: (options: iOSForegroundPresentationOptions[]) => void;
27
- notificationPermissionStatus?: string;
28
- requestNotificationPermission: () => Promise<void>;
29
- }
30
-
31
- interface CourierInboxContext {
32
- start: () => void;
33
- isLoading: boolean;
34
- error?: string;
35
- messages: InboxMessage[];
36
- unreadMessageCount: number;
37
- totalMessageCount: number;
38
- canPaginate: boolean;
39
- setPaginationLimit: (limit: number) => void;
40
- fetchNextPageOfMessages: () => Promise<InboxMessage[]>;
41
- refresh: () => Promise<void>;
42
- isRefreshing: boolean;
43
- readAllMessages: () => Promise<void>;
44
- readMessage: (messageId: string) => string;
45
- unreadMessage: (messageId: string) => string;
46
- }
47
-
48
- interface CourierContext {
49
- auth: CourierAuthContext;
50
- push: CourierPushContext;
51
- inbox: CourierInboxContext;
52
- }
53
-
54
- const CourierContext = createContext<CourierContext | undefined>(undefined);
55
-
56
- export const CourierProvider: React.FC<{ children: ReactNode }> = ({ children }) => {
57
-
58
- // Auth
59
- const [auth_userId, auth_setUserId] = useState<string | undefined>(undefined);
60
- const [auth_isLoading, auth_setIsLoading] = useState<boolean>(false);
61
- const [auth_error, auth_setError] = useState<string | undefined>(undefined);
62
-
63
- // Push
64
- const [push_pushNotificationDelivered, inbox_setPushNotificationDelivered] = useState<any>(undefined);
65
- const [push_pushNotificationClicked, inbox_setPushNotificationClicked] = useState<any>(undefined);
66
- const [push_apnsToken, push_setApnsToken] = useState<string | undefined>(undefined);
67
- const [push_fcmToken, push_setFcmToken] = useState<string | undefined>(undefined);
68
- const [push_notificationPermission, push_setNotificationPermission] = useState<string | undefined>(undefined);
69
-
70
- // Inbox
71
- const [inbox_isLoading, inbox_setIsLoading] = useState<boolean>(false);
72
- const [inbox_isRefreshing, inbox_setIsRefreshing] = useState<boolean>(false);
73
- const [inbox_error, inbox_setError] = useState<string | undefined>(undefined);
74
- const [inbox_messages, inbox_setMessages] = useState<InboxMessage[]>([]);
75
- const [inbox_unreadMessageCount, inbox_setUnreadMessageCount] = useState<number>(0);
76
- const [inbox_totalMessageCount, inbox_setTotalMessageCount] = useState<number>(0);
77
- const [inbox_canPaginate, inbox_setCanPaginate] = useState<boolean>(false);
78
-
79
- useEffect(() => {
80
-
81
- // Get the initial values
82
- const userId = Courier.shared.userId;
83
- auth_setUserId(userId);
84
-
85
- }, []);
86
-
87
- useEffect(() => {
88
-
89
- return () => {
90
- authListener?.remove();
91
- pushListener?.remove();
92
- inboxListener?.remove();
93
- }
94
-
95
- }, []);
96
-
97
- const startAuth = () => {
98
-
99
- if (!authListener) {
100
-
101
- authListener = Courier.shared.addAuthenticationListener({
102
- onUserChanged: (userId) => auth_setUserId(userId)
103
- });
104
-
105
- }
106
-
107
- }
108
-
109
- const startPush = () => {
110
-
111
- // Permissions
112
- syncNotificationPermissions();
113
-
114
- // Push tokens
115
- syncTokens();
116
-
117
- if (!pushListener) {
118
-
119
- pushListener = Courier.shared.addPushNotificationListener({
120
- onPushNotificationDelivered: (push) => inbox_setPushNotificationDelivered(push),
121
- onPushNotificationClicked: (push) => inbox_setPushNotificationClicked(push)
122
- });
123
-
124
- }
125
-
126
- }
127
-
128
- const startInbox = () => {
129
-
130
- if (!inboxListener) {
131
-
132
- inboxListener = Courier.shared.addInboxListener({
133
- onInitialLoad: () => {
134
- inbox_setIsLoading(true);
135
- inbox_setError(undefined);
136
- },
137
- onError: (error) => {
138
- inbox_setIsLoading(false);
139
- inbox_setError(error);
140
- },
141
- onMessagesChanged: (messages, unreadMessageCount, totalMessageCount, canPaginate) => {
142
- inbox_setIsLoading(false);
143
- inbox_setError(undefined);
144
- inbox_setMessages(messages);
145
- inbox_setUnreadMessageCount(unreadMessageCount);
146
- inbox_setTotalMessageCount(totalMessageCount);
147
- inbox_setCanPaginate(canPaginate);
148
- }
149
- });
150
-
151
- }
152
-
153
- }
154
-
155
- const signIn = async (props: { accessToken: string, clientKey?: string, userId: string }): Promise<void> => {
156
-
157
- auth_setIsLoading(true);
158
- auth_setError(undefined);
159
-
160
- try {
161
- await Courier.shared.signIn(props);
162
- } catch (error) {
163
- auth_setError(error as string);
164
- }
165
-
166
- auth_setIsLoading(false);
167
-
168
- };
169
-
170
- const signOut = async (): Promise<void> => {
171
-
172
- auth_setIsLoading(true);
173
- auth_setError(undefined);
174
-
175
- try {
176
- await Courier.shared.signOut();
177
- } catch (error) {
178
- auth_setError(error as string);
179
- }
180
-
181
- auth_setIsLoading(false);
182
-
183
- };
184
-
185
- const syncNotificationPermissions = async () => {
186
-
187
- const status = await Courier.shared.getNotificationPermissionStatus();
188
- push_setNotificationPermission(status);
189
-
190
- }
191
-
192
- const syncTokens = async () => {
193
-
194
- // APNS
195
- const apnsToken = Courier.shared.apnsToken;
196
- push_setApnsToken(apnsToken);
197
-
198
- const fcmToken = await Courier.shared.fcmToken;
199
- push_setFcmToken(fcmToken);
200
-
201
- }
202
-
203
- const requestNotificationPermission = async () => {
204
- const status = await Courier.shared.requestNotificationPermission();
205
- push_setNotificationPermission(status);
206
- };
207
-
208
- const iOSForegroundPresentationOptions = (options: iOSForegroundPresentationOptions[]) => {
209
- Courier.shared.iOSForegroundPresentationOptions({
210
- options: options,
211
- });
212
- }
213
-
214
- const setPaginationLimit = (limit: number) => {
215
- Courier.shared.setInboxPaginationLimit({ limit });
216
- };
217
-
218
- const fetchNextPageOfMessages = (): Promise<InboxMessage[]> => {
219
- return Courier.shared.fetchNextPageOfMessages();
220
- };
221
-
222
- const refresh = async () => {
223
- inbox_setIsRefreshing(true);
224
- await Courier.shared.refreshInbox();
225
- inbox_setIsRefreshing(false);
226
- };
227
-
228
- const readAllMessages = () => {
229
-
230
- // Skip if no user is found
231
- if (!Courier.shared.userId) {
232
- return Promise.resolve();
233
- }
234
-
235
- // Read the messages
236
- return Courier.shared.readAllInboxMessages();
237
-
238
- };
239
-
240
- const readMessage = (messageId: string) => {
241
- return Courier.shared.readMessage({ messageId });
242
- };
243
-
244
- const unreadMessage = (messageId: string) => {
245
- return Courier.shared.unreadMessage({ messageId });
246
- };
247
-
248
- return (
249
- <CourierContext.Provider value={{
250
- auth: {
251
- start: startAuth,
252
- isLoading: auth_isLoading,
253
- error: auth_error,
254
- userId: auth_userId,
255
- signIn,
256
- signOut
257
- },
258
- push: {
259
- start: startPush,
260
- delivered: push_pushNotificationDelivered,
261
- clicked: push_pushNotificationClicked,
262
- tokens: {
263
- fcm: push_fcmToken,
264
- apns: push_apnsToken,
265
- },
266
- iOSForegroundPresentationOptions,
267
- notificationPermissionStatus: push_notificationPermission,
268
- requestNotificationPermission,
269
- },
270
- inbox: {
271
- start: startInbox,
272
- isLoading: inbox_isLoading,
273
- error: inbox_error,
274
- messages: inbox_messages,
275
- unreadMessageCount: inbox_unreadMessageCount,
276
- totalMessageCount: inbox_totalMessageCount,
277
- canPaginate: inbox_canPaginate,
278
- setPaginationLimit,
279
- fetchNextPageOfMessages,
280
- refresh,
281
- isRefreshing: inbox_isRefreshing,
282
- readAllMessages,
283
- readMessage,
284
- unreadMessage,
285
- }
286
- }}>
287
- {children}
288
- </CourierContext.Provider>
289
- );
290
-
291
- };
292
-
293
- // Auth Hook
294
- export const useCourierAuth = (): CourierAuthContext => {
295
-
296
- const context = useContext(CourierContext);
297
-
298
- if (!context) {
299
- throw new Error('useCourierAuth must be used within an CourierProvider');
300
- }
301
-
302
- context.auth.start();
303
-
304
- return context.auth;
305
-
306
- };
307
-
308
- interface UseCourierPushProps {
309
- iOSForegroundPresentationOptions?: iOSForegroundPresentationOptions[]
310
- }
311
-
312
- // Push Hook
313
- export const useCourierPush = (props: UseCourierPushProps = {}): CourierPushContext => {
314
-
315
- const context = useContext(CourierContext);
316
-
317
- if (!context) {
318
- throw new Error('useCourierPush must be used within an CourierProvider');
319
- }
320
-
321
- // Set the presentation options
322
- const options = props.iOSForegroundPresentationOptions;
323
- if (options) {
324
- context.push.iOSForegroundPresentationOptions(options);
325
- }
326
-
327
- context.push.start();
328
-
329
- return context.push;
330
-
331
- };
332
-
333
- interface UseCourierInboxProps {
334
- paginationLimit?: number;
335
- }
336
-
337
- // Inbox Hook
338
- export const useCourierInbox = (props: UseCourierInboxProps = {}): CourierInboxContext => {
339
-
340
- const context = useContext(CourierContext);
341
-
342
- if (!context) {
343
- throw new Error('useCourierInbox must be used within an CourierProvider');
344
- }
345
-
346
- // Set the initial pagination limit if needed
347
- const limit = props.paginationLimit;
348
- if (limit) {
349
- context.inbox.setPaginationLimit(limit);
350
- }
351
-
352
- context.inbox.start();
353
-
354
- return context.inbox;
355
-
356
- };