Procházet zdrojové kódy

File renaming and file duplication refactor and fixes

Ruben před 1 rokem
rodič
revize
4e0f330a44

+ 5 - 0
src/PicView.Avalonia/CustomControls/PicBox.cs

@@ -419,6 +419,11 @@ public class PicBox : Control
                 return new Size();
             }
 
+            if (!vm.FileInfo.Exists)
+            {
+                return new Size();
+            }
+
             using var magickImage = new MagickImage();
             magickImage.Ping(vm.FileInfo);
             return new Size(magickImage.Width, magickImage.Height);

+ 6 - 2
src/PicView.Avalonia/Gallery/GalleryFunctions.cs

@@ -146,6 +146,10 @@ public static class GalleryFunctions
 
         var galleryListBox = mainView.GalleryView.GalleryListBox;
         if (galleryListBox == null) return;
+        if (galleryListBox.Items.Count < 0)
+        {
+            return;
+        }
         var initialDirectory = Path.GetDirectoryName(vm.FileInfo.FullName);
         try
         {
@@ -186,8 +190,8 @@ public static class GalleryFunctions
                     return;
                 }
             }
-            
-            thumbs = thumbs.OrderBySequence(files, x => x.FileLocation).ToList();
+            await Dispatcher.UIThread.InvokeAsync(() =>
+                thumbs = thumbs.OrderBySequence(files, x => x.FileLocation).ToList());
             vm.SelectedGalleryItemIndex = -1;
 
             for (var i = 0; i < files.Count; i++)

+ 7 - 20
src/PicView.Avalonia/Navigation/ImageIterator.cs

@@ -21,8 +21,6 @@ public sealed class ImageIterator : IDisposable
 
     public List<string> ImagePaths { get; private set; }
 
-    public bool IsRenamingInProgress { get; set; }
-
     public int CurrentIndex { get; private set; }
 
     public FileInfo InitialFileInfo { get; private set; } = null!;
@@ -84,11 +82,6 @@ public sealed class ImageIterator : IDisposable
 
     private async Task OnFileAdded(FileSystemEventArgs e)
     {
-        if (IsRenamingInProgress)
-        {
-            return;
-        }
-
         if (ImagePaths.Contains(e.FullPath))
         {
             return;
@@ -188,11 +181,6 @@ public sealed class ImageIterator : IDisposable
 
     private async Task OnFileDeleted(FileSystemEventArgs e)
     {
-        if (IsRenamingInProgress)
-        {
-            return;
-        }
-
         if (e.FullPath.IsSupported() == false)
         {
             return;
@@ -290,11 +278,6 @@ public sealed class ImageIterator : IDisposable
 
     private async Task OnFileRenamed(RenamedEventArgs e)
     {
-        if (IsRenamingInProgress)
-        {
-            return;
-        }
-
         if (e.FullPath.IsSupported() == false)
         {
             if (ImagePaths.Contains(e.OldFullPath))
@@ -313,7 +296,7 @@ public sealed class ImageIterator : IDisposable
         _isRunning = true;
 
         var oldIndex = ImagePaths.IndexOf(e.OldFullPath);
-
+        var sameFile = CurrentIndex == oldIndex;
         var fileInfo = new FileInfo(e.FullPath);
         if (fileInfo.Exists == false)
         {
@@ -344,9 +327,13 @@ public sealed class ImageIterator : IDisposable
             return;
         }
 
-        SetTitleHelper.SetTitle(_vm);
-
         await PreLoader.RefreshFileInfo(oldIndex, ImagePaths);
+        if (sameFile)
+        {
+            _vm.FileInfo = fileInfo;
+        }
+        
+        SetTitleHelper.SetTitle(_vm);
 
         _isRunning = false;
         FileHistoryNavigation.Rename(e.OldFullPath, e.FullPath);

+ 5 - 1
src/PicView.Avalonia/Navigation/NavigationHelper.cs

@@ -254,7 +254,11 @@ public static class NavigationHelper
                 if (index != -1)
                 {
                    await vm.ImageIterator.IterateToIndex(index);
-                   return;
+                }
+                else
+                {
+                    await ErrorHandling.ReloadAsync(vm);
+                    return;
                 }
             }
         }

+ 0 - 4
src/PicView.Avalonia/UI/FunctionsHelper.cs

@@ -648,10 +648,6 @@ public static class FunctionsHelper
 
     public static async Task DuplicateFile()
     {
-        if (Vm is null)
-        {
-            return;
-        }
         if (!NavigationHelper.CanNavigate(Vm))
         {
             return;

+ 3 - 11
src/PicView.Avalonia/ViewModels/MainViewModel.cs

@@ -1376,21 +1376,11 @@ public class MainViewModel : ViewModelBase
             return;
         }
 
-        if (ImageIterator is not null)
-        {
-            ImageIterator.IsRenamingInProgress = true;
-        }
-
         var newPath = await ConversionHelper.ConvertTask(FileInfo, index);
         if (!string.IsNullOrWhiteSpace(newPath))
         {
             await NavigationHelper.LoadPicFromStringAsync(newPath, this);
         }
-
-        if (ImageIterator is not null)
-        {
-            ImageIterator.IsRenamingInProgress = false;
-        }
     }
     
     private async Task CopyFileTask(string path)
@@ -1472,7 +1462,8 @@ public class MainViewModel : ViewModelBase
             return;
         }
 
-        if (Path.GetFileName(path) == FileInfo.FullName)
+        IsLoading = true;
+        if (path == FileInfo.FullName)
         {
             await FunctionsHelper.DuplicateFile();
         }
@@ -1483,6 +1474,7 @@ public class MainViewModel : ViewModelBase
                 FileHelper.DuplicateAndReturnFileName(path);
             });
         }
