feat: Populate new history system with legacy query history

This commit is contained in:
01Dri 2025-10-11 02:43:27 -03:00
parent 8e8e9d35ac
commit 9e1b8c1a72
5 changed files with 114 additions and 37 deletions

View file

@ -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<HistoryItem> LastOpenedHistoryItems { get; private set; } = [];
[JsonInclude]
public List<HistoryItem> QueryHistoryItems { get; private set; } = [];
//Legacy
[JsonInclude] public List<HistoryItemLegacy> Items { get; private set; } = [];
[JsonInclude] public List<HistoryItem> LastOpenedHistoryItems { get; private set; } = [];
[JsonInclude] public List<HistoryItem> 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<HistoryItem> GetHistoryItems(Settings settings)
{
if (settings.ShowHistoryQueryResultsForHomePage) return QueryHistoryItems;
return LastOpenedHistoryItems;
public List<HistoryItem> 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<HistoryItem> PopulateActions(List<HistoryItem> 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<ActionContext, bool> 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<ActionContext, bool> 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
};

View file

@ -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<ActionContext, bool> ExecuteAction { get; set; }
[JsonIgnore]
public Func<ActionContext, bool> QueryAction { get; set; }
}

View file

@ -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; }
}

View file

@ -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<HistoryItemLegacy> Items { get; private set; } = [];
}

View file

@ -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<Result> GetHistoryResults(IEnumerable<HistoryItem> historyItems)
{
var results = new List<Result>();
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);