Browse Source

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 tháng trước cách đây
mục cha
commit
09f348d313

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

@@ -195,7 +195,7 @@ public static class DragAndDropHelper
         }
         }
         else if (path.IsSupported())
         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);
         var ext = Path.GetExtension(path);
         if (ext.Equals(".svg", StringComparison.InvariantCultureIgnoreCase) ||
         if (ext.Equals(".svg", StringComparison.InvariantCultureIgnoreCase) ||
@@ -238,22 +238,23 @@ public static class DragAndDropHelper
     {
     {
         Bitmap? thumb;
         Bitmap? thumb;
         // Try to get preloaded image first
         // Try to get preloaded image first
-        var preload = NavigationManager.GetPreLoadValue(path);
+        var preload = NavigationManager.TryGetPreLoadValue(path);
         if (preload?.ImageModel?.Image is Bitmap bmp)
         if (preload?.ImageModel?.Image is Bitmap bmp)
         {
         {
             thumb = bmp;
             thumb = bmp;
+            
+            await UpdateThumbnailUI(thumb);
         }
         }
         else
         else
         {
         {
             // Generate thumbnail
             // Generate thumbnail
             thumb = await GetThumbnails.GetThumbAsync(path, SizeDefaults.WindowMinSize - 30)
             thumb = await GetThumbnails.GetThumbAsync(path, SizeDefaults.WindowMinSize - 30)
                 .ConfigureAwait(false);
                 .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)
     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() =>
     public PreLoadValue? GetCurrentPreLoadValue() =>
         isRunning ? PreLoader.Get(_vm.PicViewer.FileInfo.FullName, ImagePaths) : PreLoader.Get(CurrentIndex, ImagePaths);
         isRunning ? PreLoader.Get(_vm.PicViewer.FileInfo.FullName, ImagePaths) : PreLoader.Get(CurrentIndex, ImagePaths);
 
 
     public async Task<PreLoadValue?> GetCurrentPreLoadValueAsync() =>
     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()
     public PreLoadValue? GetNextPreLoadValue()
     {
     {
@@ -425,7 +425,7 @@ public class ImageIterator : IAsyncDisposable
     public async Task<PreLoadValue?>? GetNextPreLoadValueAsync()
     public async Task<PreLoadValue?>? GetNextPreLoadValueAsync()
     {
     {
         var nextIndex = GetIteration(CurrentIndex, NavigateTo.Next);
         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);
     public void RemoveItemFromPreLoader(int index) => PreLoader.Remove(index, ImagePaths);
@@ -671,7 +671,7 @@ public class ImageIterator : IAsyncDisposable
             if (Settings.ImageScaling.ShowImageSideBySide)
             if (Settings.ImageScaling.ShowImageSideBySide)
             {
             {
                 var nextIndex = GetIteration(index, IsReversed ? NavigateTo.Previous : NavigateTo.Next);
                 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)
                 if (CurrentIndex != index)
                 {
                 {
                     // Skip loading if user went to next value
                     // 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 FileInfo? GetInitialFileInfo => ImageIterator?.InitialFileInfo;
     
     
-    public static PreLoadValue? GetPreLoadValue(int index) => 
+    public static PreLoadValue? TryGetPreLoadValue(int index) => 
         ImageIterator?.GetPreLoadValue(index) ?? null;
         ImageIterator?.GetPreLoadValue(index) ?? null;
-    public static PreLoadValue? GetPreLoadValue(string fileName) => 
+    public static PreLoadValue? TryGetPreLoadValue(string fileName) => 
         ImageIterator?.GetPreLoadValue(fileName) ?? null;
         ImageIterator?.GetPreLoadValue(fileName) ?? null;
     public static async Task<PreLoadValue?> GetPreLoadValueAsync(int index) => 
     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) => 
     public static async Task<PreLoadValue?> GetPreLoadValueAsync(string fileName) => 
-        await ImageIterator?.GetPreLoadValueAsync(GetFileNameIndex(fileName)) ?? null;
+        await ImageIterator?.GetOrLoadPreLoadValueAsync(GetFileNameIndex(fileName)) ?? null;
     public static PreLoadValue? GetCurrentPreLoadValue() => 
     public static PreLoadValue? GetCurrentPreLoadValue() => 
         ImageIterator?.GetCurrentPreLoadValue() ?? null;
         ImageIterator?.GetCurrentPreLoadValue() ?? null;
     public static async Task<PreLoadValue?> GetCurrentPreLoadValueAsync() =>
     public static async Task<PreLoadValue?> GetCurrentPreLoadValueAsync() =>

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

@@ -279,17 +279,17 @@ public class PreLoader : IAsyncDisposable
 
 
 
 
     /// <summary>
     /// <summary>
-    ///     Gets the preloaded value for a specific key asynchronously.
+    /// Retrieves a preloaded image value or loads it asynchronously if not already loaded.
     /// </summary>
     /// </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>
     /// <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 (list == null || key < 0 || key >= list.Count)
         {
         {
 #if DEBUG
 #if DEBUG
-            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(GetAsync)} invalid parameters: \n{key}");
+            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(GetOrLoadAsync)} invalid parameters: \n{key}");
 #endif
 #endif
             return null;
             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="fileName">The full path of the image file to retrieve the preloaded value for.</param>
     /// <param name="list">The list of image paths.</param>
     /// <param name="list">The list of image paths.</param>
     /// <returns>The preloaded value if it exists; otherwise, null.</returns>
     /// <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);
             list);
 
 
     #endregion
     #endregion