瀏覽代碼

Improve file renaming and error checking

Ruben 1 年之前
父節點
當前提交
01a495d764

+ 4 - 6
src/PicView.WPF/ChangeImage/FileUpdateNavigation.cs

@@ -86,7 +86,7 @@ internal static class FileUpdateNavigation
         // Fix incorrect selection
         await UC.GetPicGallery.Dispatcher.InvokeAsync(() =>
         {
-            GalleryNavigation.SelectedGalleryItem = - 1;
+            GalleryNavigation.SelectedGalleryItem = -1;
             GalleryNavigation.SetSelected(GalleryNavigation.SelectedGalleryItem, false);
             GalleryNavigation.SetSelected(oldIndex, false);
         });
@@ -165,13 +165,11 @@ internal static class FileUpdateNavigation
         {
             return;
         }
-        var bitmapSource = !sameFile ? null : PreLoader.Get(Navigation.FolderIndex)?.BitmapSource;
-        PreLoader.Remove(index);
 
-        if (sameFile)
+        var clearedCache = await PreLoader.RefreshFileInfo(index).ConfigureAwait(false);
+        if (!clearedCache)
         {
-            Navigation.FolderIndex = index;
-            await PreLoader.AddAsync(Navigation.FolderIndex, fileInfo, bitmapSource).ConfigureAwait(false);
+            PreLoader.Remove(index);
         }
         await UpdateTitle(index);
         _running = false;

+ 20 - 15
src/PicView.WPF/ChangeImage/PreLoader.cs

@@ -148,23 +148,28 @@ internal static class PreLoader
     }
 
     /// <summary>
-    ///  Renames the key of a specified file in the cache.
+    /// Refreshes the file information for the specified index in the PreLoadList dictionary.
     /// </summary>
-    /// <param name="index">index of file to be renamed</param>
-    /// <returns></returns>
-    internal static void Rename(int index)
+    /// <param name="index">The index of the image.</param>
+    /// <returns>True if the file information was successfully refreshed, false otherwise.</returns>
+    internal static async Task<bool> RefreshFileInfo(int index)
     {
         if (index < 0 || index >= Pics.Count)
         {
 #if DEBUG
-            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(ReferenceEquals)} invalid index: \n{index}");
+            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(RefreshFileInfo)} invalid index: \n{index}");
 #endif
-            return;
+            return false;
+        }
+
+        var removed = PreLoadList.TryRemove(index, out var preLoadValue);
+        if (preLoadValue is not null)
+        {
+            preLoadValue.FileInfo = null;
         }
 
-        PreLoadList.TryRemove(index, out var preLoadValue);
-        preLoadValue.FileInfo = null;
-        _ = AddAsync(index, null, preLoadValue.BitmapSource, preLoadValue.Orientation).ConfigureAwait(true);
+        await AddAsync(index, null, preLoadValue?.BitmapSource, preLoadValue?.Orientation).ConfigureAwait(false);
+        return removed;
     }
 
     /// <summary>
@@ -283,13 +288,13 @@ internal static class PreLoader
         {
             if (Reverse)
             {
-                await NegativeLoop(cancellationTokenSource).ConfigureAwait(false);
-                await PositiveLoop(cancellationTokenSource).ConfigureAwait(false);
+                await NegativeLoop(cancellationTokenSource);
+                await PositiveLoop(cancellationTokenSource);
             }
             else
             {
-                await PositiveLoop(cancellationTokenSource).ConfigureAwait(false);
-                await NegativeLoop(cancellationTokenSource).ConfigureAwait(false);
+                await PositiveLoop(cancellationTokenSource);
+                await NegativeLoop(cancellationTokenSource);
             }
         }
         catch (Exception exception)
@@ -331,7 +336,7 @@ internal static class PreLoader
                     return;
                 }
                 var index = (nextStartingIndex + i) % Pics.Count;
-                await AddAsync(index).ConfigureAwait(false);
+                await AddAsync(index);
             });
         }
 
@@ -351,7 +356,7 @@ internal static class PreLoader
                     return;
                 }
                 var index = (prevStartingIndex - i + Pics.Count) % Pics.Count;
-                await AddAsync(index).ConfigureAwait(false);
+                await AddAsync(index);
             });
         }
     }

+ 6 - 3
src/PicView.WPF/FileHandling/FileFunctions.cs

@@ -57,8 +57,7 @@ internal static class FileFunctions
             IsFileBeingRenamed = false;
             return null;
         }
-        var bitmapsource = PreLoader.Get(oldIndex)?.BitmapSource;
-        PreLoader.Remove(oldIndex);
+
         var fileInfo = new FileInfo(newPath);
         if (fileInfo.Exists == false) { return null; }
 
@@ -73,10 +72,14 @@ internal static class FileFunctions
             return null;
         }
         IsFileBeingRenamed = false;
+        var clearedCache = await PreLoader.RefreshFileInfo(Navigation.FolderIndex);
+        if (!clearedCache)
+        {
+            PreLoader.Remove(Navigation.FolderIndex);
+        }
 
         await FileUpdateNavigation.UpdateTitle(Navigation.FolderIndex);
         await FileUpdateNavigation.UpdateGalleryAsync(Navigation.FolderIndex, oldIndex, fileInfo, oldPath, newPath, true).ConfigureAwait(false);
-        await PreLoader.AddAsync(Navigation.FolderIndex, fileInfo, bitmapsource).ConfigureAwait(false);
         await ImageInfo.UpdateValuesAsync(fileInfo).ConfigureAwait(false);
 
         FileHistoryNavigation.Rename(oldPath, newPath);

+ 5 - 1
src/PicView.WPF/Views/Windows/ImageInfoWindow.xaml.cs

@@ -155,7 +155,11 @@ public partial class ImageInfoWindow
         ShowInFolder.MouseEnter += (_, _) => AnimationHelper.MouseEnterBgTexColor(ShowInFolderBrush);
         ShowInFolder.MouseLeave += (_, _) => ButtonMouseLeaveAnim(ShowInFolderFill);
         ShowInFolder.MouseLeave += (_, _) => AnimationHelper.MouseLeaveBgTexColor(ShowInFolderBrush);
-        ShowInFolder.Click += (_, _) => OpenSave.OpenInExplorer(Pics[FolderIndex]);
+        ShowInFolder.Click += (_, _) =>
+        {
+            if (ErrorHandling.CheckOutOfRange()) return;
+            OpenSave.OpenInExplorer(Pics[FolderIndex]);
+        };
 
         // Optimize Image
         OptimizeImageButton.MouseEnter += (_, _) => ButtonMouseOverAnim(OptimizeImageFill);