Skip to content

Commit a47d8fe

Browse files
authored
Merge branch 'dev' into improve_update
2 parents 8450e68 + 7c8a437 commit a47d8fe

File tree

3 files changed

+46
-30
lines changed

3 files changed

+46
-30
lines changed

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 36 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
3232
private static readonly string ClassName = nameof(MainViewModel);
3333

3434
private Query _lastQuery;
35-
private bool _lastIsHomeQuery;
35+
private bool _previousIsHomeQuery;
3636
private Query _progressQuery; // Used for QueryResultAsync
3737
private Query _updateQuery; // Used for ResultsUpdated
3838
private string _queryTextBeforeLeaveResults;
@@ -1267,7 +1267,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
12671267
return;
12681268
}
12691269

1270-
var isHomeQuery = query.RawQuery == string.Empty;
1270+
var currentIsHomeQuery = query.RawQuery == string.Empty;
12711271

12721272
try
12731273
{
@@ -1297,12 +1297,6 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
12971297
// Update the query's IsReQuery property to true if this is a re-query
12981298
query.IsReQuery = isReQuery;
12991299

1300-
// handle the exclusiveness of plugin using action keyword
1301-
RemoveOldQueryResults(query, isHomeQuery);
1302-
1303-
_lastQuery = query;
1304-
_lastIsHomeQuery = isHomeQuery;
1305-
13061300
ICollection<PluginPair> plugins = Array.Empty<PluginPair>();
13071301
if (isHomeQuery)
13081302
{
@@ -1360,7 +1354,7 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13601354
// plugins are ICollection, meaning LINQ will get the Count and preallocate Array
13611355

13621356
Task[] tasks;
1363-
if (isHomeQuery)
1357+
if (currentIsHomeQuery)
13641358
{
13651359
tasks = plugins.Select(plugin => plugin.Metadata.HomeDisabled switch
13661360
{
@@ -1416,7 +1410,7 @@ async Task QueryTaskAsync(PluginPair plugin, CancellationToken token)
14161410
{
14171411
App.API.LogDebug(ClassName, $"Wait for querying plugin <{plugin.Metadata.Name}>");
14181412

1419-
if (searchDelay && !isHomeQuery) // Do not delay for home query
1413+
if (searchDelay && !currentIsHomeQuery) // Do not delay for home query
14201414
{
14211415
var searchDelayTime = plugin.Metadata.SearchDelayTime ?? Settings.SearchDelayTime;
14221416

@@ -1429,7 +1423,7 @@ async Task QueryTaskAsync(PluginPair plugin, CancellationToken token)
14291423
// Task.Yield will force it to run in ThreadPool
14301424
await Task.Yield();
14311425

1432-
var results = isHomeQuery ?
1426+
var results = currentIsHomeQuery ?
14331427
await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
14341428
await PluginManager.QueryForPluginAsync(plugin, query, token);
14351429

@@ -1458,8 +1452,13 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
14581452

14591453
App.API.LogDebug(ClassName, $"Update results for plugin <{plugin.Metadata.Name}>");
14601454

1455+
// Indicate if to clear existing results so to show only ones from plugins with action keywords
1456+
var shouldClearExistingResults = ShouldClearExistingResults(query, currentIsHomeQuery);
1457+
_lastQuery = query;
1458+
_previousIsHomeQuery = currentIsHomeQuery;
1459+
14611460
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query,
1462-
token, reSelect)))
1461+
token, reSelect, shouldClearExistingResults)))
14631462
{
14641463
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
14651464
}
@@ -1561,25 +1560,36 @@ private async Task BuildQueryAsync(IEnumerable<BaseBuiltinShortcutModel> builtIn
15611560
}
15621561
}
15631562

1564-
private void RemoveOldQueryResults(Query query, bool isHomeQuery)
1563+
/// <summary>
1564+
/// Determines whether the existing search results should be cleared based on the current query and the previous query type.
1565+
/// This is needed because of the design that treats plugins with action keywords and global action keywords separately. Results are gathered
1566+
/// either from plugins with matching action keywords or global action keyword, but not both. So when the current results are from plugins
1567+
/// with a matching action keyword and a new result set comes from a new query with the global action keyword, the existing results need to be cleared,
1568+
/// and vice versa. The same applies to home page query results.
1569+
///
1570+
/// There is no need to clear results from global action keyword if a new set of results comes along that is also from global action keywords.
1571+
/// This is because the removal of obsolete results is handled in ResultsViewModel.NewResults(ICollection<ResultsForUpdate>).
1572+
/// </summary>
1573+
/// <param name="query">The current query.</param>
1574+
/// <param name="currentIsHomeQuery">A flag indicating if the current query is a home query.</param>
1575+
/// <returns>True if the existing results should be cleared, false otherwise.</returns>
1576+
private bool ShouldClearExistingResults(Query query, bool currentIsHomeQuery)
15651577
{
1566-
// If last and current query are home query, we don't need to clear the results
1567-
if (_lastIsHomeQuery && isHomeQuery)
1578+
// If previous or current results are from home query, we need to clear them
1579+
if (_previousIsHomeQuery || currentIsHomeQuery)
15681580
{
1569-
return;
1570-
}
1571-
// If last or current query is home query, we need to clear the results
1572-
else if (_lastIsHomeQuery || isHomeQuery)
1573-
{
1574-
App.API.LogDebug(ClassName, $"Remove old results");
1575-
Results.Clear();
1581+
App.API.LogDebug(ClassName, $"Cleared old results");
1582+
return true;
15761583
}
1577-
// If last and current query are not home query, we need to check action keyword
1578-
else if (_lastQuery?.ActionKeyword != query?.ActionKeyword)
1584+
1585+
// If the last and current query are not home query type, we need to check the action keyword
1586+
if (_lastQuery?.ActionKeyword != query?.ActionKeyword)
15791587
{
1580-
App.API.LogDebug(ClassName, $"Remove old results");
1581-
Results.Clear();
1588+
App.API.LogDebug(ClassName, $"Cleared old results");
1589+
return true;
15821590
}
1591+
1592+
return false;
15831593
}
15841594

15851595
private Result ContextMenuTopMost(Result result)

Flow.Launcher/ViewModel/ResultsForUpdate.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ public record struct ResultsForUpdate(
99
PluginMetadata Metadata,
1010
Query Query,
1111
CancellationToken Token,
12-
bool ReSelectFirstResult = true)
12+
bool ReSelectFirstResult = true,
13+
bool shouldClearExistingResults = false)
1314
{
1415
public string ID { get; } = Metadata.ID;
1516
}

Flow.Launcher/ViewModel/ResultsViewModel.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -232,10 +232,15 @@ private List<ResultViewModel> NewResults(ICollection<ResultsForUpdate> resultsFo
232232
if (!resultsForUpdates.Any())
233233
return Results;
234234

235+
var newResults = resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings));
236+
237+
if (resultsForUpdates.Any(x => x.shouldClearExistingResults))
238+
return newResults.OrderByDescending(rv => rv.Result.Score).ToList();
239+
235240
return Results.Where(r => r?.Result != null && resultsForUpdates.All(u => u.ID != r.Result.PluginID))
236-
.Concat(resultsForUpdates.SelectMany(u => u.Results, (u, r) => new ResultViewModel(r, _settings)))
237-
.OrderByDescending(rv => rv.Result.Score)
238-
.ToList();
241+
.Concat(newResults)
242+
.OrderByDescending(rv => rv.Result.Score)
243+
.ToList();
239244
}
240245
#endregion
241246

0 commit comments

Comments
 (0)