Skip to content

Commit d6704ed

Browse files
committed
Support history items
1 parent e833333 commit d6704ed

File tree

1 file changed

+77
-4
lines changed

1 file changed

+77
-4
lines changed

Flow.Launcher/ViewModel/MainViewModel.cs

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,12 @@ public partial class MainViewModel : BaseModel, ISavable, IDisposable
5050

5151
private readonly IReadOnlyList<Result> _emptyResult = new List<Result>();
5252

53+
private readonly PluginMetadata _historyMetadata = new()
54+
{
55+
ID = "298303A65D128A845D28A7B83B3968C2",
56+
Priority = 0
57+
};
58+
5359
#endregion
5460

5561
#region Constructor
@@ -1300,11 +1306,30 @@ private async Task QueryResultsAsync(bool searchDelay, bool isReQuery = false, b
13001306

13011307
// plugins are ICollection, meaning LINQ will get the Count and preallocate Array
13021308

1303-
var tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch
1309+
Task[] tasks;
1310+
if (homeQuery)
13041311
{
1305-
false => QueryTaskAsync(plugin, _updateSource.Token),
1306-
true => Task.CompletedTask
1307-
}).ToArray();
1312+
var homeTasks = plugins.Select(plugin => plugin.Metadata.HomeDisabled switch
1313+
{
1314+
false => QueryTaskAsync(plugin, _updateSource.Token),
1315+
true => Task.CompletedTask
1316+
}).ToList();
1317+
1318+
if (Settings.ShowHistoryResultsForHomePage)
1319+
{
1320+
homeTasks.Add(QueryHistoryTaskAsync());
1321+
}
1322+
1323+
tasks = homeTasks.ToArray();
1324+
}
1325+
else
1326+
{
1327+
tasks = plugins.Select(plugin => plugin.Metadata.Disabled switch
1328+
{
1329+
false => QueryTaskAsync(plugin, _updateSource.Token),
1330+
true => Task.CompletedTask
1331+
}).ToArray();
1332+
}
13081333

13091334
try
13101335
{
@@ -1377,6 +1402,54 @@ await PluginManager.QueryHomeForPluginAsync(plugin, token) :
13771402
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
13781403
}
13791404
}
1405+
1406+
async Task QueryHistoryTaskAsync()
1407+
{
1408+
// Since it is wrapped within a ThreadPool Thread, the synchronous context is null
1409+
// Task.Yield will force it to run in ThreadPool
1410+
await Task.Yield();
1411+
1412+
// Select last history results and revert its order to make sure last history results are on top
1413+
var lastHistoryResults = _history.Items.TakeLast(Settings.MaxHistoryResultsToShowForHomePage).Reverse();
1414+
1415+
var historyResults = new List<Result>();
1416+
foreach (var h in lastHistoryResults)
1417+
{
1418+
var title = App.API.GetTranslation("executeQuery");
1419+
var time = App.API.GetTranslation("lastExecuteTime");
1420+
var result = new Result
1421+
{
1422+
Title = string.Format(title, h.Query),
1423+
SubTitle = string.Format(time, h.ExecutedDateTime),
1424+
IcoPath = "Images\\history.png",
1425+
Preview = new Result.PreviewInfo
1426+
{
1427+
PreviewImagePath = Constant.HistoryIcon,
1428+
Description = string.Format(time, h.ExecutedDateTime)
1429+
},
1430+
OriginQuery = new Query { RawQuery = h.Query },
1431+
Action = _ =>
1432+
{
1433+
SelectedResults = Results;
1434+
App.API.ChangeQuery(h.Query);
1435+
return false;
1436+
}
1437+
};
1438+
historyResults.Add(result);
1439+
}
1440+
1441+
// No need to make copy of results and update badge ico property
1442+
1443+
if (_updateSource.Token.IsCancellationRequested) return;
1444+
1445+
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(historyResults, _historyMetadata, query,
1446+
_updateSource.Token)))
1447+
{
1448+
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
1449+
}
1450+
1451+
await Task.CompletedTask;
1452+
}
13801453
}
13811454

13821455
private async Task<Query> ConstructQueryAsync(string queryText, IEnumerable<CustomShortcutModel> customShortcuts,

0 commit comments

Comments
 (0)