- Notifications
You must be signed in to change notification settings - Fork1
Description
Description:
Hi,
Since we updated from version 2.1.3-2 to 2.2.2, we are experiencing an issue when checking if a file has changed before navigating back to the previous screen (e.g., when the user taps “Cancel”). Specifically, we want to determine if any changes have been made to the file before going back.
Previous Behavior (v2.1.3-2):
1.We copied the original PDF to a tempPath.
2.We opened the temp file for editing.
3.Before navigating back, we saved the temp file, computed the hash of both the original and temp files, and compared the hashes.
4.If the user made no changes, the hashes matched.
This approach worked perfectly in version 2.1.3-2.
Current Behavior (v2.2.2):
In version 2.2.2, the temp file is always different from the original, even when the user does nothing (i.e., no changes were made to the file).
Additional Notes:
•I also tested this on a fresh project to rule out other issues.
•We tried using the hasChange() function from the ref, but the promise never resolves.
Code Example:
Here is an example of the code we’re using:
function App(): React.JSX.Element { const pdfReaderRef = React.useRef<CPDFReaderView>(null); const [pdfPath, setPdfPath] = React.useState<{ absolutePath: string; originalPath: string; name: string; }>(); const [loading, setLoading] = React.useState(true); const tempPath = RNFS.TemporaryDirectoryPath + '/temp.pdf'; const COMPDFKIT_KEY = 'MY_VERY_SECRET_KEY' useEffect(() => { (async () => { console.log('Initialisation: ', await ComPDFKit.init_(COMPDFKIT_KEY)); })(); }, []); useEffect(() => { if (pdfPath === undefined) { (async () => { const filePath = await getFullPathOfSelectedPDF(); if (filePath !== undefined && filePath.originalPath !== '') { setPdfPath(filePath!); } })(); } console.log('Setting PDF Path:', pdfPath); }, [pdfPath]); const memoizedReaderView = useMemo(() => { if (!pdfPath) { return <Text>No PDF selected</Text>; } if (loading) { RNFS.copyFile(pdfPath.originalPath, tempPath).then(() => { setLoading(false); }); } return ( <CPDFReaderView ref={pdfReaderRef} document={tempPath} configuration={comPDFKitConfiguration} /> ); }, [pdfPath, loading, tempPath]); useEffect(() => { return () => { RNFS.unlink(tempPath); }; // eslint-disable-next-line react-hooks/exhaustive-deps }, []); const handleDidChange = async () => { console.log('Save document to temp path:', tempPath); await pdfReaderRef.current ?.save() .then(async () => { console.log('Document saved'); // Hash temp file and hash original file const hash = await RNFS.hash(tempPath, 'md5'); const originalHash = await RNFS.hash(pdfPath?.absolutePath!, 'md5'); // Compare hashes console.log('Hash:', hash); console.log('Original Hash:', originalHash); console.log('Hashes match:', hash === originalHash); }) .catch(err => { console.error('Error saving document:', err); }); }; return ( <SafeAreaView style={{flex: 1}}> <Button title="Did I change?" onPress={handleDidChange} /> {memoizedReaderView} </SafeAreaView> );}Could you please help us resolve this issue or confirm if this is a bug?
Thank you!