NTP-24325 | Remove polling

This commit is contained in:
Aman Chaturvedi
2025-01-16 19:08:45 +05:30
parent ad768570b6
commit 4ce8e38e58
3 changed files with 32 additions and 35 deletions

View File

@@ -1,8 +1,13 @@
import { StyleSheet, View } from 'react-native';
import React from 'react';
import React, { useEffect } from 'react';
import Text from '../../RN-UI-LIB/src/components/Text';
import Heading from '../../RN-UI-LIB/src/components/Heading';
import Button from '../../RN-UI-LIB/src/components/Button';
import { MILLISECONDS_IN_A_SECOND } from '@rn-ui-lib/utils/common';
import usePolling from '@hooks/usePolling';
import { useAppDispatch, useAppSelector } from '@hooks';
import { BLOCKER_SCREEN_DATA } from './Constants';
import { checkLocationEnabled } from '@hooks/useIsLocationEnabled';
interface IActionButton {
title: string;
@@ -16,11 +21,28 @@ interface IBlockerInstructions {
actionBtn?: IActionButton;
}
const CHECK_DEVICE_LOCATION_INTERVAL = 2 * MILLISECONDS_IN_A_SECOND;
const BlockerInstructions: React.FC<IBlockerInstructions> = ({
heading,
instructions,
actionBtn,
}) => {
const { isDeviceLocationEnabled } = useAppSelector((state) => state.foregroundService);
const dispatch = useAppDispatch();
const stopLocationPolling = usePolling(() => {
if (heading === BLOCKER_SCREEN_DATA.DEVICE_LOCATION_OFF.heading && !isDeviceLocationEnabled) {
dispatch(checkLocationEnabled(isDeviceLocationEnabled));
}
}, CHECK_DEVICE_LOCATION_INTERVAL);
useEffect(() => {
return () => {
stopLocationPolling();
};
}, []);
return (
<View style={styles.errorContainer}>
<Heading type={'h4'} style={styles.errorHeading} bold dark>

View File

@@ -5,16 +5,10 @@ import { logError } from '../components/utlis/errorUtils';
import { useEffect } from 'react';
import { AppState } from 'react-native';
import { AppStates } from '@interfaces/appStates';
import usePolling from './usePolling';
import { MILLISECONDS_IN_A_SECOND } from '@rn-ui-lib/utils/common';
import { AppDispatch } from '@store';
const CHECK_DEVICE_LOCATION_INTERVAL = 2 * MILLISECONDS_IN_A_SECOND ;
const useIsLocationEnabled = () => {
const { isDeviceLocationEnabled } = useAppSelector((state) => state.foregroundService);
const dispatch = useAppDispatch();
const checkLocationEnabled = async () => {
export const checkLocationEnabled =
(isDeviceLocationEnabled: boolean) => async (dispatch: AppDispatch) => {
try {
const isLocationEnabled = await locationEnabled();
if (!isDeviceLocationEnabled && isLocationEnabled) {
@@ -30,16 +24,14 @@ const useIsLocationEnabled = () => {
}
};
usePolling(() => {
if(!isDeviceLocationEnabled) {
checkLocationEnabled();
}
}, CHECK_DEVICE_LOCATION_INTERVAL);
const useIsLocationEnabled = () => {
const { isDeviceLocationEnabled } = useAppSelector((state) => state.foregroundService);
const dispatch = useAppDispatch();
useEffect(() => {
const appStateChange = AppState.addEventListener('change', async (change) => {
if (change === AppStates.ACTIVE) {
checkLocationEnabled();
dispatch(checkLocationEnabled(isDeviceLocationEnabled));
}
});
return () => {

View File

@@ -5,11 +5,8 @@ import {
import React, { useEffect } from 'react';
import { _map, MILLISECONDS_IN_A_MINUTE } from '../../../RN-UI-LIB/src/utlis/common';
import { getNotifications, notificationAction } from '../../action/notificationActions';
import { LocalStorageKeys, SCREEN_ANIMATION_DURATION } from '../../common/Constants';
import {
getScreenFocusListenerObj,
setAsyncStorageItem,
} from '../../components/utlis/commonFunctions';
import { SCREEN_ANIMATION_DURATION } from '../../common/Constants';
import { getScreenFocusListenerObj } from '../../components/utlis/commonFunctions';
import { useAppDispatch, useAppSelector } from '../../hooks';
import useIsOnline from '../../hooks/useIsOnline';
import AllCasesMain from '../allCases';
@@ -18,19 +15,12 @@ import ImpersonatedUser from '../impersonatedUser';
import Notifications from '../notifications';
import TodoList from '../todoList/TodoList';
import { getAgentDetail } from '../../action/authActions';
import { CaptureGeolocation, DeviceLocation } from '@components/form/services/geoLocation.service';
import { setDeviceGeolocation } from '@reducers/foregroundServiceSlice';
import NearbyCases from '@screens/allCases/NearbyCases';
import usePolling from '@hooks/usePolling';
import useResyncFirebase from '@hooks/useResyncFirebase';
import CaseDetailStack from '@screens/caseDetails/CaseDetailStack';
import { getFirestoreResyncIntervalInMinutes } from '@common/AgentActivityConfigurableConstants';
import { getSelfieDocument } from '@actions/profileActions';
import getLitmusExperimentResult, {
LitmusExperimentName,
LitmusExperimentNameMap,
} from '@services/litmusExperiments.service';
import { GLOBAL } from '@constants/Global';
const Stack = createNativeStackNavigator();
@@ -81,19 +71,12 @@ const ProtectedRouter = () => {
const resyncFirebase = useResyncFirebase();
const stopLocationPolling = usePolling(() => {
CaptureGeolocation.watchLocation((location: DeviceLocation) => {
return dispatch(setDeviceGeolocation(location));
});
}, 3 * MILLISECONDS_IN_A_MINUTE);
const stopFirebaseResyncPolling = usePolling(() => {
void resyncFirebase();
}, getFirestoreResyncIntervalInMinutes() * MILLISECONDS_IN_A_MINUTE);
useEffect(() => {
return () => {
stopLocationPolling();
stopFirebaseResyncPolling();
};
}, []);