diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index dbaef9b9..0def7637 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -6,6 +6,7 @@ + diff --git a/src/common/TrackingComponent.tsx b/src/common/TrackingComponent.tsx index 9d8e448f..288f11ef 100644 --- a/src/common/TrackingComponent.tsx +++ b/src/common/TrackingComponent.tsx @@ -114,6 +114,7 @@ const TrackingComponent: React.FC = ({ children }) => { isActiveOnApp: Boolean(isActiveOnApp), userActivityOnApp: String(userActivityonApp), }; + dispatch(setDeviceGeolocationsBuffer(geolocation)); dispatch(sendLocationAndActivenessToServer([geolocation])); } catch (e: any) { logError(e, 'Error during background location sending.'); diff --git a/src/components/form/services/geoLocation.service.ts b/src/components/form/services/geoLocation.service.ts index e63a4ee8..c0f070fb 100644 --- a/src/components/form/services/geoLocation.service.ts +++ b/src/components/form/services/geoLocation.service.ts @@ -6,6 +6,12 @@ import { addClickstreamEvent } from '../../../services/clickstreamEventService'; import { CLICKSTREAM_EVENT_NAMES } from '../../../common/Constants'; const FIVE_MIN = 5 * 60 * 1000; +export interface DeviceLocation { + latitude: number; + longitude: number; + timestamp: number; +} + export class CaptureGeolocation { private static capturedLocation: { [caseId: string]: { @@ -72,7 +78,35 @@ export class CaptureGeolocation { text1: 'Error getting geolocation' + JSON.stringify(error || {}), }); }, - { enableHighAccuracy: true, timeout: 1e4, maximumAge: 1e4, showLocationDialog: false } + { enableHighAccuracy: true, timeout: 1e4, maximumAge: 1e4, forceRequestLocation: true } + ); + }); + } + + static async watchLocation( + callbackFn: (location: DeviceLocation) => void + ): Promise { + return new Promise(async (resolve, reject) => { + const isLocationOn = await PermissionsAndroid.check( + PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION + ); + if (!isLocationOn) { + resolve(undefined); + } + Geolocation.watchPosition( + (position) => { + callbackFn?.({ + latitude: position.coords.latitude, + longitude: position.coords.longitude, + timestamp: Date.now(), + }); + resolve(position.coords); + }, + (error) => { + logError(error as any, 'Unable to get location'); + reject(error); + }, + { enableHighAccuracy: true, distanceFilter: 1, forceRequestLocation: true } ); }); } diff --git a/src/screens/addressGeolocation/AddressItem.tsx b/src/screens/addressGeolocation/AddressItem.tsx index e5da4956..7ccf3341 100644 --- a/src/screens/addressGeolocation/AddressItem.tsx +++ b/src/screens/addressGeolocation/AddressItem.tsx @@ -149,7 +149,9 @@ function AddressItem({ {showRelativeDistance && relativeDistanceBwLatLong ? ( <>({relativeDistanceFormatter(relativeDistanceBwLatLong)} km away) - ) : null} + ) : ( + '--' + )} {lastFeedbackForAddress?.feedbackPresent ? ( diff --git a/src/screens/addressGeolocation/index.tsx b/src/screens/addressGeolocation/index.tsx index 239246f5..1adc9bd2 100644 --- a/src/screens/addressGeolocation/index.tsx +++ b/src/screens/addressGeolocation/index.tsx @@ -126,21 +126,19 @@ const AddressGeolocation: React.FC = ({ route: routeParams useEffect(() => { addClickstreamEvent(CLICKSTREAM_EVENT_NAMES.FA_ALL_ADDRESSES_LANDED, commonParams); - if (!currentGeolocationCoordinates.latitude || !currentGeolocationCoordinates.longitude) { - (async () => { - const location = await CaptureGeolocation.fetchLocation(`${Date.now()}`, 0); + (async () => { + const location = await CaptureGeolocation.fetchLocation(`${Date.now()}`, 0); - if (location != null) { - dispatch( - setDeviceGeolocation({ - latitude: location.latitude, - longitude: location.longitude, - timestamp: Date.now(), - }) - ); - } - })(); - } + if (location != null) { + dispatch( + setDeviceGeolocation({ + latitude: location.latitude, + longitude: location.longitude, + timestamp: Date.now(), + }) + ); + } + })(); }, []); if (!isOnline) { diff --git a/src/screens/auth/ProtectedRouter.tsx b/src/screens/auth/ProtectedRouter.tsx index 884d7fa0..622a0b82 100644 --- a/src/screens/auth/ProtectedRouter.tsx +++ b/src/screens/auth/ProtectedRouter.tsx @@ -33,6 +33,8 @@ import RegisterPayments from '../registerPayements/RegisterPayments'; import TodoList from '../todoList/TodoList'; import UngroupedAddressContainer from '../addressGeolocation/UngroupedAddressContainer'; import { getAgentDetail } from '../../action/authActions'; +import { CaptureGeolocation, DeviceLocation } from '@components/form/services/geoLocation.service'; +import { setDeviceGeolocation } from '@reducers/foregroundServiceSlice'; const Stack = createNativeStackNavigator(); @@ -112,6 +114,13 @@ const ProtectedRouter = () => { // Firestore listener hook useFirestoreUpdates(); + React.useEffect(() => { + // Watching Position for significant change + CaptureGeolocation.watchLocation((location: DeviceLocation) => + dispatch(setDeviceGeolocation(location)) + ); + }, []); + return (