Browse Source

Refactor/bug fixes, misc

Ruben 7 months ago
parent
commit
7276414562

+ 7 - 23
src/PicView.Avalonia/Navigation/FileHistoryNavigation.cs

@@ -93,12 +93,11 @@ public static class FileHistoryNavigation
     {
         if (!NavigationManager.CanNavigate(vm))
         {
-            await OpenLastFileAsync(vm).ConfigureAwait(false);
-            return;
-        }
-        
-        if (!NavigationManager.CanNavigate(vm))
-        {
+            var lastFile = Path.GetFileNameWithoutExtension(GetLastFile());
+            if (lastFile == Path.GetFileNameWithoutExtension(vm.FileInfo.Name))
+            {
+                return;
+            }
             await OpenLastFileAsync(vm).ConfigureAwait(false);
             return;
         }
@@ -108,36 +107,21 @@ public static class FileHistoryNavigation
 
     public static async Task LoadEntryAsync(MainViewModel vm, int index, bool next)
     {
-        var imagePaths = NavigationManager.GetCollection;
-
         _fileHistory ??= new FileHistory();
         string? nextEntry;
         if (next)
         {
-            nextEntry = await Task.FromResult(_fileHistory.GetNextEntry(Settings.UIProperties.Looping, index, imagePaths)).ConfigureAwait(false);
+            nextEntry = await Task.FromResult(_fileHistory.GetNextEntry(Settings.UIProperties.Looping, index)).ConfigureAwait(false);
         }
         else
         {
-            nextEntry = await Task.FromResult(_fileHistory.GetPreviousEntry(Settings.UIProperties.Looping, index, imagePaths)).ConfigureAwait(false);
+            nextEntry = await Task.FromResult(_fileHistory.GetPreviousEntry(Settings.UIProperties.Looping, index)).ConfigureAwait(false);
         }
 
         if (string.IsNullOrWhiteSpace(nextEntry))
         {
             return;
         }
-
-        if (imagePaths.Contains(nextEntry))
-        {
-            if (nextEntry == imagePaths[index])
-            {
-                return;
-            }
-
-            vm.CurrentView = vm.ImageViewer;
-
-            await NavigationManager.Navigate(imagePaths.IndexOf(nextEntry), vm).ConfigureAwait(false);
-            return;
-        }
         await NavigationManager.LoadPicFromStringAsync(nextEntry, vm);
     }
     

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

@@ -438,8 +438,7 @@ public static class NavigationManager
                 }
                 else
                 {
-                    await _imageIterator.DisposeAsync();
-                    await ErrorHandling.ReloadAsync(vm);
+                    await LoadWithoutImageIterator(fileInfo, vm);
                 }
             }
             else
@@ -576,11 +575,14 @@ public static class NavigationManager
 
         var imageModel = await GetImageModel.GetImageModelAsync(fileInfo).ConfigureAwait(false);
         await UpdateImage.SetSingleImageAsync(imageModel.Image, imageModel.ImageType, url, vm);
+
+        vm.IsLoading = false;
         vm.FileInfo = fileInfo;
         vm.ExifOrientation = imageModel.EXIFOrientation;
         FileHistoryNavigation.Add(url);
 
-        vm.IsLoading = false;
+        await DisposeImageIteratorAsync();
+        
     }
 
     /// <summary>
@@ -720,11 +722,13 @@ public static class NavigationManager
         {
             return;
         }
+        await _imageIterator.ClearAsync();
+        _imageIterator.ImagePaths.Clear();
         await _imageIterator.DisposeAsync();
     }
     
     public static bool IsCollectionEmpty => _imageIterator.ImagePaths is null || _imageIterator.ImagePaths.Count < 0;
-    public static List<string> GetCollection => _imageIterator.ImagePaths;
+    public static List<string>? GetCollection => _imageIterator?.ImagePaths;
     
     public static void UpdateFileListAndIndex(List<string> fileList, int index) => _imageIterator?.UpdateFileListAndIndex(fileList, index);
     

+ 7 - 19
src/PicView.Avalonia/Navigation/UpdateImage.cs

@@ -206,16 +206,7 @@ public static class UpdateImage
     /// <param name="vm">The main view model instance.</param>
     public static void SetSingleImage(object source, ImageType imageType, string name, MainViewModel vm)
     {
-        ExecuteSetSingleImageAsync(
-            source,
-            imageType,
-            name,
-            vm,
-            (action, priority) =>
-            {
-                Dispatcher.UIThread.Invoke(action, priority);
-                return Task.CompletedTask;
-            }).GetAwaiter().GetResult();
+        SetSingleImageAsync(source, imageType, name, vm).GetAwaiter().GetResult();
     }
 
     /// <inheritdoc cref="SetSingleImage" />
@@ -227,7 +218,6 @@ public static class UpdateImage
             name,
             vm,
             async (action, priority) => { await Dispatcher.UIThread.InvokeAsync(action, priority); });
-        await NavigationManager.DisposeImageIteratorAsync();
     }
 
     /// <summary>
@@ -275,15 +265,9 @@ public static class UpdateImage
             height = bitmap?.PixelSize.Height ?? 0;
         }
 
