/** * Centralized permission broker — screens must request permissions through * these helpers, never react-native-permissions directly. */ import { check, request, RESULTS, PermissionStatus, openSettings, PERMISSIONS, Permission, checkNotifications, requestNotifications, } from 'react-native-permissions'; import { Platform } from 'react-native'; import { logger } from '../core/observability/logger'; // Normalize permission for platform const normalizePermission = (perm: string): Permission => { return Platform.select({ ios: (PERMISSIONS.IOS as Record)[perm] || perm, android: (PERMISSIONS.ANDROID as Record)[perm] || perm, default: perm, }) as Permission; }; // Generic permission requester export const requestSinglePermission = async ( permission: string ): Promise => { const normalized = normalizePermission(permission); const status: PermissionStatus = await check(normalized); if (status === RESULTS.GRANTED) { return true; } if (status === RESULTS.BLOCKED) { logger.info('permissions', `${permission} is blocked — opening settings`); openSettings(); return false; } if (status === RESULTS.DENIED || status === RESULTS.LIMITED) { const result: PermissionStatus = await request(normalized); return result === RESULTS.GRANTED; } return false; }; export const requestNotificationPermission = async (): Promise => { if (Platform.OS !== 'android') { return true; } const { status } = await checkNotifications(); if (status === RESULTS.GRANTED) { return true; } if (status === RESULTS.BLOCKED) { logger.info('permissions', 'notifications blocked — opening settings'); openSettings(); return false; } if (status === RESULTS.DENIED) { const { status: result } = await requestNotifications(['alert', 'sound']); return result === RESULTS.GRANTED; } return false; }; export const isAnyPermissionGranted = async (permissions: Permission[]): Promise => { for (const permission of permissions) { const result = await check(permission); if (result === RESULTS.GRANTED) { return true; // At least one permission is granted } } return false; // No permissions granted }; export async function checkPermission(permission: Permission) { try { const result = await check(permission); return result === RESULTS.GRANTED; } catch (error) { logger.warn('permissions', 'permission check failed', { error: String(error) }); return false; } }