All files / src/services notificationPreferenceService.ts

100% Statements 15/15
100% Branches 2/2
100% Functions 6/6
100% Lines 13/13

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 321x   1x       1x     3x     1x 2x 2x     1x 3x       3x     1x     1x 1x    
import * as SecureStore from 'expo-secure-store';
 
const PUSH_NOTIFICATIONS_ENABLED_KEY = 'sem.push-notifications-enabled';
 
type PushPreferenceListener = (enabled: boolean) => void;
 
const listeners = new Set<PushPreferenceListener>();
 
function emitPushPreference(enabled: boolean) {
  listeners.forEach((listener) => listener(enabled));
}
 
export async function getPushNotificationsEnabled(): Promise<boolean> {
  const storedValue = await SecureStore.getItemAsync(PUSH_NOTIFICATIONS_ENABLED_KEY);
  return storedValue !== 'false';
}
 
export async function setPushNotificationsEnabled(enabled: boolean): Promise<void> {
  await SecureStore.setItemAsync(
    PUSH_NOTIFICATIONS_ENABLED_KEY,
    enabled ? 'true' : 'false',
  );
  emitPushPreference(enabled);
}
 
export function subscribePushNotificationsEnabled(
  listener: PushPreferenceListener,
): () => void {
  listeners.add(listener);
  return () => listeners.delete(listener);
}