Improve code quality

This commit is contained in:
Jack251970 2025-02-24 10:01:52 +08:00
parent 6e763f0c3b
commit 2741366a3f
2 changed files with 12 additions and 15 deletions

View file

@ -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<string> oldActionKeywords, IReadOnlyList<string> newActionKeywords)
private void ReplaceActionKeyword(string id, IReadOnlyList<string> removedActionKeywords, IReadOnlyList<string> 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();
}

View file

@ -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);