Преглед изворни кода

Refactor preloader and debugging logic: replace `Trace` with `DebugHelper.LogDebug`, rename `_showAddRemove`, clean up redundant `#if DEBUG` blocks, simplify `SetStats` method, and improve preloading task handling in `ImageIterator`.

Ruben пре 4 месеци
родитељ
комит
cc842cbfbb

+ 3 - 3
src/PicView.Avalonia/ColorManagement/ColorManager.cs

@@ -92,9 +92,9 @@ public static class ColorManager
         [(int)ColorOptions.Emerald] = new ThemeColors(
             logoLight: Color.FromRgb(255, 253, 42),
             logoDark: Color.FromRgb(255, 253, 42),
-            primary: Color.FromRgb(0, 114, 0), // Changed from (32, 231, 107) to #007200
-            secondary: Color.FromArgb(242, 50, 164, 50), // Updated secondary color to match new primary
-            buttonColor: Color.FromRgb(0, 114, 0) // Button color matches primary
+            primary: Color.FromRgb(0, 114, 0), 
+            secondary: Color.FromArgb(242, 50, 164, 50), 
+            buttonColor: Color.FromRgb(0, 114, 0) 
         )
     };
 

+ 4 - 2
src/PicView.Avalonia/Navigation/ImageIterator.cs

@@ -757,8 +757,10 @@ public class ImageIterator : IAsyncDisposable
                         DispatcherPriority.Render);
                 }
 
-                await PreLoader.PreLoadAsync(index, IsReversed, ImagePaths)
-                    .ConfigureAwait(false);
+                // We shouldn't wait for preloading to finish, since this should complete as soon as image changed. 
+                // Awaiting preloader will cause delay, in E.G., moving the cursor after the image has changed.
+                _ = Task.Run(() => PreLoader.PreLoadAsync(index, IsReversed, ImagePaths)
+                    .ConfigureAwait(false));
             }
 
             PreLoader.Add(index, ImagePaths, preloadValue?.ImageModel);

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

@@ -128,7 +128,7 @@ public static class NavigationManager
             });
 
             TitleManager.SetSideBySideTitle(vm, imageModel, nextImageModel);
-            UpdateImage.SetStats(vm, index, imageModel);
+            UpdateImage.SetStats(vm, imageModel);
 
             // Fixes incorrect rendering in the side by side view
             // TODO: Improve and fix side by side and remove this hack 
@@ -149,7 +149,7 @@ public static class NavigationManager
                     TitleManager.SetTitle(vm, imageModel);
                 }
 
-                UpdateImage.SetStats(vm, index, imageModel);
+                UpdateImage.SetStats(vm, imageModel);
             }
         }
 

+ 6 - 4
src/PicView.Avalonia/Navigation/UpdateImage.cs

@@ -12,6 +12,8 @@ using PicView.Core.Gallery;
 using PicView.Core.ImageDecoding;
 using PicView.Core.Titles;
 
+// ReSharper disable RedundantAlwaysMatchSubpattern
+
 namespace PicView.Avalonia.Navigation;
 
 public static class UpdateImage
@@ -159,7 +161,7 @@ public static class UpdateImage
             }
         }
         
-        SetStats(vm, index, preLoadValue.ImageModel);
+        SetStats(vm, preLoadValue.ImageModel);
         
         return;
 
@@ -219,7 +221,7 @@ public static class UpdateImage
             PixelWidth = width,
             PixelHeight = height
         };
-        SetStats(vm, index, imageModel);
+        SetStats(vm, imageModel);
     }
 
     #endregion
@@ -297,12 +299,12 @@ public static class UpdateImage
 
     #region Set stats
 
