summaryrefslogtreecommitdiff
path: root/app/(tabs)/_layout.tsx
blob: 6bd652701bb3fccc0f3109f62a85e90e8bffd41d (plain)
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
import { Ionicons } from "@expo/vector-icons";
import * as Notifications from "expo-notifications";
import { Redirect, Tabs, router } from "expo-router";
import { useEffect, useRef } from "react";
import { View } from "react-native";
import { useAuth } from "../../lib/auth";
import { DeviceProvider } from "../../lib/device";
import { t } from "../../lib/locales";
import {
  addNotificationReceivedListener,
  addNotificationResponseListener,
  getLastNotificationResponse,
  initializeNotifications,
} from "../../lib/notifications";
import { colors } from "../../lib/theme";
import { LoadingScreen } from "../../lib/ui";

export default function TabsLayout() {
  const { isLoading, isAuthenticated } = useAuth();
  const notificationListener = useRef<Notifications.EventSubscription | null>(
    null
  );
  const responseListener = useRef<Notifications.EventSubscription | null>(null);

  useEffect(() => {
    if (!isAuthenticated) return;

    // Initialize notifications when authenticated
    initializeNotifications().then(({ permissionStatus, tokenRegistered }) => {
      console.log(
        `Notifications initialized: permission=${permissionStatus}, tokenRegistered=${tokenRegistered}`
      );
    });

    // Set up notification listeners
    notificationListener.current = addNotificationReceivedListener(
      (notification) => {
        console.log("Notification received:", notification);
      }
    );

    responseListener.current = addNotificationResponseListener((response) => {
      console.log("Notification tapped:", response);
      const data = response.notification.request.content.data;

      // Navigate based on notification type
      if (data?.type === "dangerous_content") {
        router.push("/(tabs)/alerts");
      } else if (data?.type === "new_contact") {
        // Navigate to contact detail screen with contact info
        router.push({
          pathname: "/(tabs)/contact-detail",
          params: {
            contactName: String(data.contactName || "Unknown"),
            contactIdentifier: String(data.contactIdentifier || "Not provided"),
            contactType: String(data.contactType || "unknown"),
            deviceName: String(data.deviceName || "Unknown Device"),
          },
        });
      }
    });

    // Check if app was opened from a notification (cold start)
    getLastNotificationResponse().then((response) => {
      if (response) {
        const data = response.notification.request.content.data;
        if (data?.type === "dangerous_content") {
          router.push("/(tabs)/alerts");
        } else if (data?.type === "new_contact") {
          router.push({
            pathname: "/(tabs)/contact-detail",
            params: {
              contactName: String(data.contactName || "Unknown"),
              contactIdentifier: String(
                data.contactIdentifier || "Not provided"
              ),
              contactType: String(data.contactType || "unknown"),
              deviceName: String(data.deviceName || "Unknown Device"),
            },
          });
        }
      }
    });

    return () => {
      if (notificationListener.current) {
        notificationListener.current.remove();
      }
      if (responseListener.current) {
        responseListener.current.remove();
      }
    };
  }, [isAuthenticated]);

  if (isLoading) {
    return <LoadingScreen />;
  }

  if (!isAuthenticated) {
    return <Redirect href="/(auth)/welcome" />;
  }

  return (
    <DeviceProvider>
      <View
        style={{
          flex: 1,
          backgroundColor: colors.background,
        }}
      >
        <Tabs
          screenOptions={{
            tabBarShowLabel: true,
            tabBarActiveTintColor: colors.primary,
            tabBarInactiveTintColor: colors.onSurfaceVariant,
            tabBarLabelStyle: {
              fontSize: 11,
              fontWeight: "600",
            },
            tabBarStyle: {
              backgroundColor: colors.surfaceContainer,
              borderTopColor: colors.outline,
            },
            headerStyle: {
              backgroundColor: colors.background,
              borderWidth: 0,
              marginBottom: 10,
            },
            headerTintColor: colors.onBackground,
            headerTitleStyle: {
              color: colors.onBackground,
              fontSize: 30,
              fontWeight: "700",
            },
          }}
        >
          <Tabs.Screen
            name="index"
            options={{
              title: t("home"),
              tabBarIcon: ({ color, size }) => (
                <Ionicons name="home" size={size ?? 24} color={color} />
              ),
            }}
          />
          <Tabs.Screen
            name="alerts"
            options={{
              title: t("alerts"),
              tabBarIcon: ({ color, size }) => (
                <Ionicons name="alert-circle" size={size ?? 24} color={color} />
              ),
            }}
          />
          <Tabs.Screen
            name="controls"
            options={{
              title: t("controls"),
              tabBarIcon: ({ color, size }) => (
                <Ionicons
                  name="shield-checkmark"
                  size={size ?? 24}
                  color={color}
                />
              ),
            }}
          />
          <Tabs.Screen
            name="contact-detail"
            options={{
              title: "Contact Details",
              href: null,
            }}
          />
          <Tabs.Screen
            name="settings"
            options={{
              title: t("settings"),
              tabBarIcon: ({ color, size }) => (
                <Ionicons name="settings" size={size ?? 24} color={color} />
              ),
            }}
          />
        </Tabs>
      </View>
    </DeviceProvider>
  );
}