2024-03-27 20:36:03 +05:30
|
|
|
import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
|
|
|
import { NetworkConnectorModule } from "../../common/native-module/NativeModules";
|
2024-08-06 14:56:17 +05:30
|
|
|
import {
|
2024-10-09 14:46:26 +05:30
|
|
|
BENEFIT_SCREEN,
|
2024-08-06 14:56:17 +05:30
|
|
|
BUY_INSURANCE_SCREEN,
|
|
|
|
|
COMPARE_PLAN_SCREEN,
|
|
|
|
|
QUOTE_OFFER_SCREEN,
|
|
|
|
|
} from "../constants/ScreenNameConstants";
|
2024-03-27 20:36:03 +05:30
|
|
|
import { BUILD_CONFIG_DETAILS } from "../constants/StringConstant";
|
2024-07-19 01:01:24 +05:30
|
|
|
import { ScreenData } from "../interface/widgets/screenData/ScreenData";
|
2024-03-27 20:36:03 +05:30
|
|
|
|
|
|
|
|
export const getScreenDataFromCache = async (
|
2024-08-06 14:56:17 +05:30
|
|
|
key: string,
|
2024-03-27 20:36:03 +05:30
|
|
|
): Promise<ScreenData | null> => {
|
|
|
|
|
try {
|
|
|
|
|
const value = await AsyncStorage.getItem(key);
|
|
|
|
|
if (!!value) {
|
|
|
|
|
// The key exists in AsyncStorage
|
|
|
|
|
const deserializedObject = JSON.parse(value);
|
|
|
|
|
try {
|
|
|
|
|
return deserializedObject as ScreenData;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error("Error checking AsyncStorage:", error);
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const saveScreenDataInCache = async (
|
|
|
|
|
key: string | null,
|
2024-08-06 14:56:17 +05:30
|
|
|
data: ScreenData | null,
|
2024-03-27 20:36:03 +05:30
|
|
|
) => {
|
|
|
|
|
if (!!key && !!data) {
|
|
|
|
|
const serializedObject = JSON.stringify(data);
|
|
|
|
|
await AsyncStorage.setItem(key, serializedObject);
|
|
|
|
|
} else {
|
|
|
|
|
console.error("key or data was invalid");
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const getCacheKey = (
|
|
|
|
|
screenName: string | null | undefined,
|
2024-08-06 14:56:17 +05:30
|
|
|
screenKey: string | null | undefined,
|
2024-03-27 20:36:03 +05:30
|
|
|
) => {
|
|
|
|
|
if (!!screenKey && !!screenName) {
|
|
|
|
|
return screenName + "#" + screenKey;
|
|
|
|
|
} else {
|
|
|
|
|
return screenName;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export const setBuildConfigDetails = async () => {
|
2024-08-06 14:56:17 +05:30
|
|
|
let buildConfigData: string | undefined;
|
|
|
|
|
await NetworkConnectorModule.getBuildConfigDetails().then(
|
|
|
|
|
(response: string) => {
|
|
|
|
|
buildConfigData = response;
|
|
|
|
|
},
|
|
|
|
|
);
|
2024-03-27 20:36:03 +05:30
|
|
|
if (buildConfigData) {
|
|
|
|
|
await AsyncStorage.setItem(BUILD_CONFIG_DETAILS, buildConfigData);
|
|
|
|
|
}
|
2024-08-06 14:56:17 +05:30
|
|
|
};
|
2024-03-27 20:36:03 +05:30
|
|
|
|
2024-08-06 14:56:17 +05:30
|
|
|
export const getBuildConfigDetails =
|
|
|
|
|
async (): Promise<BuildConfigDetails | null> => {
|
|
|
|
|
const value = await AsyncStorage.getItem(BUILD_CONFIG_DETAILS);
|
|
|
|
|
try {
|
|
|
|
|
if (!!value) {
|
|
|
|
|
// The key exists in AsyncStorage
|
|
|
|
|
const buildConfigData = JSON.parse(value);
|
|
|
|
|
try {
|
|
|
|
|
return buildConfigData as BuildConfigDetails;
|
|
|
|
|
} catch (error) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
2024-03-27 20:36:03 +05:30
|
|
|
return null;
|
|
|
|
|
}
|
2024-08-06 14:56:17 +05:30
|
|
|
} catch (error) {
|
|
|
|
|
// Sentry log -> Error checking BuildConfigDetails
|
2024-03-27 20:36:03 +05:30
|
|
|
return null;
|
|
|
|
|
}
|
2024-08-06 14:56:17 +05:30
|
|
|
};
|
2024-03-27 20:36:03 +05:30
|
|
|
|
|
|
|
|
export const isScreenWhiteListedForCaching = (
|
2024-08-06 14:56:17 +05:30
|
|
|
screenName: string | null | undefined,
|
2024-03-27 20:36:03 +05:30
|
|
|
) => {
|
|
|
|
|
return !screensWithCachingDisabled.includes(screenName || "");
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-06 14:56:17 +05:30
|
|
|
export const screensWithCachingDisabled = [
|
|
|
|
|
BUY_INSURANCE_SCREEN,
|
|
|
|
|
QUOTE_OFFER_SCREEN,
|
|
|
|
|
COMPARE_PLAN_SCREEN,
|
2024-10-09 14:46:26 +05:30
|
|
|
BENEFIT_SCREEN,
|
2024-08-06 14:56:17 +05:30
|
|
|
];
|