From 6f89a4e48d4fb9647f43837faf7f98eff2796e8f Mon Sep 17 00:00:00 2001 From: Dani John Date: Wed, 25 Oct 2023 18:50:38 +0530 Subject: [PATCH] Skip deleting livelyproperty directory if not present & some refactoring --- .../Helpers/Files/FileOperations.cs | 2 +- .../DepthEstimateWallpaperViewModel.cs | 2 +- .../ViewModels/LibraryViewModel.cs | 8 ++++---- .../ViewModels/SettingsViewModel.cs | 6 +++--- .../ViewModels/ThemeViewModel.cs | 2 +- src/Lively/Lively/Core/WinDesktopCore.cs | 19 ++++++++++++++----- 6 files changed, 24 insertions(+), 15 deletions(-) diff --git a/src/Lively/Lively.Common/Helpers/Files/FileOperations.cs b/src/Lively/Lively.Common/Helpers/Files/FileOperations.cs index c3cb267a..281cda24 100644 --- a/src/Lively/Lively.Common/Helpers/Files/FileOperations.cs +++ b/src/Lively/Lively.Common/Helpers/Files/FileOperations.cs @@ -158,7 +158,7 @@ public static async Task CopyFileAsync(string src, string dest) /// /// /// True if deletion completed succesfully. - public static async Task DeleteDirectoryAsync(string folderPath, int initialDelay = 1000, int retryDelay = 4000) + public static async Task TryDeleteDirectoryAsync(string folderPath, int initialDelay, int retryDelay) { bool status = true; if (Directory.Exists(folderPath)) diff --git a/src/Lively/Lively.UI.WinUI/ViewModels/DepthEstimateWallpaperViewModel.cs b/src/Lively/Lively.UI.WinUI/ViewModels/DepthEstimateWallpaperViewModel.cs index dcda7b6c..723c7816 100644 --- a/src/Lively/Lively.UI.WinUI/ViewModels/DepthEstimateWallpaperViewModel.cs +++ b/src/Lively/Lively.UI.WinUI/ViewModels/DepthEstimateWallpaperViewModel.cs @@ -183,7 +183,7 @@ await Task.Run(async() => ErrorText = $"{i18n.GetString("TextError")}: {ex.Message}"; PreviewText = string.Empty; - await FileOperations.DeleteDirectoryAsync(destDir, 0, 1000); + await FileOperations.TryDeleteDirectoryAsync(destDir, 0, 1000); } finally { diff --git a/src/Lively/Lively.UI.WinUI/ViewModels/LibraryViewModel.cs b/src/Lively/Lively.UI.WinUI/ViewModels/LibraryViewModel.cs index acf93923..07287e5a 100644 --- a/src/Lively/Lively.UI.WinUI/ViewModels/LibraryViewModel.cs +++ b/src/Lively/Lively.UI.WinUI/ViewModels/LibraryViewModel.cs @@ -275,7 +275,7 @@ public async Task WallpaperDelete(ILibraryModel obj, bool unsubscribe = true) //close if running. await desktopCore.CloseWallpaper(obj, true); //delete wp folder. - var success = await FileOperations.DeleteDirectoryAsync(obj.LivelyInfoFolderPath, 1000, 4000); + var success = await FileOperations.TryDeleteDirectoryAsync(obj.LivelyInfoFolderPath, 1000, 4000); if (success) { @@ -298,7 +298,7 @@ public async Task WallpaperDelete(ILibraryModel obj, bool unsubscribe = true) var item = new DirectoryInfo(wpdataDir[i]).Name; if (wpFolderName.Equals(item, StringComparison.Ordinal)) { - _ = FileOperations.DeleteDirectoryAsync(wpdataDir[i], 100, 1000); + _ = FileOperations.TryDeleteDirectoryAsync(wpdataDir[i], 100, 1000); break; } } @@ -367,7 +367,7 @@ await Task.Run(() => } finally { - _ = FileOperations.DeleteDirectoryAsync(tmpDir, 1000, 2000); + _ = FileOperations.TryDeleteDirectoryAsync(tmpDir, 1000, 2000); } } else if (libraryItem.LivelyInfo.IsAbsolutePath) @@ -420,7 +420,7 @@ await Task.Run(() => } finally { - _ = FileOperations.DeleteDirectoryAsync(tmpDir, 1000, 2000); + _ = FileOperations.TryDeleteDirectoryAsync(tmpDir, 1000, 2000); } } else diff --git a/src/Lively/Lively.UI.WinUI/ViewModels/SettingsViewModel.cs b/src/Lively/Lively.UI.WinUI/ViewModels/SettingsViewModel.cs index b81a75f6..02925561 100644 --- a/src/Lively/Lively.UI.WinUI/ViewModels/SettingsViewModel.cs +++ b/src/Lively/Lively.UI.WinUI/ViewModels/SettingsViewModel.cs @@ -1220,9 +1220,9 @@ await Task.Run(() => if (!isDestEmptyDir) { //not deleting the root folder, what if the user selects a folder that is not used by Lively alone! - var result1 = await FileOperations.DeleteDirectoryAsync(Path.Combine(previousDirectory, Constants.CommonPartialPaths.WallpaperInstallDir), 1000, 3000); - var result2 = await FileOperations.DeleteDirectoryAsync(Path.Combine(previousDirectory, Constants.CommonPartialPaths.WallpaperInstallTempDir), 0, 1000); - var result3 = await FileOperations.DeleteDirectoryAsync(Path.Combine(previousDirectory, Constants.CommonPartialPaths.WallpaperSettingsDir), 0, 1000); + var result1 = await FileOperations.TryDeleteDirectoryAsync(Path.Combine(previousDirectory, Constants.CommonPartialPaths.WallpaperInstallDir), 1000, 3000); + var result2 = await FileOperations.TryDeleteDirectoryAsync(Path.Combine(previousDirectory, Constants.CommonPartialPaths.WallpaperInstallTempDir), 0, 1000); + var result3 = await FileOperations.TryDeleteDirectoryAsync(Path.Combine(previousDirectory, Constants.CommonPartialPaths.WallpaperSettingsDir), 0, 1000); if (!(result1 && result2 && result3)) { //TODO: Dialogue diff --git a/src/Lively/Lively.UI.WinUI/ViewModels/ThemeViewModel.cs b/src/Lively/Lively.UI.WinUI/ViewModels/ThemeViewModel.cs index cd5c336c..87d3bc28 100644 --- a/src/Lively/Lively.UI.WinUI/ViewModels/ThemeViewModel.cs +++ b/src/Lively/Lively.UI.WinUI/ViewModels/ThemeViewModel.cs @@ -147,7 +147,7 @@ private async Task BrowseTheme() { SelectedItem = SelectedItem != obj ? SelectedItem : Themes[0]; Themes.Remove(obj); - await FileOperations.DeleteDirectoryAsync(Directory.GetParent(obj.File).FullName); + await FileOperations.TryDeleteDirectoryAsync(Directory.GetParent(obj.File).FullName, 1000, 4000); } }); diff --git a/src/Lively/Lively/Core/WinDesktopCore.cs b/src/Lively/Lively/Core/WinDesktopCore.cs index 71a64bcd..e62d9118 100644 --- a/src/Lively/Lively/Core/WinDesktopCore.cs +++ b/src/Lively/Lively/Core/WinDesktopCore.cs @@ -247,7 +247,7 @@ public async Task SetWallpaperAsync(ILibraryModel wallpaper, IDisplayMonitor dis { WallpaperUpdated?.Invoke(this, new WallpaperUpdateArgs() { Category = UpdateWallpaperType.remove, Info = wallpaper.LivelyInfo, InfoPath = wallpaper.LivelyInfoFolderPath }); //Deleting from core because incase UI client not running. - _ = FileOperations.DeleteDirectoryAsync(wallpaper.LivelyInfoFolderPath, 0, 1000); + await FileOperations.TryDeleteDirectoryAsync(wallpaper.LivelyInfoFolderPath, 0, 1000); } } catch (Win32Exception ex2) @@ -399,12 +399,21 @@ private async Task ShowWallpaperDialog(IWallpaper wallpaper) if (cancelled) { - //user cancelled/fail! + //User cancelled/fail! wallpaper.Terminate(); DesktopUtil.RefreshDesktop(); - //Deleting from core because incase UI client not running. - _ = FileOperations.DeleteDirectoryAsync(wallpaper.Model.LivelyInfoFolderPath, 0, 1000); - _ = FileOperations.DeleteDirectoryAsync(Directory.GetParent(Path.GetDirectoryName(wallpaper.LivelyPropertyCopyPath)).ToString(), 0, 1000); + + try + { + //Deleting here incase UI client is not running + await FileOperations.TryDeleteDirectoryAsync(wallpaper.Model.LivelyInfoFolderPath, 0, 1000); + if (wallpaper.LivelyPropertyCopyPath != null) + await FileOperations.TryDeleteDirectoryAsync(Directory.GetParent(Path.GetDirectoryName(wallpaper.LivelyPropertyCopyPath)).FullName, 0, 1000); + } + catch (Exception ie) + { + Logger.Error(ie); + } } return !cancelled;