NTP-20841 | PDF module integration (#1045)
This commit is contained in:
@@ -23,7 +23,7 @@ import {
|
||||
calculateBottomSheetHeight,
|
||||
sendCommunicationViaNaviAccount,
|
||||
} from '@screens/allCases/utils';
|
||||
import { IDocument, IDocumentItem } from './interface';
|
||||
import { DocumentContentType, IDocumentItem } from './interface';
|
||||
import { DOCUMENT_NOT_AVAILABLE } from '@constants/Global';
|
||||
import { NAVI_ACCOUNT_SHARE_CHANNELS, ShareVia } from './constants';
|
||||
import PDFScreenShareJourneyBottomSheet from './PDFScreenShareJourneyBottomSheet';
|
||||
@@ -33,6 +33,8 @@ import { logError } from '@components/utlis/errorUtils';
|
||||
import { GenericType } from '@common/GenericTypes';
|
||||
import PDFDownload from './PDFDownload';
|
||||
import { useAppSelector } from '@hooks';
|
||||
import { getFileType } from './PDFUtil';
|
||||
import FastImage from 'react-native-fast-image';
|
||||
|
||||
interface IPdfFullScreen {
|
||||
route: {
|
||||
@@ -66,6 +68,7 @@ const PDFFullScreen: React.FC<IPdfFullScreen> = (props) => {
|
||||
const cacheFileKey = referenceId + caseId;
|
||||
const [isLoading, setIsLoading] = useState<boolean>(false);
|
||||
const [pdfFilePath, setPdfFilePath] = useState<string>('');
|
||||
const [imagePath, setImagePath] = useState<string>('');
|
||||
const [error, setError] = useState<boolean>(false);
|
||||
const [pdfUrl, setPdfUrl] = useState(pdfUri);
|
||||
const [isValidating, setIsValidating] = useState(false);
|
||||
@@ -132,7 +135,8 @@ const PDFFullScreen: React.FC<IPdfFullScreen> = (props) => {
|
||||
useEffect(() => {
|
||||
const cacheDirectory = RNFS.CachesDirectoryPath;
|
||||
const sanitizedHighQualityUri = cacheFileKey;
|
||||
const cacheFilePath = `${cacheDirectory}/${sanitizedHighQualityUri}.pdf`;
|
||||
const cacheFilePath = `${cacheDirectory}/${sanitizedHighQualityUri}`;
|
||||
const metadataFilePath = `${cacheDirectory}/${sanitizedHighQualityUri}.json`;
|
||||
const fetchPdf = async () => {
|
||||
setIsLoading(true);
|
||||
setError(false);
|
||||
@@ -143,14 +147,21 @@ const PDFFullScreen: React.FC<IPdfFullScreen> = (props) => {
|
||||
highQualityResponse = await RNFetchBlob.fetch('GET', doc?.uri);
|
||||
} else {
|
||||
await RNFS.mkdir(cacheDirectory);
|
||||
const exists = await RNFS.exists(cacheFilePath);
|
||||
|
||||
if (exists) {
|
||||
setPdfFilePath(cacheFilePath);
|
||||
setIsLoading(false);
|
||||
setError(false);
|
||||
return;
|
||||
}
|
||||
try {
|
||||
const exists = await RNFS.exists(cacheFilePath);
|
||||
const metadataExists = await RNFS.exists(metadataFilePath);
|
||||
if (exists && metadataExists) {
|
||||
const metadataContent = await RNFS.readFile(metadataFilePath, 'utf8');
|
||||
const { fileType } = JSON.parse(metadataContent);
|
||||
setIsLoading(false);
|
||||
setError(false);
|
||||
if (fileType === DocumentContentType.PDF) {
|
||||
setPdfFilePath(cacheFilePath);
|
||||
return;
|
||||
}
|
||||
setImagePath(cacheFilePath);
|
||||
}
|
||||
} catch (error) {}
|
||||
highQualityResponse = await RNFetchBlob.fetch('GET', pdfUrl);
|
||||
}
|
||||
|
||||
@@ -159,10 +170,17 @@ const PDFFullScreen: React.FC<IPdfFullScreen> = (props) => {
|
||||
setError(true);
|
||||
} else if (highQualityResponse.respInfo.status === 200) {
|
||||
const highQualityImageBase64 = await highQualityResponse.base64();
|
||||
RNFS.writeFile(cacheFilePath, highQualityImageBase64, 'base64');
|
||||
setPdfFilePath(cacheFilePath);
|
||||
const documentType = await getFileType(highQualityImageBase64);
|
||||
await RNFS.writeFile(cacheFilePath, highQualityImageBase64, 'base64');
|
||||
const metadata = { fileType: documentType };
|
||||
await RNFS.writeFile(metadataFilePath, JSON.stringify(metadata), 'utf8');
|
||||
setIsLoading(false);
|
||||
setError(false);
|
||||
if (documentType === DocumentContentType.PDF) {
|
||||
setPdfFilePath(cacheFilePath);
|
||||
return;
|
||||
}
|
||||
setImagePath(cacheFilePath);
|
||||
}
|
||||
} catch (error) {
|
||||
setIsLoading(false);
|
||||
@@ -197,6 +215,13 @@ const PDFFullScreen: React.FC<IPdfFullScreen> = (props) => {
|
||||
</SafeAreaView>
|
||||
)}
|
||||
|
||||
{imagePath && (
|
||||
<FastImage
|
||||
source={{ uri: `file://${imagePath}` }}
|
||||
style={styles.h100}
|
||||
resizeMode={FastImage.resizeMode.contain}
|
||||
/>
|
||||
)}
|
||||
{isLoading && (
|
||||
<ActivityIndicator
|
||||
size={'large'}
|
||||
@@ -237,4 +262,7 @@ const styles = StyleSheet.create({
|
||||
width: '100%',
|
||||
color: COLORS.TEXT.RED,
|
||||
},
|
||||
h100: {
|
||||
height: '100%',
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user