logic changed for edge case

This commit is contained in:
ShriPrakashBajpai
2023-09-17 16:41:41 +05:30
parent 66e8a8b842
commit dc40a3c54f
2 changed files with 40 additions and 22 deletions

View File

@@ -42,6 +42,7 @@ import {
} from './AgentActivityConfigurableConstants';
import RNFS from 'react-native-fs';
import { GlobalImageMap } from './CachedImage';
import { get } from 'react-hook-form';
export enum FOREGROUND_TASKS {
GEOLOCATION = 'GEOLOCATION',
@@ -200,9 +201,12 @@ const TrackingComponent: React.FC<ITrackingComponent> = ({ children }) => {
//bt==>app is minimized or closed
const foregroundTimestamp = await getItem(StorageKeys.APP_FOREGROUND_TIMESTAMP);
const backgroundTimestamp = await getItem(StorageKeys.APP_BACKGROUND_TIMESTAMP);
const stateSetTimestamp = await getItem(StorageKeys.STATE_SET_TIMESTAMP);
const foregroundTime = dayJs(foregroundTimestamp);
const backgroundTime = dayJs(backgroundTimestamp);
const stateSetTime = dayJs(stateSetTimestamp);
const diffBetweenBackgroundAndForegroundTime = dayJs(backgroundTime).diff(
foregroundTime,
'seconds'
@@ -211,15 +215,18 @@ const TrackingComponent: React.FC<ITrackingComponent> = ({ children }) => {
dayJs().diff(foregroundTime, 'seconds') < 0 ? 0 : dayJs().diff(foregroundTime, 'seconds');
const diffBetweenCurrentTimeAndBackgroundTime =
dayJs().diff(backgroundTime, 'minutes') < 0 ? 0 : dayJs().diff(backgroundTime, 'minutes');
const diffBetweenCurrentTimeAndSetStateTime =
dayJs().diff(stateSetTime, 'minutes') < 0 ? 0 : dayJs().diff(stateSetTime, 'minutes');
const ACTIVITY_TIME_ON_APP = getActivityTimeOnApp();
const ACTIVITY_TIME_WINDOW_HIGH = getActivityTimeWindowHigh();
const ACTIVITY_TIME_WINDOW_MEDIUM = getActivityTimeWindowMedium();
const isBackgroundTimeWithInHighRange =
diffBetweenCurrentTimeAndBackgroundTime < ACTIVITY_TIME_WINDOW_HIGH;
const isBackgroundTimeWithInMediumRange =
diffBetweenCurrentTimeAndBackgroundTime < ACTIVITY_TIME_WINDOW_MEDIUM;
const isStateSetTimeMoreThanHighRangeAndLessThanMediumRange =
diffBetweenCurrentTimeAndSetStateTime >= ACTIVITY_TIME_WINDOW_HIGH;
const isStateSetTimeMoreThanMediumRange =
diffBetweenCurrentTimeAndSetStateTime >=
ACTIVITY_TIME_WINDOW_MEDIUM - ACTIVITY_TIME_WINDOW_HIGH;
const isForegroundTimeAfterBackground = dayJs(foregroundTimestamp).isAfter(backgroundTimestamp);
if (AppState.currentState === AppStates.ACTIVE) {
@@ -238,13 +245,15 @@ const TrackingComponent: React.FC<ITrackingComponent> = ({ children }) => {
console.log('handleUpdateActivity');
console.log(foregroundTimestamp);
console.log(backgroundTimestamp);
console.log(stateSetTimestamp);
console.log(foregroundTime);
console.log(backgroundTime);
console.log(stateSetTime);
console.log(diffBetweenBackgroundAndForegroundTime);
console.log(diffBetweenCurrentTimeAndForegroundTime);
console.log(diffBetweenCurrentTimeAndBackgroundTime);
console.log(isBackgroundTimeWithInHighRange);
console.log(isBackgroundTimeWithInMediumRange);
console.log(isStateSetTimeMoreThanHighRangeAndLessThanMediumRange);
console.log(isStateSetTimeMoreThanMediumRange);
console.log(isForegroundTimeAfterBackground);
console.log(ACTIVITY_TIME_ON_APP);
console.log(ACTIVITY_TIME_WINDOW_HIGH);
@@ -252,28 +261,31 @@ const TrackingComponent: React.FC<ITrackingComponent> = ({ children }) => {
if (isForegroundTimeAfterBackground) {
if (diffBetweenCurrentTimeAndForegroundTime >= ACTIVITY_TIME_ON_APP) {
await setItem(StorageKeys.USER_ACTIVITY_ON_APP, AgentActivity.HIGH);
await setItem(StorageKeys.STATE_SET_TIMESTAMP, dayJs().toString());
console.log('isForegroundTimeAfterBackground HIGH');
await setItem(StorageKeys.USER_ACTIVITY_ON_APP, AgentActivity.HIGH);
console.log('High State Set Time' + (await getItem(StorageKeys.STATE_SET_TIMESTAMP)));
return;
}
return;
} else if (isBackgroundTimeWithInHighRange) {
if (diffBetweenBackgroundAndForegroundTime >= ACTIVITY_TIME_ON_APP) {
console.log('isBackgroundTimeWithInHighRange HIGH');
await setItem(StorageKeys.USER_ACTIVITY_ON_APP, AgentActivity.HIGH);
return;
}
} else if (
isStateSetTimeMoreThanHighRangeAndLessThanMediumRange &&
(await getItem(StorageKeys.USER_ACTIVITY_ON_APP)) == AgentActivity.HIGH
) {
await setItem(StorageKeys.USER_ACTIVITY_ON_APP, AgentActivity.MEDIUM);
await setItem(StorageKeys.STATE_SET_TIMESTAMP, stateSetTime.add(2, 'minutes').toString());
console.log('isStateSetTimeWithInMediumRange MEDIUM');
console.log('Medium State Set Time' + (await getItem(StorageKeys.STATE_SET_TIMESTAMP)));
return;
} else if (isBackgroundTimeWithInMediumRange) {
if (diffBetweenBackgroundAndForegroundTime >= ACTIVITY_TIME_ON_APP) {
console.log('isBackgroundTimeWithInMediumRange MEDIUM');
await setItem(StorageKeys.USER_ACTIVITY_ON_APP, AgentActivity.MEDIUM);
return;
}
return;
} else {
console.log('isBackgroundTimeWithInLowRange LOW');
} else if (
isStateSetTimeMoreThanMediumRange &&
(await getItem(StorageKeys.USER_ACTIVITY_ON_APP)) == AgentActivity.MEDIUM
) {
await setItem(StorageKeys.USER_ACTIVITY_ON_APP, AgentActivity.LOW);
await setItem(StorageKeys.STATE_SET_TIMESTAMP, stateSetTime.add(2, 'minutes').toString());
console.log('isStateSetTimeWithInLowRange LOW');
console.log('Low State Set Time' + (await getItem(StorageKeys.STATE_SET_TIMESTAMP)));
} else {
return;
}
};
@@ -388,6 +400,11 @@ const TrackingComponent: React.FC<ITrackingComponent> = ({ children }) => {
if (diffBetweenBackgroundAndForegroundTime >= ACTIVITY_TIME_ON_APP) {
await setItem(StorageKeys.USER_ACTIVITY_ON_APP, AgentActivity.HIGH);
await setItem(StorageKeys.STATE_SET_TIMESTAMP, dayJs().toString());
console.log('AppGoesBackgroundState HIGH');
console.log(
'High State Set Time on Background' + (await getItem(StorageKeys.STATE_SET_TIMESTAMP))
);
return;
}
return;

View File

@@ -3,4 +3,5 @@ export enum StorageKeys {
APP_BACKGROUND_TIMESTAMP = 'appBackgroundTimestamp',
IS_USER_ACTIVE = 'isUserActive',
USER_ACTIVITY_ON_APP = 'userActivityOnApp',
STATE_SET_TIMESTAMP = 'stateSetTimestamp',
}