Fix form keyboard issues and sticky footers in agent screens
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
import { useState, useEffect, useCallback } from 'react';
|
||||
import { Platform, PermissionsAndroid, Alert } from 'react-native';
|
||||
import Geolocation from '@react-native-community/geolocation';
|
||||
|
||||
interface UserLocation {
|
||||
latitude: number;
|
||||
longitude: number;
|
||||
}
|
||||
|
||||
interface UseUserLocationResult {
|
||||
location: UserLocation | null;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
refresh: () => void;
|
||||
}
|
||||
|
||||
/**
|
||||
* Hook to get and manage the user's current GPS location.
|
||||
* Handles permissions for both Android and iOS.
|
||||
*/
|
||||
export const useUserLocation = (): UseUserLocationResult => {
|
||||
const [location, setLocation] = useState<UserLocation | null>(null);
|
||||
const [loading, setLoading] = useState(true);
|
||||
const [error, setError] = useState<string | null>(null);
|
||||
|
||||
const requestPermission = useCallback(async (): Promise<boolean> => {
|
||||
if (Platform.OS === 'ios') {
|
||||
// iOS permission is handled via Info.plist; Geolocation auto-prompts
|
||||
return true;
|
||||
}
|
||||
|
||||
try {
|
||||
const granted = await PermissionsAndroid.request(
|
||||
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
|
||||
{
|
||||
title: 'Location Permission',
|
||||
message: 'BankCBS needs access to your location to find nearby branches.',
|
||||
buttonPositive: 'Allow',
|
||||
buttonNegative: 'Deny',
|
||||
},
|
||||
);
|
||||
return granted === PermissionsAndroid.RESULTS.GRANTED;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const fetchLocation = useCallback(async () => {
|
||||
setLoading(true);
|
||||
setError(null);
|
||||
|
||||
const hasPermission = await requestPermission();
|
||||
|
||||
if (!hasPermission) {
|
||||
setError('Location permission denied');
|
||||
setLoading(false);
|
||||
// Fallback to a default location (Bengaluru) for demo purposes
|
||||
setLocation({ latitude: 12.9716, longitude: 77.5946 });
|
||||
return;
|
||||
}
|
||||
|
||||
Geolocation.getCurrentPosition(
|
||||
(position) => {
|
||||
setLocation({
|
||||
latitude: position.coords.latitude,
|
||||
longitude: position.coords.longitude,
|
||||
});
|
||||
setLoading(false);
|
||||
},
|
||||
(err) => {
|
||||
setError(err.message);
|
||||
setLoading(false);
|
||||
// Fallback to a default location for demo purposes
|
||||
setLocation({ latitude: 12.9716, longitude: 77.5946 });
|
||||
},
|
||||
{
|
||||
enableHighAccuracy: true,
|
||||
timeout: 15000,
|
||||
maximumAge: 60000,
|
||||
},
|
||||
);
|
||||
}, [requestPermission]);
|
||||
|
||||
useEffect(() => {
|
||||
fetchLocation();
|
||||
}, [fetchLocation]);
|
||||
|
||||
return { location, loading, error, refresh: fetchLocation };
|
||||
};
|
||||
Reference in New Issue
Block a user