123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175 |
- using System.Diagnostics;
- using PicView.Avalonia.ImageHandling;
- using PicView.Avalonia.UI;
- using PicView.Avalonia.ViewModels;
- using PicView.Avalonia.Views.UC.PopUps;
- using PicView.Core.FileHandling;
- using PicView.Core.Localization;
- namespace PicView.Avalonia.FileSystem;
- public static class FileManager
- {
- /// <summary>
- /// Deletes the current file, either permanently or by moving to recycle bin
- /// </summary>
- public static async Task DeleteFile(bool recycle, MainViewModel vm)
- {
- if (vm.PicViewer.FileInfo is null)
- {
- return;
- }
-
- try
- {
- string? errorMsg = null;
-
- if (!recycle)
- {
- var prompt = TranslationManager.GetTranslation("DeleteFilePermanently");
- var deleteDialog = new DeleteDialog(prompt, vm.PicViewer.FileInfo.FullName);
- UIHelper.GetMainView.MainGrid.Children.Add(deleteDialog);
- // Dialog handles the deletion
- }
- else
- {
- errorMsg = await Task.FromResult(FileDeletionHelper.DeleteFileWithErrorMsg(vm.PicViewer.FileInfo.FullName, recycle));
- }
-
- if (!string.IsNullOrEmpty(errorMsg))
- {
- await TooltipHelper.ShowTooltipMessageAsync(errorMsg, true);
- }
- }
- catch (Exception ex)
- {
- await LogAndShowError(ex, nameof(DeleteFile));
- }
- }
-
- /// <summary>
- /// Shows properties dialog for the specified file
- /// </summary>
- public static async Task ShowFileProperties(string path, MainViewModel vm)
- {
- if (!ValidateParameters(path, vm.PlatformService))
- {
- return;
- }
-
- try
- {
- await Task.Run(() => vm.PlatformService!.ShowFileProperties(path));
- }
- catch (Exception ex)
- {
- await LogAndShowError(ex, nameof(ShowFileProperties));
- }
- }
-
- /// <summary>
- /// Prints the specified image file
- /// </summary>
- public static async Task Print(string? path, MainViewModel vm)
- {
- if (!ValidateParameters(path, vm.PlatformService))
- {
- return;
- }
-
- try
- {
- vm.IsLoading = true;
- await ExecutePlatformServiceOperationAsync(path!, vm,
- (platformService, file) => platformService.Print(file));
- }
- catch (Exception ex)
- {
- await LogAndShowError(ex, nameof(Print));
- }
- finally
- {
- vm.IsLoading = false;
- }
- }
-
- /// <summary>
- /// Opens the file location in file explorer
- /// </summary>
- public static async Task LocateOnDisk(string path, MainViewModel vm)
- {
- if (!ValidateParameters(path, vm.PlatformService))
- {
- return;
- }
- try
- {
- await Task.Run(() => vm.PlatformService!.LocateOnDisk(path));
- }
- catch (Exception ex)
- {
- await LogAndShowError(ex, nameof(LocateOnDisk));
- }
- }
-
- /// <summary>
- /// Shows the dialog to open the file with another application
- /// </summary>
- public static async Task OpenWith(string path, MainViewModel vm)
- {
- if (!ValidateParameters(path, vm.PlatformService))
- {
- return;
- }
- try
- {
- await Task.Run(() => vm.PlatformService!.OpenWith(path));
- }
- catch (Exception ex)
- {
- await LogAndShowError(ex, nameof(LocateOnDisk));
- }
- }
- #region Private Helper Methods
-
- /// <summary>
- /// Validates common parameters for file operations
- /// </summary>
- private static bool ValidateParameters(string? path, object? platformService)
- {
- return !string.IsNullOrWhiteSpace(path) && platformService != null;
- }
-
- /// <summary>
- /// Helper method to handle common platform service operations that might require file conversion
- /// </summary>
- private static async Task ExecutePlatformServiceOperationAsync(string path, MainViewModel vm,
- Action<dynamic, string> platformServiceAction)
- {
- var file = await ImageFormatConverter.ConvertToCommonSupportedFormatAsync(path, vm)
- .ConfigureAwait(false);
-
- if (string.IsNullOrWhiteSpace(file))
- {
- await TooltipHelper.ShowTooltipMessageAsync(TranslationManager.Translation.UnexpectedError);
- return;
- }
- await Task.Run(() => platformServiceAction(vm.PlatformService!, file));
- }
-
- /// <summary>
- /// Logs errors and shows appropriate error messages
- /// </summary>
- private static async Task LogAndShowError(Exception ex, string methodName)
- {
- #if DEBUG
- Debug.WriteLine($"{nameof(FileManager)}.{methodName}: {ex.Message}");
- Debug.WriteLine($"Stack trace: {ex.StackTrace}");
- #endif
- await TooltipHelper.ShowTooltipMessageAsync(ex.Message);
- }
-
- #endregion
- }
|