| 1 | import { Tabs, Redirect, router } from 'expo-router';
|
| 2 | import React, { useEffect } from 'react';
|
| 3 | import { View, ActivityIndicator } from 'react-native';
|
| 4 |
|
| 5 | import { HapticTab } from '@/components/misc/haptic-tab';
|
| 6 | import { IconSymbol } from '@/components/ui/icon-symbol';
|
| 7 | import { Colors } from '@/constants/theme';
|
| 8 | import { useColorScheme } from '@/hooks/use-color-scheme';
|
| 9 | import { useAuth } from '@/context/AuthContext';
|
| 10 | import * as Notifications from "expo-notifications";
|
| 11 |
|
| 12 | export default function TabLayout() {
|
| 13 | const colorScheme = useColorScheme();
|
| 14 | const { isAuthenticated, isLoading } = useAuth();
|
| 15 |
|
| 16 | useEffect(() => {
|
| 17 | if (!isLoading && !isAuthenticated) {
|
| 18 | router.replace('/auth/login');
|
| 19 | }
|
| 20 | }, [isAuthenticated, isLoading]);
|
| 21 |
|
| 22 | // TODO: Store the notification in a hook ctx, then capture if was
|
| 23 | useEffect(() => {
|
| 24 | const subscription = Notifications.addNotificationResponseReceivedListener(response => {
|
| 25 | const screen = response.notification.request.content.data?.screen;
|
| 26 |
|
| 27 | if (screen === 'notifications') {
|
| 28 | router.push('/notifications');
|
| 29 | }
|
| 30 | });
|
| 31 |
|
| 32 | // Handle notification tap when app was already open
|
| 33 | Notifications.setNotificationHandler({
|
| 34 | handleNotification: async () => ({
|
| 35 | shouldShowAlert: true,
|
| 36 | shouldPlaySound: false,
|
| 37 | shouldSetBadge: false,
|
| 38 | }),
|
| 39 | });
|
| 40 |
|
| 41 | return () => subscription.remove();
|
| 42 | }, []);
|
| 43 |
|
| 44 | if (isLoading) {
|
| 45 | return (
|
| 46 | <View style={{ flex: 1, justifyContent: 'center', alignItems: 'center' }}>
|
| 47 | <ActivityIndicator size="large" />
|
| 48 | </View>
|
| 49 | );
|
| 50 | }
|
| 51 |
|
| 52 | if (!isAuthenticated) {
|
| 53 | return <Redirect href="/auth/login" />;
|
| 54 | }
|
| 55 |
|
| 56 | return (
|
| 57 | <Tabs
|
| 58 | screenOptions={{
|
| 59 | tabBarInactiveTintColor: Colors['light'].tabIconDefault,
|
| 60 | tabBarActiveTintColor: Colors['light'].tabIconSelected,
|
| 61 | headerShown: false,
|
| 62 | tabBarButton: HapticTab,
|
| 63 | }}>
|
| 64 |
|
| 65 | <Tabs.Screen
|
| 66 | name="history"
|
| 67 | options={{
|
| 68 | title: 'Histórico',
|
| 69 | tabBarIcon: ({ color }) => <IconSymbol size={28} name="history" color={color} />,
|
| 70 | }}
|
| 71 | />
|
| 72 |
|
| 73 | <Tabs.Screen
|
| 74 | name="insights"
|
| 75 | options={{
|
| 76 | title: 'Jornada',
|
| 77 | tabBarIcon: ({ color }) => <IconSymbol size={28} name="book" color={color} />,
|
| 78 | }}
|
| 79 | />
|
| 80 |
|
| 81 | <Tabs.Screen
|
| 82 | name="new"
|
| 83 | options={{
|
| 84 | title: 'Novo',
|
| 85 | tabBarIcon: ({ color }) => <IconSymbol size={28} name="add-circle" color={color} />,
|
| 86 | }}
|
| 87 | />
|
| 88 |
|
| 89 | <Tabs.Screen
|
| 90 | name="actions"
|
| 91 | options={{
|
| 92 | title: 'Ações',
|
| 93 | tabBarIcon: ({ color }) => <IconSymbol size={28} name="bolt" color={color} />,
|
| 94 | }}
|
| 95 | />
|
| 96 |
|
| 97 | <Tabs.Screen
|
| 98 | name="settings"
|
| 99 | options={{
|
| 100 | title: 'Ajustes',
|
| 101 | tabBarIcon: ({ color }) => <IconSymbol size={28} name="settings.fill" color={color} />,
|
| 102 | }}
|
| 103 | />
|
| 104 | </Tabs>
|
| 105 | );
|
| 106 | }
|