-    public static void SetStats(MainViewModel vm, int index, ImageModel imageModel)
+    public static void SetStats(MainViewModel vm, ImageModel imageModel)
     {
         vm.IsSingleImage = false;
         vm.PicViewer.PixelWidth = imageModel.PixelWidth;
         vm.PicViewer.PixelHeight = imageModel.PixelHeight;
-        vm.GetIndex = index + 1;
+        vm.GetIndex = NavigationManager.GetNonZeroIndex;
         vm.PicViewer.ExifOrientation = imageModel.EXIFOrientation;
         vm.PicViewer.FileInfo = imageModel.FileInfo;
         vm.ZoomValue = 1;

+ 25 - 57
src/PicView.Avalonia/Preloading/Preloader.cs

@@ -2,6 +2,7 @@
 using System.Diagnostics;
 using Avalonia.Media.Imaging;
 using PicView.Avalonia.ImageHandling;
+using PicView.Core.DebugTools;
 using static System.GC;
 
 namespace PicView.Avalonia.Preloading;
@@ -12,9 +13,9 @@ namespace PicView.Avalonia.Preloading;
 public class PreLoader : IAsyncDisposable
 {
 #if DEBUG
-
     // ReSharper disable once ConvertToConstant.Local
-    private static readonly bool ShowAddRemove = false;
+    // ReSharper disable once FieldCanBeMadeReadOnly.Local
+    private static bool _showAddRemove = true;
 #endif
 
     /// <summary>
@@ -72,7 +73,7 @@ public class PreLoader : IAsyncDisposable
             imageModel = await GetImageModel.GetImageModelAsync(fileInfo).ConfigureAwait(false);
             preLoadValue.ImageModel = imageModel;
 #if DEBUG
-            if (ShowAddRemove)
+            if (_showAddRemove)
             {
                 Trace.WriteLine($"{imageModel?.FileInfo?.Name} added at {index}");
             }
@@ -81,9 +82,8 @@ public class PreLoader : IAsyncDisposable
         }
         catch (Exception ex)
         {
-#if DEBUG
-            Trace.WriteLine($"{nameof(AddAsync)} exception: \n{ex}");
-#endif
+            _preLoadList.TryRemove(index, out _); // Remove failed entry
+            DebugHelper.LogDebug(nameof(PreLoader), nameof(AddAsync), ex);
             return false;
         }
         finally
@@ -104,9 +104,7 @@ public class PreLoader : IAsyncDisposable
 
         if (imageModel is null)
         {
-#if DEBUG
-            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(Add)} invalid ImageModel");
-#endif
+            DebugHelper.LogDebug(nameof(PreLoader), nameof(AddAsync), "ImageModel is null");
             return false;
         }
 
@@ -122,26 +120,20 @@ public class PreLoader : IAsyncDisposable
     {
         if (list == null)
         {
-#if DEBUG
-            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(RefreshFileInfo)} list null \n{index}");
-#endif
+            DebugHelper.LogDebug(nameof(PreLoader), nameof(RefreshFileInfo), "list null, index: " + index);
             return;
         }
 
         if (index < 0 || index >= list.Count)
         {
-#if DEBUG
-            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(RefreshFileInfo)} invalid index: \n{index}");
-#endif
+            DebugHelper.LogDebug(nameof(PreLoader), nameof(RefreshFileInfo), "invalid index: " + index);
             return;
         }
 
         var isExisting = _preLoadList.TryGetValue(index, out var value);
         if (!isExisting)
         {
-#if DEBUG
-            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(RefreshFileInfo)} index not found: \n{index}");
-#endif
+            DebugHelper.LogDebug(nameof(PreLoader), nameof(RefreshFileInfo), "index not found: " + index);
             return;
         }
 
@@ -159,9 +151,7 @@ public class PreLoader : IAsyncDisposable
     {
         if (list == null)
         {
-#if DEBUG
-            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(Resynchronize)} list is null");
-#endif
+            DebugHelper.LogDebug(nameof(PreLoader), nameof(Resynchronize), "list is null");
             return;
         }
         
@@ -222,14 +212,14 @@ public class PreLoader : IAsyncDisposable
                 if (!_preLoadList.TryAdd(newIndex, removedValue))
                 {
 #if DEBUG
-                    if (ShowAddRemove)
+                    if (_showAddRemove)
                     {
                         Trace.WriteLine($"Failed to resynchronize {filePath} to index {newIndex}");
                     }
 #endif
                 }
 #if DEBUG
-                else if (ShowAddRemove)
+                else if (_showAddRemove)
                 {
                     Trace.WriteLine($"Resynchronized {filePath} from index {oldIndex} to {newIndex}");
                 }
@@ -254,9 +244,7 @@ public class PreLoader : IAsyncDisposable
         {
             return Contains(key, list) ? _preLoadList[key] : null;
         }
-#if DEBUG
-        Trace.WriteLine($"{nameof(PreLoader)}.{nameof(Get)} invalid parameters: \n{key}");
-#endif
+        DebugHelper.LogDebug(nameof(PreLoader), nameof(Get), "invalid parameters:" + key);
         return null;
     }
 
