| ... | @@ -1,5 +1,6 @@ | ... | @@ -1,5 +1,6 @@ |
| 1 | import * as Notifications from "expo-notifications"; | 1 | import * as Notifications from "expo-notifications"; |
| 2 | import * as Effect from "effect/Effect"; | 2 | import * as Effect from "effect/Effect"; |
| | 3 | import * as Schema from "effect/Schema"; |
| 3 | import { Platform } from "react-native"; | 4 | import { Platform } from "react-native"; |
| 4 | | 5 | |
| 5 | export type NotificationPermissionResult = | 6 | export type NotificationPermissionResult = |
| ... | @@ -7,9 +8,31 @@ export type NotificationPermissionResult = | ... | @@ -7,9 +8,31 @@ export type NotificationPermissionResult = |
| 7 | | { readonly type: "granted" } | 8 | | { readonly type: "granted" } |
| 8 | | { readonly type: "denied"; readonly canAskAgain: boolean }; | 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 | export const requestAgentNotificationPermission: Effect.Effect< | 33 | export const requestAgentNotificationPermission: Effect.Effect< |
| 11 | NotificationPermissionResult, | 34 | NotificationPermissionResult, |
| 12 | unknown | 35 | NotificationPermissionReadError | NotificationPermissionRequestError |
| 13 | > = Effect.gen(function* () { | 36 | > = Effect.gen(function* () { |
| 14 | if (Platform.OS !== "ios") { | 37 | if (Platform.OS !== "ios") { |
| 15 | return { type: "unsupported" }; | 38 | return { type: "unsupported" }; |
| ... | @@ -17,7 +40,7 @@ export const requestAgentNotificationPermission: Effect.Effect< | ... | @@ -17,7 +40,7 @@ export const requestAgentNotificationPermission: Effect.Effect< |
| 17 | | 40 | |
| 18 | const existing = yield* Effect.tryPromise({ | 41 | const existing = yield* Effect.tryPromise({ |
| 19 | try: () => Notifications.getPermissionsAsync(), | 42 | try: () => Notifications.getPermissionsAsync(), |
| 20 | catch: (error) => error, | 43 | catch: (cause) => new NotificationPermissionReadError({ cause }), |
| 21 | }); | 44 | }); |
| 22 | if (existing.granted) { | 45 | if (existing.granted) { |
| 23 | return { type: "granted" }; | 46 | return { type: "granted" }; |
| ... | @@ -36,7 +59,7 @@ export const requestAgentNotificationPermission: Effect.Effect< | ... | @@ -36,7 +59,7 @@ export const requestAgentNotificationPermission: Effect.Effect< |
| 36 | allowSound: true, | 59 | allowSound: true, |
| 37 | }, | 60 | }, |
| 38 | }), | 61 | }), |
| 39 | catch: (error) => error, | 62 | catch: (cause) => new NotificationPermissionRequestError({ cause }), |
| 40 | }); | 63 | }); |
| 41 | return requested.granted | 64 | return requested.granted |
| 42 | ? { type: "granted" } | 65 | ? { type: "granted" } |