From 10ecb1641071d54ee0154be0d6417bbfec8c3b52 Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Mon, 15 Apr 2024 23:28:37 +0200 Subject: [PATCH] disposable and description --- ...rToys.Run.Plugin.EdgeFavorite.Tests.csproj | 4 +-- .../Helpers/FavoriteProvider.cs | 17 +++++++++-- .../Helpers/ProfileManager.cs | 30 +++++++++++++++---- .../Main.cs | 16 ++++++++-- Directory.Build.props | 2 +- 5 files changed, 57 insertions(+), 12 deletions(-) diff --git a/Community.PowerToys.Run.Plugin.EdgeFavorite.Tests/Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.csproj b/Community.PowerToys.Run.Plugin.EdgeFavorite.Tests/Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.csproj index e0aeabd..14ac07d 100644 --- a/Community.PowerToys.Run.Plugin.EdgeFavorite.Tests/Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.csproj +++ b/Community.PowerToys.Run.Plugin.EdgeFavorite.Tests/Community.PowerToys.Run.Plugin.EdgeFavorite.Tests.csproj @@ -24,8 +24,8 @@ - - + + diff --git a/Community.PowerToys.Run.Plugin.EdgeFavorite/Helpers/FavoriteProvider.cs b/Community.PowerToys.Run.Plugin.EdgeFavorite/Helpers/FavoriteProvider.cs index 03e4386..4503588 100644 --- a/Community.PowerToys.Run.Plugin.EdgeFavorite/Helpers/FavoriteProvider.cs +++ b/Community.PowerToys.Run.Plugin.EdgeFavorite/Helpers/FavoriteProvider.cs @@ -9,11 +9,12 @@ namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers { - public class FavoriteProvider : IFavoriteProvider + public sealed class FavoriteProvider : IFavoriteProvider, IDisposable { private readonly string _path; private readonly FileSystemWatcher _watcher; private FavoriteItem _root; + private bool _disposed; public FavoriteItem Root => _root; @@ -37,6 +38,17 @@ public FavoriteProvider(string path, ProfileInfo profileInfo) _watcher.EnableRaisingEvents = true; } + public void Dispose() + { + if (_disposed) + { + return; + } + + _watcher?.Dispose(); + _disposed = true; + } + private void InitFavorites() { try @@ -103,7 +115,8 @@ private void ProcessFavorites(JsonElement element, FavoriteItem parent, string p if (children.ValueKind == JsonValueKind.Array) { - foreach (var child in children.EnumerateArray()) + using var childEnumerator = children.EnumerateArray(); + foreach (var child in childEnumerator) { ProcessFavorites(child, folder, path, false); } diff --git a/Community.PowerToys.Run.Plugin.EdgeFavorite/Helpers/ProfileManager.cs b/Community.PowerToys.Run.Plugin.EdgeFavorite/Helpers/ProfileManager.cs index 95c20ae..0da85c1 100644 --- a/Community.PowerToys.Run.Plugin.EdgeFavorite/Helpers/ProfileManager.cs +++ b/Community.PowerToys.Run.Plugin.EdgeFavorite/Helpers/ProfileManager.cs @@ -12,21 +12,19 @@ namespace Community.PowerToys.Run.Plugin.EdgeFavorite.Helpers { - public class ProfileManager : IProfileManager + public sealed class ProfileManager : IProfileManager, IDisposable { private static readonly string _userDataPath = Environment.ExpandEnvironmentVariables(@"%LOCALAPPDATA%\Microsoft\Edge\User Data"); private readonly List _favoriteProviders = new(); + private bool _disposed; public ReadOnlyCollection FavoriteProviders => _favoriteProviders.AsReadOnly(); - public ProfileManager() - { - } - public void ReloadProfiles(bool defaultOnly) { if (_favoriteProviders.Count > 0) { + DisposeFavoriteProviders(); _favoriteProviders.Clear(); } @@ -56,6 +54,17 @@ public void ReloadProfiles(bool defaultOnly) } } + public void Dispose() + { + if (_disposed) + { + return; + } + + DisposeFavoriteProviders(); + _disposed = true; + } + private static void TrySetProfileName(ProfileInfo profileInfo, string directoryPath) { _ = Task.Run(() => @@ -96,5 +105,16 @@ private static void TrySetProfileName(ProfileInfo profileInfo, string directoryP } }); } + + private void DisposeFavoriteProviders() + { + foreach (var privider in _favoriteProviders) + { + if (privider is IDisposable disposableProvider) + { + disposableProvider.Dispose(); + } + } + } } } diff --git a/Community.PowerToys.Run.Plugin.EdgeFavorite/Main.cs b/Community.PowerToys.Run.Plugin.EdgeFavorite/Main.cs index c091c5e..ea01da4 100644 --- a/Community.PowerToys.Run.Plugin.EdgeFavorite/Main.cs +++ b/Community.PowerToys.Run.Plugin.EdgeFavorite/Main.cs @@ -14,7 +14,7 @@ namespace Community.PowerToys.Run.Plugin.EdgeFavorite { - public class Main : IPlugin, ISettingProvider, IContextMenu + public sealed class Main : IPlugin, ISettingProvider, IContextMenu, IDisposable { public static string PluginID => "D73A7EF0633F4C82A14454FFD848F447"; @@ -27,10 +27,11 @@ public class Main : IPlugin, ISettingProvider, IContextMenu private PluginInitContext? _context; private bool _searchTree; private bool _defaultOnly; + private bool _disposed; public string Name => "Edge Favorite"; - public string Description => "Open Microsoft Edge favorites."; + public string Description => "Opens Microsoft Edge favorites"; public IEnumerable AdditionalOptions => new List { @@ -133,6 +134,17 @@ public List LoadContextMenus(Result selectedResult) return favorite.CreateContextMenuResult(); } + public void Dispose() + { + if (_disposed) + { + return; + } + + _profileManager?.Dispose(); + _disposed = true; + } + private void OnThemeChanged(Theme currentTheme, Theme newTheme) { UpdateIconsPath(newTheme); diff --git a/Directory.Build.props b/Directory.Build.props index ee4447b..1fa6a9b 100644 --- a/Directory.Build.props +++ b/Directory.Build.props @@ -1,6 +1,6 @@ - 0.4.0 + 0.4.1