import { StatusBar } from 'expo-status-bar';
import React, { useState, useRef, useEffect } from 'react';
import { NavigationContainer } from '@react-navigation/native';
import { createNativeStackNavigator } from '@react-navigation/native-stack';
import { createBottomTabNavigator } from '@react-navigation/bottom-tabs';
import { Provider as PaperProvider, DefaultTheme, Appbar, Button, Text } from 'react-native-paper';
import MaterialIcons from 'react-native-vector-icons/MaterialIcons';
import Login from "./Views/Login.js"
import Feed from "./Views/Feed.js"
import Profile from "./Views/Profile.js"
import Search from './Views/Search.js';
import Groups from './Views/Groups.js';
import Courses from './Views/Courses.js';
import NotificationsView from './Views/NotificationsView.js';
import SinglePost from './Views/SinglePost.js'
import * as Device from 'expo-device';
import * as Notifications from 'expo-notifications';
import API from './API.js';
import i18n from "./i18nMessages.js";
import NewPostView from './Views/NewPost.js';
import { TouchableOpacity, View, Image, KeyboardAvoidingView } from 'react-native';
import MenuView from './Views/Menu.js';
import ProfileSettings from './Views/ProfileSettings.js';
import InviteView from './Views/Invite.js';
import MediaView from './components/MediaView.js';
import { useSnapshot } from 'valtio';
import GlobalState from './contexts/GlobalState.js';
import NewGroup from './Views/NewGroup.js';
import * as Linking2 from 'expo-linking';
import { Linking } from 'expo';
const handleDeepLink = (url) => {
// Handle the deep linking URL here
alert('Deep link:', url);
}
const checkDeepLinking = async () => {
// Check if the app was opened by a deep linking URL
const initialUrl = await Linking.getInitialURL();
if (initialUrl) {
handleDeepLink(initialUrl);
}
// Add an event listener to handle deep linking URLs
Linking.addEventListener('url', (event) => {
handleDeepLink(event.url);
});
}
const Tab = createBottomTabNavigator();
const Stack = createNativeStackNavigator();
const theme = {
...DefaultTheme,
roundness: 2,
colors: {
...DefaultTheme.colors,
primary: '#000000',
accent: '#0d6efd',
background: "#edf2f7",
},
};
Notifications.setNotificationHandler({
handleNotification: async () => {
//console.log("notif background");
return {
shouldShowAlert: true,
shouldPlaySound: true,
shouldSetBadge: true,
};
},
});
async function registerForPushNotificationsAsync() {
if (Platform.OS === 'android') {
Notifications.setNotificationChannelAsync('default', {
name: 'default',
importance: Notifications.AndroidImportance.MAX,
vibrationPattern: [0, 250, 250, 250],
lightColor: '#FF231F7C',
});
}
let token;
if (Device.isDevice) {
const { status: existingStatus } = await Notifications.getPermissionsAsync();
let finalStatus = existingStatus;
if (existingStatus !== 'granted') {
const { status } = await Notifications.requestPermissionsAsync();
finalStatus = status;
}
if (finalStatus !== 'granted') {
//alert('Failed to get push token for push notification!');
return;
}
token = (await Notifications.getExpoPushTokenAsync({ projectId: "c2bb4d4e-4d4d-4f34-a873-7cad78c6023c", })).data;
} else {
//alert('Must use physical device for Push Notifications');
}
return token;
}
const MainNavigation = ({route}) => {
const gState = useSnapshot(GlobalState);
const viewer = gState.me;
const [expoPushToken, setExpoPushToken] = useState('');
const [notification, setNotification] = useState(false);
const notificationListener = useRef();
const responseListener = useRef();
const url = Linking2.useURL();
useEffect(() => {
// Do something with url
alert(url);
}, [url]);
useEffect(() => {
registerForPushNotificationsAsync().then(async (token) => {
let isLoggedIn = await API.isLoggedIn();
if (!isLoggedIn) return false;
API.registerToken(token);
return setExpoPushToken(token);
});
checkDeepLinking();
// This listener is fired whenever a notification is received while the app is foregrounded
notificationListener.current = Notifications.addNotificationReceivedListener(notification => {
//console.log("got notif", notification)
setNotification(notification);
});
// This listener is fired whenever a user taps on or interacts with a notification (works when app is foregrounded, backgrounded, or killed)
responseListener.current = Notifications.addNotificationResponseReceivedListener(response => {
//console.log("got notif click", notification)
console.log(response);
});
const interval = setInterval(async () => {
if(await API.isLoggedIn()){
let me = await API.getMe();
//console.log(JSON.stringify(viewer), JSON.stringify(me))
if(JSON.stringify(viewer) !== JSON.stringify(me)){
console.log("Updating me")
GlobalState.me = me;
}
}
}, 30000);
return () => {
Notifications.removeNotificationSubscription(notificationListener.current);
Notifications.removeNotificationSubscription(responseListener.current);
clearInterval(interval);
};
}, []);
return (
<>
(
),
header: () => { <>> },
}}
listeners={({ navigation, route }) => ({
tabPress: e => {
navigation.navigate('Feed', {reRender: Math.random()});
},
})}
/>
(
),
header: () => { <>> },
}}
/>
(
),
tabBarButton: (props) => (
{props.children}
),
header: () => { <>> },
}}
listeners={({ navigation, route }) => ({
tabPress: e => {
//console.log("listener", route)
if(route.name !== "NewPost"){
// Target current profile if one in route
navigation.navigate('NewPost', {toProfile: route.params?.profileid});
} else {
//Send function on child
navigation.navigate('NewPost', {toProfile: route.params?.profileid, sendNow: true});
}
},
})}
/>
(
),
header: () => { <>> },
}}
/>
(
),
header: () => { <>> },
}}
listeners={({ navigation, route }) => ({
tabPress: e => {
navigation.navigate('MyProfile', {profileid: viewer._id});
},
})}
/>
{/*
(
),
header: () => { <>> },
}}
/>*/}
>
)
}
export default function App() {
return (
,
}} theme={theme}>
{
return (
{props.navigation.canGoBack() ? {
props.navigation.goBack();
}} /> : {props.navigation.navigate('Menu');}} />}
{alert("Chats are comming soon.")}} />
{props.navigation.navigate("Search")}} />
{props.navigation.navigate("Notifications")}} />
)
},
}}>
);
}