diff --git a/client/App.js b/client/App.js index d3c38fe..94e25a6 100644 --- a/client/App.js +++ b/client/App.js @@ -12,13 +12,16 @@ import AccountScreen from './src/AccountScreen'; import SignUpScreen from './src/SignUp'; import DisplayRouteScreen from './src/DisplayRoute'; import FriendsScreen from './src/FriendsScreen'; +import { retrieveData } from './src/caching'; const Stack = createNativeStackNavigator(); export default function App() { + const firstScreen = + retrieveData('username') === null ? 'AccountScreen' : 'Map'; return ( - + { + const fetchUsername = async () => { + try { + const username = await retrieveData('username'); + setUsernameValid(username); + } catch (error) { + console.error('Error retrieving username:', error); + } + }; + fetchUsername(); +},[]); + + const logout = async () => { + try { + await removeData('username'); + setUsernameValid(null); + navigation.navigate('Map'); + } catch (error) { + console.error('Error logging out:', error); + } + }; return ( - navigation.navigate('LoginScreen')} - color="#841584" - > - Log In - - - navigation.navigate('SignUpScreen')} - color="#841584" - > - Sign Up - - - navigation.navigate('Map')} - color="#841584" - > - Continue as Guest - + {usernameValid === null ? ( + <> + navigation.navigate('LoginScreen')} + color="#841584" + > + Log In + + navigation.navigate('SignUpScreen')} + color="#841584" + > + Sign Up + + navigation.navigate('Map')} + color="#841584" + > + Continue as Guest + + + ) : ( + <> + + Log Out + + You are logged in + + )} ); diff --git a/client/src/LogIn.js b/client/src/LogIn.js index 509b2dd..ee10cfb 100644 --- a/client/src/LogIn.js +++ b/client/src/LogIn.js @@ -9,7 +9,7 @@ import { } from 'react-native'; import { handleLogin } from './utils/accountUtils'; import styles from './components/styles/Login.styles'; -import { retrieveData } from './caching'; + export default function LoginScreen({ navigation }) { const [username, setUsername] = useState(''); @@ -19,8 +19,6 @@ export default function LoginScreen({ navigation }) { return ( - {retrieveData('username') === null ? ( - <> ref2.current.focus()} /> - - handleLogin(username, password, navigation)} @@ -45,19 +41,6 @@ export default function LoginScreen({ navigation }) { > Log In - - ) : ( - <> - You are logged in - navigation.navigate('Map')} - color="#841584" - > - Go to Map - - - )} ); diff --git a/client/src/Map.js b/client/src/Map.js index db37c7c..6c5e4d3 100644 --- a/client/src/Map.js +++ b/client/src/Map.js @@ -109,7 +109,7 @@ export default function MapScreen({ navigation }) { style={MapStyles.loginButton} onPress={() => navigation.navigate('AccountScreen')} > - Log In + Account { export const removeData = async (key) => { try { await AsyncStorage.removeItem(JSON.stringify(key)); - console.log('Removed successfully'); } catch { console.error('Error removing data from cache'); } diff --git a/client/src/map(temp).js b/client/src/map(temp).js deleted file mode 100644 index 04100dc..0000000 --- a/client/src/map(temp).js +++ /dev/null @@ -1,294 +0,0 @@ -// import React, { useState, useEffect, useRef } from 'react'; -// import { -// StyleSheet, -// View, -// Text, -// TouchableOpacity, -// Alert, -// Platform, -// } from 'react-native'; -// import MapView, { Marker, Polyline } from 'react-native-maps'; -// import * as Location from 'expo-location'; -// import { decode } from '@googlemaps/polyline-codec'; // For decoding the polyline - -// export default function MapScreen({ navigation }) { -// const [webSocket, setWebSocket] = useState(null); -// const [location, setLocation] = useState(null); // No hardcoded initial location -// const [errorMessage, setErrorMessage] = useState(''); -// const [polylineCoordinates, setPolylineCoordinates] = useState([]); // State for polyline coordinates -// const mapRef = useRef(null); // Reference to the MapView - -// // WebSocket Setup -// useEffect(() => { -// const wsUrl = `${process.env.EXPO_PUBLIC_API_URL.replace(/^http/, 'ws')}/ws/location`; -// console.log('Connecting to WebSocket:', wsUrl); - -// const socket = new WebSocket(wsUrl); - -// socket.onopen = () => { -// console.log('WebSocket connection opened'); -// setWebSocket(socket); -// }; - -// socket.onmessage = (event) => { -// console.log('Message from server:', event.data); -// }; - -// socket.onerror = (error) => { -// console.error('WebSocket error:', error); -// }; - -// socket.onclose = () => { -// console.log('WebSocket connection closed'); -// }; - -// return () => { -// socket.close(); -// }; -// }, []); - -// // Location Setup -// useEffect(() => { -// (async () => { -// const { status } = await Location.requestForegroundPermissionsAsync(); -// if (status !== 'granted') { -// setErrorMessage('Permission to access location was denied.'); -// return; -// } - -// // Fetch initial location -// const currentLocation = await Location.getCurrentPositionAsync({ -// accuracy: Location.Accuracy.BestForNavigation, -// }); -// const { latitude, longitude } = currentLocation.coords; -// setLocation({ latitude, longitude }); - -// // Set up continuous location tracking -// const locationSubscription = await Location.watchPositionAsync( -// { -// accuracy: Location.Accuracy.BestForNavigation, -// timeInterval: 5000, // Update every 5 seconds -// distanceInterval: 5, // Update if device moves 5 meters -// }, -// (newLocation) => { -// const { latitude, longitude } = newLocation.coords; -// console.log('Updated Location:', { latitude, longitude }); -// setLocation({ latitude, longitude }); - -// // Animate the map to the new region -// if (mapRef.current) { -// mapRef.current.animateToRegion( -// { -// latitude, -// longitude, -// latitudeDelta: 0.01, -// longitudeDelta: 0.01, -// }, -// 1000, // Animation duration in milliseconds -// ); -// } -// }, -// ); - -// return () => locationSubscription.remove(); -// })(); -// }, []); - -// // Function to Fetch and Decode Polyline -// const fetchPolyline = async () => { -// try { -// const response = await fetch( -// `${process.env.EXPO_PUBLIC_API_URL}/wayfinding/get_routes`, -// { -// method: 'POST', -// headers: { 'Content-Type': 'application/json' }, -// body: JSON.stringify({ -// origin: 'Tara Street, Dublin', -// destination: 'Ashbourne, Ireland', -// mode: 'walking', -// alternatives: false, -// }), -// }, -// ); - -// const data = await response.json(); - -// if (data.route) { -// setPolylineCoordinates(data.route); // The response is already a list of { latitude, longitude } -// } -// } catch (error) { -// console.error('Error fetching polyline:', error); -// } -// }; - -// // Function to Send Location to WebSocket -// const sendLocation = () => { -// if (webSocket && location) { -// const locationData = JSON.stringify(location); -// webSocket.send(locationData); -// console.log('Sent location:', locationData); -// } else if (!location) { -// Alert.alert( -// 'Location not available', -// 'Please wait for the GPS to fetch your location.', -// ); -// } else { -// Alert.alert( -// 'WebSocket not connected', -// 'Please wait for the WebSocket connection to establish.', -// ); -// } -// }; - -// // Fetch polyline when the component mounts -// useEffect(() => { -// fetchPolyline(); -// }, []); - -// return ( -// -// {errorMessage ? ( -// {errorMessage} -// ) : location ? ( -// <> -// -// -// -// {/* Render the polyline */} -// {polylineCoordinates.length > 0 && ( -// -// )} -// -// -// Send Location -// -// navigation.navigate('LoginScreen')} -// > -// Log In -// -// navigation.navigate('FindRouteScreen')} -// > -// Find Route -// -// navigation.navigate('WeatherScreen')} -// > -// Weather -// -// -// ) : ( -// Fetching your location... -// )} -// -// ); -// } - -// const styles = StyleSheet.create({ -// container: { -// flex: 1, -// ...(Platform.OS === 'web' ? { height: '100vh' } : {}), -// }, -// map: { -// flex: 1, -// minHeight: 300, -// }, -// sendButton: { -// position: 'absolute', -// bottom: 20, -// left: '50%', -// transform: [{ translateX: -50 }], -// backgroundColor: '#007bff', -// paddingVertical: 10, -// paddingHorizontal: 20, -// borderRadius: 10, -// }, -// buttonText: { -// color: '#fff', -// fontWeight: 'bold', -// textAlign: 'center', -// }, -// TouchableOpacity: { -// position: 'absolute', -// alignItems: 'center', -// left: '70%', -// top: '0%', -// backgroundColor: '#007bff', -// paddingVertical: 10, -// paddingHorizontal: 40, -// borderRadius: 10, -// shadowColor: '#000', -// shadowOpacity: 0.2, -// shadowOffset: { width: 0, height: 2 }, -// shadowRadius: 5, -// elevation: 5, -// }, -// TouchableOpacity1: { -// position: 'absolute', -// alignItems: 'center', -// left: '0%', -// top: '0%', -// backgroundColor: '#007bff', -// paddingVertical: 10, -// paddingHorizontal: 40, -// borderRadius: 10, -// shadowColor: '#000', -// shadowOpacity: 0.2, -// shadowOffset: { width: 0, height: 2 }, -// shadowRadius: 5, -// elevation: 5, -// }, -// findRouteButton: { -// position: 'absolute', -// alignItems: 'center', -// left: '34%', -// top: '0%', -// backgroundColor: '#007bff', -// paddingVertical: 10, -// paddingHorizontal: 35, -// borderRadius: 10, -// shadowColor: '#000', -// shadowOpacity: 0.2, -// shadowOffset: { width: 0, height: 2 }, -// shadowRadius: 5, -// elevation: 5, -// }, -// error: { -// flex: 1, -// textAlign: 'center', -// textAlignVertical: 'center', -// fontSize: 18, -// color: 'red', -// }, -// loadingText: { -// flex: 1, -// textAlign: 'center', -// textAlignVertical: 'center', -// fontSize: 18, -// }, -// });