From 2741366a3f3aa66da0c6d7c1f715752f9fbbe5de Mon Sep 17 00:00:00 2001 From: Jack251970 <1160210343@qq.com> Date: Mon, 24 Feb 2025 10:01:52 +0800 Subject: [PATCH] Improve code quality --- Flow.Launcher/ActionKeywords.xaml.cs | 25 +++++++++++----------- Flow.Launcher/ViewModel/PluginViewModel.cs | 2 -- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/Flow.Launcher/ActionKeywords.xaml.cs b/Flow.Launcher/ActionKeywords.xaml.cs index 10f3cf4e9..80a4abef1 100644 --- a/Flow.Launcher/ActionKeywords.xaml.cs +++ b/Flow.Launcher/ActionKeywords.xaml.cs @@ -4,7 +4,6 @@ using Flow.Launcher.Plugin; using Flow.Launcher.ViewModel; using Flow.Launcher.Core; using System.Linq; -using Flow.Launcher.Core.Plugin; using System.Collections.Generic; namespace Flow.Launcher @@ -43,11 +42,14 @@ namespace Flow.Launcher newActionKeywords = newActionKeywords.Count > 0 ? newActionKeywords : new() { Query.GlobalPluginWildcardSign }; - if (!newActionKeywords.Except(oldActionKeywords).Any(PluginManager.ActionKeywordRegistered)) + var addedActionKeywords = newActionKeywords.Except(oldActionKeywords).ToList(); + var removedActionKeywords = oldActionKeywords.Except(newActionKeywords).ToList(); + if (!addedActionKeywords.Any(App.API.ActionKeywordAssigned)) { if (oldActionKeywords.Count != newActionKeywords.Count) { - ReplaceActionKeyword(plugin.Metadata.ID, oldActionKeywords, newActionKeywords); + ReplaceActionKeyword(plugin.Metadata.ID, removedActionKeywords, addedActionKeywords); + return; } var sortedOldActionKeywords = oldActionKeywords.OrderBy(s => s).ToList(); @@ -61,7 +63,7 @@ namespace Flow.Launcher } else { - ReplaceActionKeyword(plugin.Metadata.ID, oldActionKeywords, newActionKeywords); + ReplaceActionKeyword(plugin.Metadata.ID, removedActionKeywords, addedActionKeywords); } } else @@ -71,21 +73,18 @@ namespace Flow.Launcher } } - private void ReplaceActionKeyword(string id, IReadOnlyList oldActionKeywords, IReadOnlyList newActionKeywords) + private void ReplaceActionKeyword(string id, IReadOnlyList removedActionKeywords, IReadOnlyList addedActionKeywords) { - // Because add & remove action keyword will change action keyword metadata, - // so we need to clone it to fix collection modified while iterating exception - var oldActionKeywordsClone = oldActionKeywords.ToList(); - foreach (var actionKeyword in oldActionKeywordsClone) + foreach (var actionKeyword in removedActionKeywords) { - PluginManager.RemoveActionKeyword(id, actionKeyword); + App.API.RemoveActionKeyword(id, actionKeyword); } - foreach (var actionKeyword in newActionKeywords) + foreach (var actionKeyword in addedActionKeywords) { - PluginManager.AddActionKeyword(id, actionKeyword); + App.API.AddActionKeyword(id, actionKeyword); } - // Update action keywords text and close + // Update action keywords text and close window pluginViewModel.OnActionKeywordsChanged(); Close(); } diff --git a/Flow.Launcher/ViewModel/PluginViewModel.cs b/Flow.Launcher/ViewModel/PluginViewModel.cs index c726cc343..9b717216a 100644 --- a/Flow.Launcher/ViewModel/PluginViewModel.cs +++ b/Flow.Launcher/ViewModel/PluginViewModel.cs @@ -8,7 +8,6 @@ using System.Windows.Controls; using CommunityToolkit.Mvvm.Input; using Flow.Launcher.Core.Resource; using Flow.Launcher.Resources.Controls; -using System.Collections.Generic; namespace Flow.Launcher.ViewModel { @@ -44,7 +43,6 @@ namespace Flow.Launcher.ViewModel } } - private async void LoadIconAsync() { Image = await ImageLoader.LoadAsync(PluginPair.Metadata.IcoPath);