|
| 1 | +using System.Linq; |
| 2 | +using System.Threading; |
| 3 | +using System.Threading.Tasks; |
| 4 | +using Flow.Launcher.Core.Plugin; |
| 5 | +using Flow.Launcher.Plugin; |
| 6 | +using Flow.Launcher.Storage; |
| 7 | + |
| 8 | +namespace Flow.Launcher.Helper; |
| 9 | + |
| 10 | +#nullable enable |
| 11 | + |
| 12 | +public static class ResultHelper |
| 13 | +{ |
| 14 | + public static async Task<Result?> PopulateResultsAsync(LastOpenedHistoryItem item) |
| 15 | + { |
| 16 | + return await PopulateResultsAsync(item.PluginID, item.Query, item.Title, item.SubTitle, item.RecordKey); |
| 17 | + } |
| 18 | + |
| 19 | + public static async Task<Result?> PopulateResultsAsync(string pluginId, string rawQuery, string title, string subTitle, string recordKey) |
| 20 | + { |
| 21 | + var plugin = PluginManager.GetPluginForId(pluginId); |
| 22 | + if (plugin == null) return null; |
| 23 | + var query = QueryBuilder.Build(rawQuery, PluginManager.NonGlobalPlugins); |
| 24 | + if (query == null) return null; |
| 25 | + try |
| 26 | + { |
| 27 | + var freshResults = await plugin.Plugin.QueryAsync(query, CancellationToken.None); |
| 28 | + // Try to match by record key first if it is valid, otherwise fall back to title + subtitle match |
| 29 | + if (string.IsNullOrEmpty(recordKey)) |
| 30 | + { |
| 31 | + return freshResults?.FirstOrDefault(r => r.Title == title && r.SubTitle == subTitle); |
| 32 | + } |
| 33 | + else |
| 34 | + { |
| 35 | + return freshResults?.FirstOrDefault(r => r.RecordKey == recordKey) ?? |
| 36 | + freshResults?.FirstOrDefault(r => r.Title == title && r.SubTitle == subTitle); |
| 37 | + } |
| 38 | + } |
| 39 | + catch (System.Exception e) |
| 40 | + { |
| 41 | + App.API.LogException(nameof(ResultHelper), $"Failed to query results for {plugin.Metadata.Name}", e); |
| 42 | + return null; |
| 43 | + } |
| 44 | + } |
| 45 | +} |
0 commit comments