@@ -288,9 +276,7 @@ public class PreLoader : IAsyncDisposable
     {
         if (list == null || key < 0 || key >= list.Count)
         {
-#if DEBUG
-            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(GetOrLoadAsync)} invalid parameters: \n{key}");
-#endif
+            DebugHelper.LogDebug(nameof(PreLoader), nameof(GetOrLoadAsync), "invalid parameters: " + key);
             return null;
         }
 
@@ -323,18 +309,8 @@ public class PreLoader : IAsyncDisposable
     /// <param name="key">The key to check.</param>
     /// <param name="list">The list of image paths.</param>
     /// <returns>True if the key exists; otherwise, false.</returns>
-    public bool Contains(int key, List<string> list)
-    {
-        return list != null && key >= 0 && key < list.Count && _preLoadList.ContainsKey(key);
-    }
-
-    /// <summary>
-    ///     Checks if an image with the specified file name exists in the preload list.
-    /// </summary>
-    /// <param name="fileName">The full path of the image file to check for existence.</param>
-    /// <returns>True if the image exists in the preload list; otherwise, false.</returns>
-    public bool Contains(string fileName) =>
-        _preLoadList.Values.ToList().FindIndex(x => x.ImageModel.FileInfo.FullName == fileName) != -1;
+    public bool Contains(int key, List<string> list) =>
+        list != null && key >= 0 && key < list.Count && _preLoadList.ContainsKey(key);
 
     #endregion
 
@@ -350,17 +326,13 @@ public class PreLoader : IAsyncDisposable
     {
         if (list == null || key < 0 || key >= list.Count)
         {
-#if DEBUG
-            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(Remove)} invalid parameters: \n{key}");
-#endif
+            DebugHelper.LogDebug(nameof(PreLoader), nameof(Remove), "invalid parameters: " + key);
             return false;
         }
 
         if (!Contains(key, list))
         {
-#if DEBUG
-            Trace.WriteLine($"{nameof(PreLoader)}.{nameof(Remove)} key does not exist: \n{key}");
-#endif
+            DebugHelper.LogDebug(nameof(PreLoader), nameof(Remove), "key does not exist: " + key);
             return false;
         }
 
@@ -384,7 +356,7 @@ public class PreLoader : IAsyncDisposable
 
             var removed = _preLoadList.TryRemove(key, out _);
 #if DEBUG
-            if (removed && ShowAddRemove)
+            if (removed && _showAddRemove)
             {
                 Trace.WriteLine($"{Path.GetFileName(list[key])} removed at {list.IndexOf(list[key])}");
             }
@@ -393,9 +365,7 @@ public class PreLoader : IAsyncDisposable
         }
         catch (Exception e)
         {
-#if DEBUG
-            Trace.WriteLine($"{nameof(Remove)} exception:\n{e.Message}");
-#endif
+            DebugHelper.LogDebug(nameof(PreLoader), nameof(Remove), e);
         }
 
         return false;
@@ -435,7 +405,7 @@ public class PreLoader : IAsyncDisposable
         _preLoadList.Clear();
         
 #if DEBUG
-        if (ShowAddRemove)
+        if (_showAddRemove)
         {
             Trace.WriteLine("Preloader cleared");
         }
@@ -500,7 +470,7 @@ public class PreLoader : IAsyncDisposable
         }
 
 #if DEBUG
-        if (ShowAddRemove)
+        if (_showAddRemove)
         {
             Trace.WriteLine($"\nPreLoading started at {currentIndex}\n");
         }
@@ -525,9 +495,7 @@ public class PreLoader : IAsyncDisposable
         }
         catch (Exception exception)
         {
-#if DEBUG
-            Trace.WriteLine($"{nameof(PreLoadAsync)} exception:\n{exception.Message}");
-#endif
+            DebugHelper.LogDebug(nameof(PreLoader), nameof(PreLoadAsync), exception);
         }
         finally
         {
@@ -641,7 +609,7 @@ public class PreLoader : IAsyncDisposable
         _disposed = true;
         
 #if DEBUG
-        if (ShowAddRemove)
+        if (_showAddRemove)
         {
             Trace.WriteLine("Preloader disposed");
         }

+ 4 - 0
src/PicView.Core/Navigation/FileTypeResolver.cs

@@ -3,6 +3,10 @@ using PicView.Core.ImageDecoding;
 
 namespace PicView.Core.Navigation;
 
+/// <summary>
+/// Provides methods for determining the type of file or resource from a given input string
+/// and encapsulates logic to classify the resource as a loadable entity.
+/// </summary>
 public static class FileTypeResolver
 {
     /// <summary>