import {useEffect} from 'react' import {Linking, View} from 'react-native' import * as Notification from 'expo-notifications' import {type AppBskyNotificationDefs} from '@atproto/api' import {msg, Trans} from '@lingui/macro' import {useLingui} from '@lingui/react' import {useQuery, useQueryClient} from '@tanstack/react-query' import {useAppState} from '#/lib/hooks/useAppState' import { type AllNavigatorParams, type NativeStackScreenProps, } from '#/lib/routes/types' import {isAndroid, isIOS, isWeb} from '#/platform/detection' import {useNotificationSettingsQuery} from '#/state/queries/notifications/settings' import {atoms as a} from '#/alf' import {Admonition} from '#/components/Admonition' import {At_Stroke2_Corner2_Rounded as AtIcon} from '#/components/icons/At' import {BellRinging_Stroke2_Corner0_Rounded as BellRingingIcon} from '#/components/icons/BellRinging' import {Bubble_Stroke2_Corner2_Rounded as BubbleIcon} from '#/components/icons/Bubble' import {Haptic_Stroke2_Corner2_Rounded as HapticIcon} from '#/components/icons/Haptic' import { Heart2_Stroke2_Corner0_Rounded as HeartIcon, LikeRepost_Stroke2_Corner2_Rounded as LikeRepostIcon, } from '#/components/icons/Heart2' import {PersonPlus_Stroke2_Corner2_Rounded as PersonPlusIcon} from '#/components/icons/Person' import {CloseQuote_Stroke2_Corner0_Rounded as CloseQuoteIcon} from '#/components/icons/Quote' import { Repost_Stroke2_Corner2_Rounded as RepostIcon, RepostRepost_Stroke2_Corner2_Rounded as RepostRepostIcon, } from '#/components/icons/Repost' import {Shapes_Stroke2_Corner0_Rounded as ShapesIcon} from '#/components/icons/Shapes' import * as Layout from '#/components/Layout' import * as SettingsList from '../components/SettingsList' import {ItemTextWithSubtitle} from './components/ItemTextWithSubtitle' const RQKEY = ['notification-permissions'] type Props = NativeStackScreenProps export function NotificationSettingsScreen({}: Props) { const {_} = useLingui() const queryClient = useQueryClient() const {data: settings, isError} = useNotificationSettingsQuery() const {data: permissions, refetch} = useQuery({ queryKey: RQKEY, queryFn: async () => { if (isWeb) return null return await Notification.getPermissionsAsync() }, }) const appState = useAppState() useEffect(() => { if (appState === 'active') { refetch() } }, [appState, refetch]) const onRequestPermissions = async () => { if (isWeb) return if (permissions?.canAskAgain) { const response = await Notification.requestPermissionsAsync() queryClient.setQueryData(RQKEY, response) } else { if (isAndroid) { try { await Linking.sendIntent( 'android.settings.APP_NOTIFICATION_SETTINGS', [ { key: 'android.provider.extra.APP_PACKAGE', value: 'xyz.blueskyweb.app', }, ], ) } catch { Linking.openSettings() } } else if (isIOS) { Linking.openSettings() } } } return ( Notifications {permissions && !permissions.granted && ( <> Enable push notifications )} {isError && ( Failed to load notification settings. )} Likes} subtitleText={} showSkeleton={!settings} /> New followers} subtitleText={} showSkeleton={!settings} /> Replies} subtitleText={} showSkeleton={!settings} /> Mentions} subtitleText={} showSkeleton={!settings} /> Quotes} subtitleText={} showSkeleton={!settings} /> Reposts} subtitleText={} showSkeleton={!settings} /> Activity from others} subtitleText={ } showSkeleton={!settings} /> Likes of your reposts} subtitleText={ } showSkeleton={!settings} /> Reposts of your reposts} subtitleText={ } showSkeleton={!settings} /> Everything else} // technically a bundle of several settings, but since they're set together // and are most likely in sync we'll just show the state of one of them subtitleText={ } showSkeleton={!settings} /> ) } function SettingPreview({ preference, }: { preference?: | AppBskyNotificationDefs.Preference | AppBskyNotificationDefs.FilterablePreference }) { const {_} = useLingui() if (!preference) { return null } else { if ('include' in preference) { if (preference.include === 'all') { if (preference.list && preference.push) { return _(msg`In-app, Push, Everyone`) } else if (preference.list) { return _(msg`In-app, Everyone`) } else if (preference.push) { return _(msg`Push, Everyone`) } } else if (preference.include === 'follows') { if (preference.list && preference.push) { return _(msg`In-app, Push, People you follow`) } else if (preference.list) { return _(msg`In-app, People you follow`) } else if (preference.push) { return _(msg`Push, People you follow`) } } } else { if (preference.list && preference.push) { return _(msg`In-app, Push`) } else if (preference.list) { return _(msg`In-app`) } else if (preference.push) { return _(msg`Push`) } } } return _(msg`Off`) }