+        IsLoading = false;
     }
     
     private async Task ShowFilePropertiesTask(string path)

+ 28 - 45
src/PicView.Avalonia/Views/UC/EditableTitlebar.axaml.cs

@@ -8,34 +8,13 @@ using PicView.Avalonia.UI;
 using PicView.Avalonia.ViewModels;
 using PicView.Core.FileHandling;
 using PicView.Core.ImageDecoding;
+using PicView.Core.Localization;
+using PicView.Core.Navigation;
 
 namespace PicView.Avalonia.Views.UC;
 
 public partial class EditableTitlebar : UserControl
 {
-    #region Properties
-
-    public bool IsRenaming
-    {
-        get
-        {
-            if (DataContext is not MainViewModel vm)
-            {
-                return false;
-            }
-            return vm.ImageIterator?.IsRenamingInProgress ?? false;
-        }
-        private set
-        {
-            if (DataContext is not MainViewModel vm)
-            {
-                return;
-            }
-            vm.ImageIterator.IsRenamingInProgress = value;
-        }
-    }
-
-    #endregion
     public EditableTitlebar()
     {
         InitializeComponent();
@@ -56,7 +35,7 @@ public partial class EditableTitlebar : UserControl
             return;
         }
 
-        if (IsRenaming || vm.IsEditableTitlebarOpen)
+        if (vm.IsEditableTitlebarOpen)
         {
             return;
         }
@@ -136,14 +115,21 @@ public partial class EditableTitlebar : UserControl
             return;
         }
         vm.IsLoading = true;
-        IsRenaming = true;
         var oldPath = vm.FileInfo.FullName;
         var newPath = Path.Combine(vm.FileInfo.DirectoryName, TextBox.Text);
+
+        if (File.Exists(newPath))
+        {
+            // Show error message to user
+            return;
+        }
         
         // Check if the file is being moved to a different directory
         if (Path.GetDirectoryName(oldPath) != Path.GetDirectoryName(newPath))
         {
-            await Renamed().ConfigureAwait(false);
+            vm.ImageIterator?.RemoveCurrentItemFromPreLoader();
+            await vm.ImageIterator?.NextIteration(NavigateTo.Next);
+            FileHelper.RenameFile(oldPath, newPath);
             return;
         }
         
@@ -158,10 +144,9 @@ public partial class EditableTitlebar : UserControl
 
             if (saved)
             {
-                // Navigate to newly saved file
-                await NavigationHelper.LoadPicFromFile(newPath, vm).ConfigureAwait(false);
-                
                 // Delete old file
+                vm.ImageIterator?.RemoveCurrentItemFromPreLoader();
+                
                 var deleteMsg = FileDeletionHelper.DeleteFileWithErrorMsg(oldPath, false);
                 if (!string.IsNullOrWhiteSpace(deleteMsg))
                 {
@@ -172,36 +157,34 @@ public partial class EditableTitlebar : UserControl
                 }
             }
             await End();
-            return;
         }
-        var renamed = await Renamed().ConfigureAwait(false);
-        if (!renamed)
+        else
         {
-            IsRenaming = false;
-            return;
+            var renamed = FileHelper.RenameFile(oldPath, newPath);
+            if (!renamed)
+            {
+                // TODO Show error message
+                await TooltipHelper.ShowTooltipMessageAsync(TranslationHelper.Translation.UnexpectedError);
+                return;
+            }
+            await End();
         }
-        await End();
-        return;
 
-        async Task<bool> Renamed()
-        {
-            return await Task.FromResult(FileHelper.RenameFile(oldPath, newPath));
-        }
+        return;
 
         async Task End()
         {
-            SetTitleHelper.SetTitle(vm);
+            // vm.ImageIterator?.RemoveCurrentItemFromPreLoader();
+            // await NavigationHelper.LoadPicFromFile(newPath, vm).ConfigureAwait(false);
             vm.IsLoading = false;
-            IsRenaming = false;
             await Dispatcher.UIThread.InvokeAsync(() =>
             {
                 TextBox.ClearSelection();
-                SetTitleHelper.RefreshTitle(vm);
-                vm.IsEditableTitlebarOpen = false;
                 Cursor = new Cursor(StandardCursorType.Arrow);
                 MainKeyboardShortcuts.IsKeysEnabled = true;
                 UIHelper.GetMainView.Focus();
             });
+            vm.IsEditableTitlebarOpen = false;
         }
     }
     
@@ -229,7 +212,7 @@ public partial class EditableTitlebar : UserControl
         TextBox.SelectionEnd = end;
         vm.IsEditableTitlebarOpen = true;
         Cursor = new Cursor(StandardCursorType.Ibeam);
-        Focus();
+        TextBox.Focus();
     }
 
     #endregion