From 857ff739547e96ac3bde141ee68a4dab903f6aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=98=E9=9F=AC=20=E5=BC=A0?= Date: Wed, 17 Feb 2021 22:36:35 +0800 Subject: [PATCH] Use IReadOnlyCollection to avoid avoid duplicate iteration Add distinct to NewResults to try avoiding duplicate result --- Flow.Launcher/ViewModel/MainViewModel.cs | 2 +- Flow.Launcher/ViewModel/ResultsViewModel.cs | 13 ++++++------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index afbe6e197..6d92b0c65 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -748,7 +748,7 @@ namespace Flow.Launcher.ViewModel /// /// To avoid deadlock, this method should not called from main thread /// - public void UpdateResultView(IEnumerable resultsForUpdates) + public void UpdateResultView(IReadOnlyCollection resultsForUpdates) { if (!resultsForUpdates.Any()) return; diff --git a/Flow.Launcher/ViewModel/ResultsViewModel.cs b/Flow.Launcher/ViewModel/ResultsViewModel.cs index feab3a751..a08730488 100644 --- a/Flow.Launcher/ViewModel/ResultsViewModel.cs +++ b/Flow.Launcher/ViewModel/ResultsViewModel.cs @@ -1,16 +1,13 @@ -using System; +using Flow.Launcher.Infrastructure.UserSettings; +using Flow.Launcher.Plugin; using System.Collections.Generic; -using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; using System.Threading; -using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; -using Flow.Launcher.Infrastructure.UserSettings; -using Flow.Launcher.Plugin; namespace Flow.Launcher.ViewModel { @@ -146,7 +143,7 @@ namespace Flow.Launcher.ViewModel /// /// To avoid deadlock, this method should not called from main thread /// - public void AddResults(IEnumerable resultsForUpdates, CancellationToken token) + public void AddResults(IReadOnlyCollection resultsForUpdates, CancellationToken token) { var newResults = NewResults(resultsForUpdates); @@ -192,10 +189,11 @@ namespace Flow.Launcher.ViewModel return results.Where(r => r.Result.PluginID != resultId) .Concat(newResults) .OrderByDescending(r => r.Result.Score) + .Distinct() .ToList(); } - private List NewResults(IEnumerable resultsForUpdates) + private List NewResults(IReadOnlyCollection resultsForUpdates) { if (!resultsForUpdates.Any()) return Results.ToList(); @@ -205,6 +203,7 @@ namespace Flow.Launcher.ViewModel return results.Where(r => r != null && !resultsForUpdates.Any(u => u.Metadata.ID == r.Result.PluginID)) .Concat(resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings))) .OrderByDescending(rv => rv.Result.Score) + .Distinct() .ToList(); } #endregion