Skip to content

Commit 0e74643

Browse files
committed
Improve code quality
1 parent fcf6194 commit 0e74643

File tree

1 file changed

+21
-68
lines changed

1 file changed

+21
-68
lines changed

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 21 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ public void RegisterResultsUpdatedEvent()
263263
else
264264
{
265265
// make a clone to avoid possible issue that plugin will also change the list and items when updating view model
266-
resultsCopy = DeepCloneResults(e.Results, token);
266+
resultsCopy = DeepCloneResults(e.Results, false, token);
267267
}
268268

269269
foreach (var result in resultsCopy)
@@ -505,39 +505,31 @@ private async Task OpenResultAsync(string index)
505505
}
506506
}
507507

508-
private static IReadOnlyList<Result> DeepCloneResults(IReadOnlyList<Result> results, CancellationToken token = default)
508+
private static IReadOnlyList<Result> DeepCloneResults(IReadOnlyList<Result> results, bool isQuickSwitch, CancellationToken token = default)
509509
{
510510
var resultsCopy = new List<Result>();
511511

512-
foreach (var result in results.ToList())
512+
if (isQuickSwitch)
513513
{
514-
if (token.IsCancellationRequested)
514+
foreach (var result in results.ToList())
515515
{
516-
break;
517-
}
516+
if (token.IsCancellationRequested) break;
518517

519-
var resultCopy = result.Clone();
520-
resultsCopy.Add(resultCopy);
518+
var resultCopy = ((QuickSwitchResult)result).Clone();
519+
resultsCopy.Add(resultCopy);
520+
}
521521
}
522-
523-
return resultsCopy;
524-
}
525-
526-
private static IReadOnlyList<QuickSwitchResult> DeepCloneResults(IReadOnlyList<QuickSwitchResult> results, CancellationToken token = default)
527-
{
528-
var resultsCopy = new List<QuickSwitchResult>();
529-
530-
foreach (var result in results.ToList())
522+
else
531523
{
532-
if (token.IsCancellationRequested)
524+
foreach (var result in results.ToList())
533525
{
534-
break;
535-
}
526+
if (token.IsCancellationRequested) break;
536527

537-
var resultCopy = result.Clone();
538-
resultsCopy.Add(resultCopy);
528+
var resultCopy = result.Clone();
529+
resultsCopy.Add(resultCopy);
530+
}
539531
}
540-
532+
541533
return resultsCopy;
542534
}
543535

@@ -1466,51 +1458,12 @@ async Task QueryTaskAsync(PluginPair plugin, CancellationToken token)
14661458
// Task.Yield will force it to run in ThreadPool
14671459
await Task.Yield();
14681460

1469-
if (currentIsQuickSwitch)
1470-
{
1471-
var results = await PluginManager.QueryQuickSwitchForPluginAsync(plugin, query, token);
1472-
1473-
if (token.IsCancellationRequested) return;
1474-
1475-
IReadOnlyList<QuickSwitchResult> resultsCopy;
1476-
if (results == null)
1477-
{
1478-
resultsCopy = _emptyQuickSwitchResult;
1479-
}
1480-
else
1481-
{
1482-
// make a copy of results to avoid possible issue that FL changes some properties of the records, like score, etc.
1483-
resultsCopy = DeepCloneResults(results, token);
1484-
}
1485-
1486-
foreach (var result in resultsCopy)
1487-
{
1488-
if (string.IsNullOrEmpty(result.BadgeIcoPath))
1489-
{
1490-
result.BadgeIcoPath = plugin.Metadata.IcoPath;
1491-
}
1492-
}
1493-
1494-
if (token.IsCancellationRequested) return;
1495-
1496-
App.API.LogDebug(ClassName, $"Update results for plugin <{plugin.Metadata.Name}>");
1497-
1498-
// Indicate if to clear existing results so to show only ones from plugins with action keywords
1499-
var shouldClearExistingResults = ShouldClearExistingResults(query, currentIsHomeQuery);
1500-
_lastQuery = query;
1501-
_previousIsHomeQuery = currentIsHomeQuery;
1502-
1503-
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query,
1504-
token, reSelect, shouldClearExistingResults)))
1505-
{
1506-
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
1507-
}
1508-
}
1509-
else
15101461
{
1511-
var results = currentIsHomeQuery ?
1512-
await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
1513-
await PluginManager.QueryForPluginAsync(plugin, query, token);
1462+
IReadOnlyList<Result> results = currentIsQuickSwitch ?
1463+
await PluginManager.QueryQuickSwitchForPluginAsync(plugin, query, token) :
1464+
currentIsHomeQuery ?
1465+
await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
1466+
await PluginManager.QueryForPluginAsync(plugin, query, token);
15141467

15151468
if (token.IsCancellationRequested) return;
15161469

@@ -1522,7 +1475,7 @@ await PluginManager.QueryHomeForPluginAsync(plugin, query, token) :
15221475
else
15231476
{
15241477
// make a copy of results to avoid possible issue that FL changes some properties of the records, like score, etc.
1525-
resultsCopy = DeepCloneResults(results, token);
1478+
resultsCopy = DeepCloneResults(results, currentIsQuickSwitch, token);
15261479
}
15271480

15281481
foreach (var result in resultsCopy)

0 commit comments

Comments
 (0)