Update the history item if it equals the last added item or already exists in the list.

This commit is contained in:
01Dri 2025-10-14 22:50:29 -03:00
parent ced824d791
commit d5a2695766

View file

@ -2,6 +2,9 @@
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
using CommunityToolkit.Mvvm.DependencyInjection;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
namespace Flow.Launcher.Storage
@ -16,8 +19,11 @@ namespace Flow.Launcher.Storage
[JsonInclude]
public List<LastOpenedHistoryItem> LastOpenedHistoryItems { get; private set; } = [];
private readonly Settings _settings = Ioc.Default.GetRequiredService<Settings>();
private readonly int _maxHistory = 300;
public void PopulateHistoryFromLegacyHistory()
{
if (Items.Count == 0) return;
@ -25,7 +31,7 @@ namespace Flow.Launcher.Storage
foreach (var item in Items)
{
LastOpenedHistoryItems.Add(new LastOpenedHistoryItem
{
{
Query = item.Query,
ExecutedDateTime = item.ExecutedDateTime
});
@ -34,33 +40,48 @@ namespace Flow.Launcher.Storage
}
public void Add(Result result)
{
{
if (string.IsNullOrEmpty(result.OriginQuery.RawQuery)) return;
if (string.IsNullOrEmpty(result.PluginID)) return;
var style = _settings.HistoryStyle;
// Maintain the max history limit
if (LastOpenedHistoryItems.Count > _maxHistory)
if (LastOpenedHistoryItems.Count > _maxHistory)
{
LastOpenedHistoryItems.RemoveAt(0);
}
// If the last item is the same as the current result, just update the timestamp
if (LastOpenedHistoryItems.Count > 0 &&
LastOpenedHistoryItems.Last().Equals(result))
if (LastOpenedHistoryItems.Count > 0)
{
LastOpenedHistoryItems.Last().ExecutedDateTime = DateTime.Now;
}
else
{
LastOpenedHistoryItems.Add(new LastOpenedHistoryItem
var last = LastOpenedHistoryItems.Last();
if (result.IsEquals(last, style))
{
Title = result.Title,
SubTitle = result.SubTitle,
PluginID = result.PluginID,
Query = result.OriginQuery.RawQuery,
RecordKey = result.RecordKey,
ExecutedDateTime = DateTime.Now
});
last.ExecutedDateTime = DateTime.Now;
return;
}
var existItem = LastOpenedHistoryItems.FirstOrDefault(x => result.IsEquals(x, style));
if (existItem != null)
{
existItem.ExecutedDateTime = DateTime.Now;
return;
}
}
LastOpenedHistoryItems.Add(new LastOpenedHistoryItem
{
Title = result.Title,
SubTitle = result.SubTitle,
PluginID = result.PluginID,
Query = result.OriginQuery.RawQuery,
RecordKey = result.RecordKey,
ExecutedDateTime = DateTime.Now,
HistoryStyle = style
});
}
}
}