Skip to content

Commit d5a2695

Browse files
committed
Update the history item if it equals the last added item or already exists in the list.
1 parent ced824d commit d5a2695

File tree

1 file changed

+38
-17
lines changed

1 file changed

+38
-17
lines changed

Flow.Launcher/Storage/QueryHistory.cs

Lines changed: 38 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Collections.Generic;
33
using System.Linq;
44
using System.Text.Json.Serialization;
5+
using CommunityToolkit.Mvvm.DependencyInjection;
6+
using Flow.Launcher.Helper;
7+
using Flow.Launcher.Infrastructure.UserSettings;
58
using Flow.Launcher.Plugin;
69

710
namespace Flow.Launcher.Storage
@@ -16,16 +19,19 @@ public class History
1619
[JsonInclude]
1720
public List<LastOpenedHistoryItem> LastOpenedHistoryItems { get; private set; } = [];
1821

22+
private readonly Settings _settings = Ioc.Default.GetRequiredService<Settings>();
23+
1924
private readonly int _maxHistory = 300;
2025

26+
2127
public void PopulateHistoryFromLegacyHistory()
2228
{
2329
if (Items.Count == 0) return;
2430
// Migrate old history items to new LastOpenedHistoryItems
2531
foreach (var item in Items)
2632
{
2733
LastOpenedHistoryItems.Add(new LastOpenedHistoryItem
28-
{
34+
{
2935
Query = item.Query,
3036
ExecutedDateTime = item.ExecutedDateTime
3137
});
@@ -34,33 +40,48 @@ public void PopulateHistoryFromLegacyHistory()
3440
}
3541

3642
public void Add(Result result)
37-
{
43+
{
3844
if (string.IsNullOrEmpty(result.OriginQuery.RawQuery)) return;
45+
if (string.IsNullOrEmpty(result.PluginID)) return;
3946

47+
var style = _settings.HistoryStyle;
4048
// Maintain the max history limit
41-
if (LastOpenedHistoryItems.Count > _maxHistory)
49+
if (LastOpenedHistoryItems.Count > _maxHistory)
4250
{
4351
LastOpenedHistoryItems.RemoveAt(0);
4452
}
4553

4654
// If the last item is the same as the current result, just update the timestamp
47-
if (LastOpenedHistoryItems.Count > 0 &&
48-
LastOpenedHistoryItems.Last().Equals(result))
55+
if (LastOpenedHistoryItems.Count > 0)
4956
{
50-
LastOpenedHistoryItems.Last().ExecutedDateTime = DateTime.Now;
51-
}
52-
else
53-
{
54-
LastOpenedHistoryItems.Add(new LastOpenedHistoryItem
57+
var last = LastOpenedHistoryItems.Last();
58+
if (result.IsEquals(last, style))
5559
{
56-
Title = result.Title,
57-
SubTitle = result.SubTitle,
58-
PluginID = result.PluginID,
59-
Query = result.OriginQuery.RawQuery,
60-
RecordKey = result.RecordKey,
61-
ExecutedDateTime = DateTime.Now
62-
});
60+
last.ExecutedDateTime = DateTime.Now;
61+
return;
62+
}
63+
64+
var existItem = LastOpenedHistoryItems.FirstOrDefault(x => result.IsEquals(x, style));
65+
66+
if (existItem != null)
67+
{
68+
existItem.ExecutedDateTime = DateTime.Now;
69+
return;
70+
}
6371
}
72+
73+
LastOpenedHistoryItems.Add(new LastOpenedHistoryItem
74+
{
75+
Title = result.Title,
76+
SubTitle = result.SubTitle,
77+
PluginID = result.PluginID,
78+
Query = result.OriginQuery.RawQuery,
79+
RecordKey = result.RecordKey,
80+
ExecutedDateTime = DateTime.Now,
81+
HistoryStyle = style
82+
});
6483
}
84+
85+
6586
}
6687
}

0 commit comments

Comments
 (0)