import React, { useState, useEffect, useRef, useCallback } from 'react'; import { View, Text, TouchableOpacity, Platform, ActivityIndicator, } from 'react-native'; import MapView, { Marker, Region } from 'react-native-maps'; import { useNavigation } from '@react-navigation/native'; import { StackNavigationProp } from '@react-navigation/stack'; import { getStyles } from './setLocationScreen.styles'; import { PrimaryButton } from '@components'; import { useAppTheme } from '@theme'; import { AuthStackParamList } from '../../../navigation/authStack'; import { DEFAULT_LOCATION, getCurrentLocationWithAddress, reverseGeocode, LatLng, } from '../../../services/locationServices'; type NavProp = StackNavigationProp; const DELTA = { latitudeDelta: 0.006, longitudeDelta: 0.006 }; export const SetLocationScreen: React.FC = () => { const { colors } = useAppTheme(); const styles = getStyles(colors); const navigation = useNavigation(); const mapRef = useRef(null); const [coords, setCoords] = useState(DEFAULT_LOCATION); const [address, setAddress] = useState('Fetching your location…'); const [loading, setLoading] = useState(true); // ------------------------------------------------------------------ // Fetch current location on mount // ------------------------------------------------------------------ const fetchCurrentLocation = useCallback(async () => { setLoading(true); try { console.log('--------------'); const result = await getCurrentLocationWithAddress(); console.log('result', result); setCoords(result.coords); setAddress(result.address); // Animate the map to the user's location mapRef.current?.animateToRegion({ ...result.coords, ...DELTA }, 600); } catch { // Permission denied or GPS unavailable — keep the default location console.log('--------error------'); setAddress('Could not determine location'); } finally { setLoading(false); } }, []); useEffect(() => { fetchCurrentLocation(); }, [fetchCurrentLocation]); // ------------------------------------------------------------------ // When user drags the map, update the marker + address // ------------------------------------------------------------------ const handleRegionChangeComplete = useCallback(async (region: Region) => { const newCoords: LatLng = { latitude: region.latitude, longitude: region.longitude, }; setCoords(newCoords); setAddress('Fetching address…'); const newAddress = await reverseGeocode(newCoords); setAddress(newAddress); }, []); return ( {/* ---------- Map ---------- */} {/* ---------- "Locate me" floating button ---------- */} 📍 {/* ---------- Bottom sheet ---------- */} 📍 My Location {loading ? ( ) : ( {address} )} Change navigation.navigate('CompleteProfileScreen')} style={styles.confirmButton} /> navigation.navigate('CompleteProfileScreen')} activeOpacity={0.7} > Enter address manually ); };