Files
carry-your-live/carry-your-live/app/_layout.tsx
T
2026-08-28 21:29:12 +02:00

99 lines
3.0 KiB
TypeScript

import { Stack, useRouter } from 'expo-router';
import { DatabaseProvider } from '@/hooks/useDatabase';
import { SettingsProvider } from '@/theme';
import { FriendsProvider } from '@/hooks/useFriends';
import { GestureHandlerRootView } from 'react-native-gesture-handler';
import { StatusBar } from 'expo-status-bar';
import { UpdateNotifier } from '@/components/UpdateNotifier';
import { Linking } from 'react-native';
import { useEffect } from 'react';
import { requestQuickAddFocus } from '@/utils/quickAddFocus';
import { registerForPushNotificationsAsync, setupNotificationChannels, addNotificationResponseReceivedListener, removeNotificationSubscription } from '@/services/pushNotifications';
import * as Notifications from 'expo-notifications';
Notifications.setNotificationHandler({
handleNotification: async () => ({
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
shouldShowBanner: true,
shouldShowList: true,
}),
});
function RootNavigator() {
const router = useRouter();
useEffect(() => {
setupNotificationChannels();
registerForPushNotificationsAsync().catch(() => {});
const responseListener = addNotificationResponseReceivedListener((response) => {
const data = response.notification.request.content.data as Record<string, any> | undefined;
if (!data) return;
switch (data.type) {
case 'task_reminder':
case 'task_assignment':
if (data.taskId) {
router.push({ pathname: '/task-detail', params: { id: data.taskId } });
}
break;
case 'friend_request':
if (data.friendshipId) {
router.push({ pathname: '/(tabs)/settings' });
}
break;
}
});
const handleUrl = (url: string | null) => {
if (!url || !url.includes('://quick-add')) return;
router.replace('/(tabs)');
requestQuickAddFocus();
};
Linking.getInitialURL().then(handleUrl).catch(() => {});
const subscription = Linking.addEventListener('url', ({ url }) => handleUrl(url));
const handleKeyDown = (event: KeyboardEvent) => {
if (event.key === 'Escape') {
router.back();
}
};
window.addEventListener('keydown', handleKeyDown);
return () => {
subscription.remove();
removeNotificationSubscription(responseListener);
window.removeEventListener('keydown', handleKeyDown);
};
}, [router]);
return (
<GestureHandlerRootView style={{ flex: 1 }}>
<StatusBar style="dark" />
<Stack screenOptions={{ headerShown: false }}>
<Stack.Screen name="(tabs)" />
<Stack.Screen name="add-task" />
<Stack.Screen name="task-detail" />
<Stack.Screen name="day-view" />
</Stack>
</GestureHandlerRootView>
);
}
export default function RootLayout() {
return (
<SettingsProvider>
<DatabaseProvider>
<FriendsProvider>
<UpdateNotifier />
<RootNavigator />
</FriendsProvider>
</DatabaseProvider>
</SettingsProvider>
);
}