| ... | ... | @@ -1,5 +1,6 @@ |
| 1 | 1 | import * as Notifications from "expo-notifications"; |
| 2 | 2 | import * as Effect from "effect/Effect"; |
| 3 | import * as Schema from "effect/Schema"; |
| 3 | 4 | import { Platform } from "react-native"; |
| 4 | 5 | |
| 5 | 6 | export type NotificationPermissionResult = |
| ... | ... | @@ -7,9 +8,31 @@ export type NotificationPermissionResult = |
| 7 | 8 | | { readonly type: "granted" } |
| 8 | 9 | | { readonly type: "denied"; readonly canAskAgain: boolean }; |
| 9 | 10 | |
| 11 | export class NotificationPermissionReadError extends Schema.TaggedErrorClass<NotificationPermissionReadError>()( |
| 12 | "NotificationPermissionReadError", |
| 13 | { |
| 14 | cause: Schema.Defect(), |
| 15 | }, |
| 16 | ) { |
| 17 | override get message(): string { |
| 18 | return "Failed to read notification permissions on iOS."; |
| 19 | } |
| 20 | } |
| 21 | |
| 22 | export class NotificationPermissionRequestError extends Schema.TaggedErrorClass<NotificationPermissionRequestError>()( |
| 23 | "NotificationPermissionRequestError", |
| 24 | { |
| 25 | cause: Schema.Defect(), |
| 26 | }, |
| 27 | ) { |
| 28 | override get message(): string { |
| 29 | return "Failed to request notification permissions on iOS."; |
| 30 | } |
| 31 | } |
| 32 | |
| 10 | 33 | export const requestAgentNotificationPermission: Effect.Effect< |
| 11 | 34 | NotificationPermissionResult, |
| 12 | | unknown |
| 35 | NotificationPermissionReadError | NotificationPermissionRequestError |
| 13 | 36 | > = Effect.gen(function* () { |
| 14 | 37 | if (Platform.OS !== "ios") { |
| 15 | 38 | return { type: "unsupported" }; |
| ... | ... | @@ -17,7 +40,7 @@ export const requestAgentNotificationPermission: Effect.Effect< |
| 17 | 40 | |
| 18 | 41 | const existing = yield* Effect.tryPromise({ |
| 19 | 42 | try: () => Notifications.getPermissionsAsync(), |
| 20 | | catch: (error) => error, |
| 43 | catch: (cause) => new NotificationPermissionReadError({ cause }), |
| 21 | 44 | }); |
| 22 | 45 | if (existing.granted) { |
| 23 | 46 | return { type: "granted" }; |
| ... | ... | @@ -36,7 +59,7 @@ export const requestAgentNotificationPermission: Effect.Effect< |
| 36 | 59 | allowSound: true, |
| 37 | 60 | }, |
| 38 | 61 | }), |
| 39 | | catch: (error) => error, |
| 62 | catch: (cause) => new NotificationPermissionRequestError({ cause }), |
| 40 | 63 | }); |
| 41 | 64 | return requested.granted |
| 42 | 65 | ? { type: "granted" } |