-        if (GalleryFunctions.IsBottomGalleryOpen)
-        {
-            await dispatchAction(() => { vm.GalleryMode = GalleryMode.BottomToClosed; }, DispatcherPriority.Render);
-            vm.GalleryMode = GalleryMode.Closed;
-        }
-
         vm.FileInfo = null;
 
-        await dispatchAction(() => { WindowResizing.SetSize(width, height, 0, 0, 0, vm); }, DispatcherPriority.Render);
+        await dispatchAction(() => { WindowResizing.SetSize(width, height, 0, 0, 0, vm); }, DispatcherPriority.Send);
 
         var singeImageWindowTitles = ImageTitleFormatter.GenerateTitleForSingleImage(width, height, name, 1);
         vm.WindowTitle = singeImageWindowTitles.TitleWithAppName;
@@ -295,7 +279,11 @@ public static class UpdateImage
         vm.PixelWidth = width;
         vm.PixelHeight = height;
 
-        vm.GalleryMargin = new Thickness(0);
+        if (Settings.Gallery.IsBottomGalleryShown)
+        {
+            vm.GalleryMode = GalleryMode.Closed;
+            vm.GalleryMargin = new Thickness(0);
+        }
 
         await dispatchAction(() => { UIHelper.GetGalleryView.IsVisible = false; }, DispatcherPriority.Render);
         await NavigationManager.DisposeImageIteratorAsync();

+ 1 - 1
src/PicView.Avalonia/Views/MainView.axaml.cs

@@ -142,7 +142,7 @@ public partial class MainView : UserControl
 #endif
             var secondaryAccentBrush = (SolidColorBrush)secondaryAccentColor;
             var fileLocation = FileHistoryNavigation.GetFileLocation(index);
-            var selected = NavigationManager.GetCurrentIndex == NavigationManager.GetCollection.IndexOf(fileLocation);
+            var selected = NavigationManager.GetCurrentIndex == NavigationManager.GetCollection?.IndexOf(fileLocation);
             var header = Path.GetFileNameWithoutExtension(fileLocation);
             header = header.Length > 60 ? header.Shorten(60) : header;
         

+ 6 - 0
src/PicView.Core/Gallery/GalleryMode.cs

@@ -10,6 +10,12 @@
         BottomToClosed,
         ClosedToFull,
         ClosedToBottom,
+        /// <summary>
+        /// Used when the gallery should be closed
+        /// </summary>
         Closed,
+        /// <summary>
+        /// Used when the gallery should be opened, with no animation. 
+        /// </summary>
         BottomNoAnimation
     }

+ 8 - 22
src/PicView.Core/Navigation/FileHistory.cs

@@ -196,24 +196,17 @@ public class FileHistory
 
         return index >= _fileHistory.Count ? _fileHistory[^1] : _fileHistory[index];
     }
-
-    /// <summary>
-    /// Gets the next file entry based on the current index and list of file names.
-    /// </summary>
-    /// <param name="looping">Whether to loop to the beginning when reaching the end.</param>
-    /// <param name="index">The current index in the list.</param>
-    /// <param name="list">The list of file names.</param>
-    /// <returns>The next file entry or null if not found or an exception occurs.</returns>
-    public string? GetNextEntry(bool looping, int index, List<string> list)
+    
+    public string? GetNextEntry(bool looping, int index)
     {
-        if (list.Count <= 0)
+        if (_fileHistory.Count <= 0)
         {
             return GetLastFile();
         }
 
         try
         {
-            var foundIndex = _fileHistory.IndexOf(list[index]);
+            var foundIndex = _fileHistory.IndexOf(_fileHistory[index]);
 
             if (looping)
             {
@@ -231,24 +224,17 @@ public class FileHistory
             return null;
         }
     }
-
-    /// <summary>
-    /// Gets the previous file entry based on the current index and list of file names.
-    /// </summary>
-    /// <param name="looping">Whether to loop to the end when reaching the beginning.</param>
-    /// <param name="index">The current index in the list.</param>
-    /// <param name="list">The list of file names.</param>
-    /// <returns>The previous file entry or null if not found or an exception occurs.</returns>
-    public string? GetPreviousEntry(bool looping, int index, List<string> list)
+    
+    public string? GetPreviousEntry(bool looping, int index)
     {
-        if (list.Count <= 0)
+        if (_fileHistory.Count <= 0)
         {
             return GetFirstFile();
         }
 
         try
         {
-            var foundIndex = _fileHistory.IndexOf(list[index]);
+            var foundIndex = _fileHistory.IndexOf(_fileHistory[index]);
             if (looping)
             {
                 return GetEntryAt((foundIndex - 1 + _fileHistory.Count) % _fileHistory.Count);

+ 2 - 2
src/PicView.Tests/FileHistoryTest.cs

@@ -39,11 +39,11 @@ public class FileHistoryTest
         var entry = history.GetEntryAt(1);
         Assert.NotNull(entry);
 
-        var nextEntry = history.GetNextEntry(looping: true, 2, list);
+        var nextEntry = history.GetNextEntry(looping: true, 2);
         Assert.NotNull(nextEntry);
         Assert.True(File.Exists(nextEntry));
 
-        var prevEntry = history.GetPreviousEntry(looping: true, 2, list);
+        var prevEntry = history.GetPreviousEntry(looping: true, 2);
         Assert.NotNull(prevEntry);
         Assert.True(File.Exists(prevEntry));