From 9e1b8c1a729fddef291332bc30443d1cb512b93c Mon Sep 17 00:00:00 2001 From: 01Dri Date: Sat, 11 Oct 2025 02:43:27 -0300 Subject: [PATCH] feat: Populate new history system with legacy query history --- Flow.Launcher/Storage/History.cs | 96 ++++++++++++++++------ Flow.Launcher/Storage/HistoryItem.cs | 6 +- Flow.Launcher/Storage/HistoryItemLegacy.cs | 13 +++ Flow.Launcher/Storage/HistoryLegacy.cs | 12 +++ Flow.Launcher/ViewModel/MainViewModel.cs | 24 +++--- 5 files changed, 114 insertions(+), 37 deletions(-) create mode 100644 Flow.Launcher/Storage/HistoryItemLegacy.cs create mode 100644 Flow.Launcher/Storage/HistoryLegacy.cs diff --git a/Flow.Launcher/Storage/History.cs b/Flow.Launcher/Storage/History.cs index 9155f02d8..0dd192558 100644 --- a/Flow.Launcher/Storage/History.cs +++ b/Flow.Launcher/Storage/History.cs @@ -1,38 +1,89 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; using System.Text.Json.Serialization; -using System.Threading.Tasks; -using Flow.Launcher.Infrastructure.UserSettings; +using System.Threading; +using Flow.Launcher.Core.Plugin; using Flow.Launcher.Plugin; -using JetBrains.Annotations; namespace Flow.Launcher.Storage { + public class History { - [JsonInclude] - public List LastOpenedHistoryItems { get; private set; } = []; - [JsonInclude] - public List QueryHistoryItems { get; private set; } = []; + //Legacy + [JsonInclude] public List Items { get; private set; } = []; + [JsonInclude] public List LastOpenedHistoryItems { get; private set; } = []; + [JsonInclude] public List QueryHistoryItems { get; private set; } = []; private int _maxHistory = 300; - public void AddToHistory(Result result, Settings settings) - { - if (settings.ShowHistoryQueryResultsForHomePage) + public void AddToHistory(Result result, bool isQuery) + { + if (isQuery) { - AddLastQuery(result); + AddLastQuery(result); return; } + AddLastOpened(result); } - public List GetHistoryItems(Settings settings) - { - if (settings.ShowHistoryQueryResultsForHomePage) return QueryHistoryItems; - return LastOpenedHistoryItems; + + + public List GetHistoryItems(bool isQuery) + { + if (isQuery) return PopulateActions(QueryHistoryItems, isQuery); + return PopulateActions(LastOpenedHistoryItems, isQuery); + } + + public void PopulateHistoryWithLegacyHistory() + { + foreach (var item in Items) + { + QueryHistoryItems.Add(new HistoryItem + { + RawQuery = item.Query, + ExecutedDateTime = item.ExecutedDateTime ?? DateTime.Now, + QueryAction = GetQueryAction(item.Query) + }); + } + if (Items.Any()) Items.Clear(); + } + + private List PopulateActions(List items,bool isQuery) + { + + foreach (var item in items) + { + if (item.QueryAction != null && item.ExecuteAction != null) continue; + if (isQuery && item.QueryAction == null) item.QueryAction = GetQueryAction(item.RawQuery); + if (!isQuery && item.ExecuteAction == null) item.ExecuteAction = GetExecuteAction(item.PluginID, item.RawQuery, item.Title, item.SubTitle); + } + + return items; + } + + private Func GetExecuteAction(string pluginId, string rawQuery, string title, string subTitle) + { + var plugin = PluginManager.GetPluginForId(pluginId); + + var query = QueryBuilder.Build(rawQuery, PluginManager.NonGlobalPlugins); + var freshResults = plugin.Plugin + .QueryAsync(query, CancellationToken.None) + .GetAwaiter() + .GetResult(); + return freshResults?.FirstOrDefault(r => r.Title == title + && r.SubTitle == subTitle)?.Action; + } + private Func GetQueryAction(string query) + { + return _=> + { + App.API.BackToQueryResults(); + App.API.ChangeQuery(query); + return false; + }; } private void AddLastQuery(Result result) @@ -43,7 +94,7 @@ namespace Flow.Launcher.Storage QueryHistoryItems.RemoveAt(0); } - if (QueryHistoryItems.Count > 0 && QueryHistoryItems.Last().OriginQuery.RawQuery == result.OriginQuery.RawQuery) + if (QueryHistoryItems.Count > 0 && QueryHistoryItems.Last().RawQuery == result.OriginQuery.RawQuery) { QueryHistoryItems.Last().ExecutedDateTime = DateTime.Now; } @@ -51,14 +102,9 @@ namespace Flow.Launcher.Storage { QueryHistoryItems.Add(new HistoryItem { - OriginQuery = result.OriginQuery, + RawQuery = result.OriginQuery.RawQuery, ExecutedDateTime = DateTime.Now, - QueryAction = _ => - { - App.API.BackToQueryResults(); - App.API.ChangeQuery(result.OriginQuery.RawQuery); - return false; - } + QueryAction = GetQueryAction(result.OriginQuery.RawQuery) }); } } @@ -71,7 +117,7 @@ namespace Flow.Launcher.Storage SubTitle = result.SubTitle, IcoPath = result.IcoPath ?? string.Empty, PluginID = result.PluginID, - OriginQuery = result.OriginQuery, + RawQuery = result.OriginQuery.RawQuery, ExecutedDateTime = DateTime.Now, ExecuteAction = result.Action }; diff --git a/Flow.Launcher/Storage/HistoryItem.cs b/Flow.Launcher/Storage/HistoryItem.cs index d7503108c..fb63aeffc 100644 --- a/Flow.Launcher/Storage/HistoryItem.cs +++ b/Flow.Launcher/Storage/HistoryItem.cs @@ -1,4 +1,5 @@ using System; +using System.Text.Json.Serialization; using Flow.Launcher.Plugin; namespace Flow.Launcher.Storage; @@ -8,9 +9,12 @@ public class HistoryItem public string SubTitle { get; set; } = string.Empty; public string IcoPath { get; set; } = string.Empty; public string PluginID { get; set; } = string.Empty; - public Query OriginQuery { get; set; } = null!; + public string RawQuery { get; set; } + public DateTime ExecutedDateTime { get; set; } + [JsonIgnore] public Func ExecuteAction { get; set; } + [JsonIgnore] public Func QueryAction { get; set; } } diff --git a/Flow.Launcher/Storage/HistoryItemLegacy.cs b/Flow.Launcher/Storage/HistoryItemLegacy.cs new file mode 100644 index 000000000..1ae62ff9e --- /dev/null +++ b/Flow.Launcher/Storage/HistoryItemLegacy.cs @@ -0,0 +1,13 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Flow.Launcher.Storage; +public class HistoryItemLegacy +{ + public string Query { get; set; } + public DateTime? ExecutedDateTime { get; set; } + +} diff --git a/Flow.Launcher/Storage/HistoryLegacy.cs b/Flow.Launcher/Storage/HistoryLegacy.cs new file mode 100644 index 000000000..8311c0a8d --- /dev/null +++ b/Flow.Launcher/Storage/HistoryLegacy.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.Json.Serialization; +using System.Threading.Tasks; + +namespace Flow.Launcher.Storage; +public class HistoryLegacy +{ + [JsonInclude] public List Items { get; private set; } = []; +} diff --git a/Flow.Launcher/ViewModel/MainViewModel.cs b/Flow.Launcher/ViewModel/MainViewModel.cs index ad530cc1d..db67539aa 100644 --- a/Flow.Launcher/ViewModel/MainViewModel.cs +++ b/Flow.Launcher/ViewModel/MainViewModel.cs @@ -153,6 +153,7 @@ namespace Flow.Launcher.ViewModel _topMostRecord = new FlowLauncherJsonStorageTopMostRecord(); _history = _historyStorage.Load(); + _history.PopulateHistoryWithLegacyHistory(); _userSelectedRecord = _userSelectedRecordStorage.Load(); ContextMenu = new ResultsViewModel(Settings, this) @@ -354,7 +355,7 @@ namespace Flow.Launcher.ViewModel if (QueryResultsSelected()) { SelectedResults = History; - History.SelectedIndex = _history.GetHistoryItems(Settings).Count - 1; + History.SelectedIndex = _history.GetHistoryItems(Settings.ShowHistoryQueryResultsForHomePage).Count - 1; } else { @@ -382,10 +383,10 @@ namespace Flow.Launcher.ViewModel [RelayCommand] public void ReverseHistory() { - var historyItems = _history.GetHistoryItems(Settings); + var historyItems = _history.GetHistoryItems(Settings.ShowHistoryQueryResultsForHomePage); if (historyItems.Count > 0) { - ChangeQueryText(historyItems[^lastHistoryIndex].OriginQuery.RawQuery); + ChangeQueryText(historyItems[^lastHistoryIndex].RawQuery); if (lastHistoryIndex < historyItems.Count) { lastHistoryIndex++; @@ -396,11 +397,11 @@ namespace Flow.Launcher.ViewModel [RelayCommand] public void ForwardHistory() { - var historyItems = _history.GetHistoryItems(Settings); + var historyItems = _history.GetHistoryItems(Settings.ShowHistoryQueryResultsForHomePage); if (historyItems.Count > 0) { - ChangeQueryText(historyItems[^lastHistoryIndex].OriginQuery.RawQuery); + ChangeQueryText(historyItems[^lastHistoryIndex].RawQuery); if (lastHistoryIndex > 1) { lastHistoryIndex--; @@ -537,7 +538,7 @@ namespace Flow.Launcher.ViewModel { if (Settings.ShowHistoryOnHomePage) { - _history.AddToHistory(result, Settings); + _history.AddToHistory(result, Settings.ShowHistoryQueryResultsForHomePage); } _userSelectedRecord.Add(result); @@ -615,7 +616,7 @@ namespace Flow.Launcher.ViewModel [RelayCommand] private void SelectPrevItem() { - var historyItems = _history.GetHistoryItems(Settings); + var historyItems = _history.GetHistoryItems(Settings.ShowHistoryQueryResultsForHomePage); if (QueryResultsSelected() // Results selected && string.IsNullOrEmpty(QueryText) // No input && Results.Visibility != Visibility.Visible // No items in result list, e.g. when home page is off and no query text is entered, therefore the view is collapsed. @@ -1301,7 +1302,7 @@ namespace Flow.Launcher.ViewModel var query = QueryText.ToLower().Trim(); History.Clear(); - var items = _history.GetHistoryItems(Settings); + var items = _history.GetHistoryItems(Settings.ShowHistoryQueryResultsForHomePage); var results = GetHistoryResults(items); @@ -1323,16 +1324,17 @@ namespace Flow.Launcher.ViewModel private List GetHistoryResults(IEnumerable historyItems) { var results = new List(); + foreach (var h in historyItems) { var result = new Result { Title = - Settings.ShowHistoryQueryResultsForHomePage ? Localize.executeQuery(h.OriginQuery.RawQuery) : h.Title, + Settings.ShowHistoryQueryResultsForHomePage ? Localize.executeQuery(h.RawQuery) : h.Title, SubTitle = Localize.lastExecuteTime(h.ExecutedDateTime), IcoPath = Constant.HistoryIcon, PluginID = h.PluginID, - OriginQuery = h.OriginQuery, + OriginQuery = QueryBuilder.Build(h.RawQuery, PluginManager.NonGlobalPlugins), Action = Settings.ShowHistoryLastOpenedResultsForHomePage ? h.ExecuteAction : h.QueryAction, Glyph = new GlyphInfo(FontFamily: "/Resources/#Segoe Fluent Icons", Glyph: "\uE81C") }; @@ -1568,7 +1570,7 @@ namespace Flow.Launcher.ViewModel void QueryHistoryTask(CancellationToken token) { // Select last history results and revert its order to make sure last history results are on top - var historyItems = _history.GetHistoryItems(Settings).TakeLast(Settings.MaxHistoryResultsToShowForHomePage).Reverse(); + var historyItems = _history.GetHistoryItems(Settings.ShowHistoryQueryResultsForHomePage).TakeLast(Settings.MaxHistoryResultsToShowForHomePage).Reverse(); var results = GetHistoryResults(historyItems);