Something went wrong. Try again.
Personal iOS fork of Witchsky (itself a fork of Bluesky social-app).
Something went wrong. Try again.
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970import {createContext, useContext, useEffect, useMemo, useState} from 'react'
import {type BackgroundNotificationHandlerPreferences} from './ExpoBackgroundNotificationHandler.types'import {BackgroundNotificationHandler} from './ExpoBackgroundNotificationHandlerModule'
interface BackgroundNotificationPreferencesContext { preferences: BackgroundNotificationHandlerPreferences setPref: <Key extends keyof BackgroundNotificationHandlerPreferences>( key: Key, value: BackgroundNotificationHandlerPreferences[Key], ) => void}
const Context = createContext<BackgroundNotificationPreferencesContext>( {} as BackgroundNotificationPreferencesContext,)export const useBackgroundNotificationPreferences = () => useContext(Context)
export function BackgroundNotificationPreferencesProvider({ children,}: { children: React.ReactNode}) { const [preferences, setPreferences] = useState<BackgroundNotificationHandlerPreferences>({ playSoundChat: true, })
useEffect(() => { ;void (async () => { const prefs = await BackgroundNotificationHandler.getAllPrefsAsync() setPreferences(prefs) })() }, [])
const value = useMemo( () => ({ preferences, setPref: async < Key extends keyof BackgroundNotificationHandlerPreferences, >( k: Key, v: BackgroundNotificationHandlerPreferences[Key], ) => { switch (typeof v) { case 'boolean': { await BackgroundNotificationHandler.setBoolAsync(k, v) break } case 'string': { await BackgroundNotificationHandler.setStringAsync(k, v) break } default: { throw new Error(`Invalid type for value: ${typeof v}`) } }
setPreferences(prev => ({ ...prev, [k]: v, })) }, }), [preferences], )
return <Context.Provider value={value}>{children}</Context.Provider>}