Jelajahi Sumber

Refactor image preloading methods to improve clarity.

Replaced `GetAsync` methods with `GetOrLoadAsync` to better reflect their functionality of retrieving or asynchronously loading images. Renamed related methods and parameters for improved readability and consistency across the codebase. Adjusted UI update and background loading logic to simplify thumbnail handling.
Ruben 4 bulan lalu
induk
melakukan
09f348d313

+ 9 - 8
src/PicView.Avalonia/DragAndDrop/DragAndDropHelper.cs

@@ -195,7 +195,7 @@ public static class DragAndDropHelper
         }
         else if (path.IsSupported())
         {
-            await ShowFilePreview(path, control);
+            await ShowFilePreview(path);
         }
     }
 
@@ -221,7 +221,7 @@ public static class DragAndDropHelper
         });
     }
 
-    private static async Task ShowFilePreview(string path, Control control)
+    private static async Task ShowFilePreview(string path)
     {
         var ext = Path.GetExtension(path);
         if (ext.Equals(".svg", StringComparison.InvariantCultureIgnoreCase) ||
@@ -238,22 +238,23 @@ public static class DragAndDropHelper
     {
         Bitmap? thumb;
         // Try to get preloaded image first
-        var preload = NavigationManager.GetPreLoadValue(path);
+        var preload = NavigationManager.TryGetPreLoadValue(path);
         if (preload?.ImageModel?.Image is Bitmap bmp)
         {
             thumb = bmp;
+            
+            await UpdateThumbnailUI(thumb);
         }
         else
         {
             // Generate thumbnail
             thumb = await GetThumbnails.GetThumbAsync(path, SizeDefaults.WindowMinSize - 30)
                 .ConfigureAwait(false);
+            await UpdateThumbnailUI(thumb);
+            
+            // Load full image in background
+            await PreloadFullImage(path, preload, thumb);
         }
-
-        await UpdateThumbnailUI(thumb);
-
-        // Load full image in background
-        await PreloadFullImage(path, preload, thumb);
     }
 
     private static async Task PreloadFullImage(string path, PreLoadValue? preload, Bitmap? thumb)

+ 5 - 5
src/PicView.Avalonia/Navigation/ImageIterator.cs

@@ -407,14 +407,14 @@ public class ImageIterator : IAsyncDisposable
     }
         
     
-    public async Task<PreLoadValue?> GetPreLoadValueAsync(int index) =>
-        await PreLoader.GetAsync(index, ImagePaths);
+    public async Task<PreLoadValue?> GetOrLoadPreLoadValueAsync(int index) =>
+        await PreLoader.GetOrLoadAsync(index, ImagePaths);
 
     public PreLoadValue? GetCurrentPreLoadValue() =>
         isRunning ? PreLoader.Get(_vm.PicViewer.FileInfo.FullName, ImagePaths) : PreLoader.Get(CurrentIndex, ImagePaths);
 
     public async Task<PreLoadValue?> GetCurrentPreLoadValueAsync() =>
-        isRunning ? await PreLoader.GetAsync(_vm.PicViewer.FileInfo.FullName, ImagePaths) : await PreLoader.GetAsync(CurrentIndex, ImagePaths);
+        isRunning ? await PreLoader.GetOrLoadAsync(_vm.PicViewer.FileInfo.FullName, ImagePaths) : await PreLoader.GetOrLoadAsync(CurrentIndex, ImagePaths);
 
     public PreLoadValue? GetNextPreLoadValue()
     {
@@ -425,7 +425,7 @@ public class ImageIterator : IAsyncDisposable
     public async Task<PreLoadValue?>? GetNextPreLoadValueAsync()
     {
         var nextIndex = GetIteration(CurrentIndex, NavigateTo.Next);
-        return isRunning ? await PreLoader.GetAsync(ImagePaths[nextIndex], ImagePaths) : await PreLoader.GetAsync(nextIndex, ImagePaths);
+        return isRunning ? await PreLoader.GetOrLoadAsync(ImagePaths[nextIndex], ImagePaths) : await PreLoader.GetOrLoadAsync(nextIndex, ImagePaths);
     }
 
     public void RemoveItemFromPreLoader(int index) => PreLoader.Remove(index, ImagePaths);
@@ -671,7 +671,7 @@ public class ImageIterator : IAsyncDisposable
             if (Settings.ImageScaling.ShowImageSideBySide)
             {
                 var nextIndex = GetIteration(index, IsReversed ? NavigateTo.Previous : NavigateTo.Next);
-                var nextPreloadValue = await GetPreLoadValueAsync(nextIndex).ConfigureAwait(false);
+                var nextPreloadValue = await GetOrLoadPreLoadValueAsync(nextIndex).ConfigureAwait(false);
                 if (CurrentIndex != index)
                 {
                     // Skip loading if user went to next value

+ 4 - 4
src/PicView.Avalonia/Navigation/NavigationManager.cs

@@ -419,14 +419,14 @@ public static class NavigationManager
     
     public static FileInfo? GetInitialFileInfo => ImageIterator?.InitialFileInfo;
     
-    public static PreLoadValue? GetPreLoadValue(int index) => 
+    public static PreLoadValue? TryGetPreLoadValue(int index) => 
         ImageIterator?.GetPreLoadValue(index) ?? null;
-    public static PreLoadValue? GetPreLoadValue(string fileName) => 
+    public static PreLoadValue? TryGetPreLoadValue(string fileName) => 
         ImageIterator?.GetPreLoadValue(fileName) ?? null;
     public static async Task<PreLoadValue?> GetPreLoadValueAsync(int index) => 
-        await ImageIterator?.GetPreLoadValueAsync(index) ?? null;
+        await ImageIterator?.GetOrLoadPreLoadValueAsync(index) ?? null;
     public static async Task<PreLoadValue?> GetPreLoadValueAsync(string fileName) => 
-        await ImageIterator?.GetPreLoadValueAsync(GetFileNameIndex(fileName)) ?? null;
+        await ImageIterator?.GetOrLoadPreLoadValueAsync(GetFileNameIndex(fileName)) ?? null;
     public static PreLoadValue? GetCurrentPreLoadValue() => 
         ImageIterator?.GetCurrentPreLoadValue() ?? null;
     public static async Task<PreLoadValue?> GetCurrentPreLoadValueAsync() =>

+ 7 - 7
src/PicView.Avalonia/Preloading/Preloader.cs

@@ -279,17 +279,17 @@ public class PreLoader : IAsyncDisposable
 
 
     /// <summary>
-    ///     Gets the preloaded value for a specific key asynchronously.
+    /// Retrieves a preloaded image value or loads it asynchronously if not already loaded.
     /// </summary>
-    /// <param name="key">The key of the preloaded value.</param>
+    /// <param name="key">The index of the image in the list.</param>
     /// <param name="list">The list of image paths.</param>
-    /// <returns>The preloaded value if it exists; otherwise, null.</returns>
-    public async Task<PreLoadValue?> GetAsync(int key, List<string> list)
+    /// <returns>The preloaded image value if found or successfully loaded; otherwise, null.</returns>
+    public async Task<PreLoadValue?> GetOrLoadAsync(int key, List<string> list)
     {
         if (list == null || key < 0 || key >= list.Count)
         {
 #if DEBUG
-            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(GetAsync)} invalid parameters: \n{key}");
+            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(GetOrLoadAsync)} invalid parameters: \n{key}");
 #endif
             return null;
         }
@@ -309,8 +309,8 @@ public class PreLoader : IAsyncDisposable
     /// <param name="fileName">The full path of the image file to retrieve the preloaded value for.</param>
     /// <param name="list">The list of image paths.</param>
     /// <returns>The preloaded value if it exists; otherwise, null.</returns>
-    public async Task<PreLoadValue?> GetAsync(string fileName, List<string> list) =>
-        await GetAsync(_preLoadList.Values.ToList().FindIndex(x => x.ImageModel?.FileInfo?.FullName == fileName),
+    public async Task<PreLoadValue?> GetOrLoadAsync(string fileName, List<string> list) =>
+        await GetOrLoadAsync(_preLoadList.Values.ToList().FindIndex(x => x.ImageModel?.FileInfo?.FullName == fileName),
             list);
 
     #endregion