π End-to-end encrypted cloud for everything.
Dart 2faandroidauthye2eeencryptionend-to-end-encryptionfluttergolanggoogle-photosgoogle-photos-alternativeiosopensourcephotosprivacyprivacy-toolstypescriptzero-knowledge
Improve robustness of refresh logic
Manav Rathie2f40f1e33ba
diff --git a/web/packages/gallery/components/viewer/FileViewer.tsx b/web/packages/gallery/components/viewer/FileViewer.tsxindex 5858b20204..1cdffd5943 100644--- a/web/packages/gallery/components/viewer/FileViewer.tsx+++ b/web/packages/gallery/components/viewer/FileViewer.tsx@@ -770,76 +770,32 @@ export const FileViewer: React.FC<FileViewerProps> = ({performKeyAction,]);- // Update the active annotated file, if needed, on updates to files.+ // Handle updates to files.+ //+ // [Note: Updates to the files prop for FileViewer]+ //+ // On updates to the `files` prop,+ //+ // - If the file that was previously being shown is no longer being shown,+ // move to the next slide if possible, or close the viewer otherwise (say+ // if there are no more files remaining, e.g. we archived the last one+ // when in the all section).+ //+ // - If the file that was previously being shown has moved to a new index+ // (e.g. user changed the date in the file info panel), go to that new+ // index.+ //+ // - If the file is still the same but the underlying file's updation time+ // has changed (e.g. the user changed the name of the file in the file+ // info panel), refresh the annotated file data cached by the viewer.//- // If the active annotated file is no longer being shown, move to the next- // slide if possible, or close the viewer otherwise.useEffect(() => {- setActiveAnnotatedFile((af) => {- // Do nothing if we're not showing a file (we might not be open).- if (!af) return af;-- if (!files.length) {- // If there are no more files left, close the viewer.- handleClose();- return af;- }-- const updatedFile = files.find(({ id }) => id == af.file.id);- if (!updatedFile) {- // The file we were displaying is no longer part of the list of- // files that should be displayed. Refresh the slides, adjusting- // the indexes as necessary.- //- // A special case is when we might've been the last slide, in- // which case we need to go back one slide first. To determine- // if this is needed, we also pass the expected count of files- // to our PhotoSwipe wrapper.- psRef.current?.refreshCurrentSlideContentAfterRemove(- files.length,- );- return af;- }-- // There was an update to the files array, something that might've- // also potentially modified the file we're currently viewing, and- // we also found a (possibly) file with the same ID in the- // (possibly) updated files array.- //- // > Possibly because these are not deep equality comparisons, and- // > we might get here when the array's identity has changed but not- // > necessarily its contents.- //- // We could always refresh the current slide when we get here,- // however we want to avoid refreshing the entire UI unnecessarily- // lest the user lose their zoom/pan etc.- //- // So instead we only modify the file property of the active- // annotated file.- //- // This is not correct in its full generality, but it works fine in- // the specific cases we need to handle where the file gets updated- // from within the file viewer:- //- // - In case of deleting a file, or toggling its archived status to- // something that that caused the file to no longer part of the- // list that is shown, we'll not get to this code branch.- //- // - In case of toggling archive otherwise, just updating the file- // attribute is enough, the UI state is derived from it; none of- // the other attributes of the annotated file currently depend on- // the archive status change.- //- // - Similar reasoning applies to updates to the file's file name or- // date in the file info panel.- //- // - For updates to the file's caption in the file info panel we- // already need special casing (see `updateItemDataAlt`) to reload- // the slide since the PhotoSwipe UI element that displays the- // caption needs to be reloaded. So we'll come here in the caption- // update case too, but this code then isn't strictly necessary.- return { ...af, file: updatedFile };- });+ if (!files.length) {+ // If there are no more files left, close the viewer.+ handleClose();+ } else {+ psRef.current?.refreshSlideOnFilesUpdateIfNeeded();+ }}, [handleClose, files]);useEffect(() => {diff --git a/web/packages/gallery/components/viewer/photoswipe.ts b/web/packages/gallery/components/viewer/photoswipe.tsindex 7aecab6c89..af7ab4ff5b 100644--- a/web/packages/gallery/components/viewer/photoswipe.ts+++ b/web/packages/gallery/components/viewer/photoswipe.ts@@ -286,7 +286,11 @@ export class FileViewerPhotoSwipe {const currentAnnotatedFile = () => {const file = currentFile();let annotatedFile = _currentAnnotatedFile;- if (!annotatedFile || annotatedFile.file.id != file.id) {+ if (+ !annotatedFile ||+ annotatedFile.file.id != file.id ||+ annotatedFile.file.updationTime != file.updationTime+ ) {annotatedFile = onAnnotate(file, currSlideData());_currentAnnotatedFile = annotatedFile;}@@ -295,6 +299,41 @@ export class FileViewerPhotoSwipe {const currentFileAnnotation = () => currentAnnotatedFile().annotation;+ this.refreshSlideOnFilesUpdateIfNeeded = () => {+ // This function is called when the list of underlying files changes+ // (possibly spuriously, since the list changes on identity and not+ // deep equality). In response, it performs the sequence described+ // in [Note: Updates to the files prop for FileViewer].+ //+ // The intent is to minimize the need to call `refreshSlideContent`+ // since that would cause the zoom and pan state to be reset too.++ const af = _currentAnnotatedFile;+ if (!af) return;++ const files = delegate.getFiles();+ const ci = pswp.currIndex;+ console.log("goToSlideForFileIDIfNeeded", af, files, ci);++ const cf = files[ci];+ if (!cf || cf.id != af.file.id) {+ const newIndex = files.findIndex(({ id }) => id == af.file.id);+ if (newIndex == -1) {+ console.log("refreshCurrentSlideContentAfterRemove");+ this.refreshCurrentSlideContentAfterRemove(files.length);+ } else {+ console.log("going to slide", newIndex);+ _currentAnnotatedFile = undefined;+ pswp.goTo(newIndex);+ pswp.refreshSlideContent(newIndex);+ }+ } else {+ console.log("calling currentAnnotatedFile");+ // Refresh the current annotated file if needed.+ currentAnnotatedFile();+ }+ };+/*** File (ID)s for which we should render the original, non-streamable,* video even if a HLS playlist is available.@@ -1547,7 +1586,7 @@ export class FileViewerPhotoSwipe {* @param expectedFileCount The count of files that we expect to show after* the refresh.*/- refreshCurrentSlideContentAfterRemove(newFileCount: number) {+ private refreshCurrentSlideContentAfterRemove(newFileCount: number) {// Refresh the slide, and its subsequent neighbour.//// To see why, consider item at index 3 was removed. After refreshing,@@ -1571,6 +1610,12 @@ export class FileViewerPhotoSwipe {refreshSlideAndNextNeighbour(this.pswp.currIndex);}+ /**+ * Ask the delegate for the files information afresh, and perform the+ * sequence described in [Note: Updates to the files prop for FileViewer].+ */+ refreshSlideOnFilesUpdateIfNeeded: () => void;+/*** Refresh the favorite button (if indeed it is visible at all) on the* current slide, asking the delegate for the latest state.