diff --git a/Flow.Launcher/Storage/History.cs b/Flow.Launcher/Storage/History.cs new file mode 100644 index 000000000..5a94c8a8a --- /dev/null +++ b/Flow.Launcher/Storage/History.cs @@ -0,0 +1,92 @@ +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 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; } = []; + + private int _maxHistory = 300; + + public void AddToHistory(Result result, Settings settings) + { + if (settings.ShowHistoryQueryResultsForHomePage) + { + AddLastQuery(result); + return; + } + AddLastOpened(result); + } + + public List GetHistoryItems(Settings settings) + { + if (settings.ShowHistoryQueryResultsForHomePage) return QueryHistoryItems; + if (settings.ShowHistoryLastOpenedResultsForHomePage) return LastOpenedHistoryItems; + return new List(); + } + + private void AddLastQuery(Result result) + { + if (string.IsNullOrEmpty(result.OriginQuery.RawQuery)) return; + if (QueryHistoryItems.Count > _maxHistory) + { + QueryHistoryItems.RemoveAt(0); + } + + if (QueryHistoryItems.Count > 0 && QueryHistoryItems.Last().OriginQuery == result.OriginQuery) + { + QueryHistoryItems.Last().ExecutedDateTime = DateTime.Now; + } + else + { + QueryHistoryItems.Add(new HistoryItem + { + OriginQuery = result.OriginQuery, + ExecutedDateTime = DateTime.Now + }); + } + } + + private void AddLastOpened(Result result) + { + var item = new HistoryItem + { + Title = result.Title, + SubTitle = result.SubTitle, + IcoPath = result.IcoPath ?? string.Empty, + PluginID = result.PluginID, + OriginQuery = result.OriginQuery, + ExecutedDateTime = DateTime.Now, + }; + + var existing = LastOpenedHistoryItems. + FirstOrDefault(x => x.Title == item.Title && x.PluginID == item.PluginID); + + + if (existing != null) + { + existing.ExecutedDateTime = DateTime.Now; + } + else + { + if (LastOpenedHistoryItems.Count > _maxHistory) + { + LastOpenedHistoryItems.RemoveAt(0); + } + + LastOpenedHistoryItems.Add(item); + } + } + } +} diff --git a/Flow.Launcher/Storage/HistoryItem.cs b/Flow.Launcher/Storage/HistoryItem.cs index 6f0bf7461..cb95e75d1 100644 --- a/Flow.Launcher/Storage/HistoryItem.cs +++ b/Flow.Launcher/Storage/HistoryItem.cs @@ -1,45 +1,52 @@ -using System; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Flow.Launcher.Plugin; -namespace Flow.Launcher.Storage +namespace Flow.Launcher.Storage; +public class HistoryItem { - public class HistoryItem + public string Title { get; set; } = string.Empty; + 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 DateTime ExecutedDateTime { get; set; } + + public string GetTimeAgo() { - public string Query { get; set; } - public DateTime ExecutedDateTime { get; set; } - - public string GetTimeAgo() - { - return DateTimeAgo(ExecutedDateTime); - } - - private string DateTimeAgo(DateTime dt) - { - var span = DateTime.Now - dt; - if (span.Days > 365) - { - int years = (span.Days / 365); - if (span.Days % 365 != 0) - years += 1; - return $"about {years} {(years == 1 ? "year" : "years")} ago"; - } - if (span.Days > 30) - { - int months = (span.Days / 30); - if (span.Days % 31 != 0) - months += 1; - return $"about {months} {(months == 1 ? "month" : "months")} ago"; - } - if (span.Days > 0) - return $"about {span.Days} {(span.Days == 1 ? "day" : "days")} ago"; - if (span.Hours > 0) - return $"about {span.Hours} {(span.Hours == 1 ? "hour" : "hours")} ago"; - if (span.Minutes > 0) - return $"about {span.Minutes} {(span.Minutes == 1 ? "minute" : "minutes")} ago"; - if (span.Seconds > 5) - return $"about {span.Seconds} seconds ago"; - if (span.Seconds <= 5) - return "just now"; - return string.Empty; - } + return DateTimeAgo(ExecutedDateTime); } -} \ No newline at end of file + + private string DateTimeAgo(DateTime dt) + { + var span = DateTime.Now - dt; + if (span.Days > 365) + { + int years = (span.Days / 365); + if (span.Days % 365 != 0) + years += 1; + return $"about {years} {(years == 1 ? "year" : "years")} ago"; + } + if (span.Days > 30) + { + int months = (span.Days / 30); + if (span.Days % 31 != 0) + months += 1; + return $"about {months} {(months == 1 ? "month" : "months")} ago"; + } + if (span.Days > 0) + return $"about {span.Days} {(span.Days == 1 ? "day" : "days")} ago"; + if (span.Hours > 0) + return $"about {span.Hours} {(span.Hours == 1 ? "hour" : "hours")} ago"; + if (span.Minutes > 0) + return $"about {span.Minutes} {(span.Minutes == 1 ? "minute" : "minutes")} ago"; + if (span.Seconds > 5) + return $"about {span.Seconds} seconds ago"; + if (span.Seconds <= 5) + return "just now"; + return string.Empty; + } +} diff --git a/Flow.Launcher/Storage/LastOpenedHistory.cs b/Flow.Launcher/Storage/LastOpenedHistory.cs deleted file mode 100644 index 19f30855e..000000000 --- a/Flow.Launcher/Storage/LastOpenedHistory.cs +++ /dev/null @@ -1,42 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Text.Json.Serialization; -using System.Threading.Tasks; -using Flow.Launcher.Plugin; -using Flow.Launcher.ViewModel; - -namespace Flow.Launcher.Storage; -public class LastOpenedHistory -{ - [JsonInclude] public List Items { get; private set; } = []; - private const int MaxHistory = 300; - - public void Add(Result result) - { - var item = new LastOpenedHistoryItem - { - Title = result.Title, - SubTitle = result.SubTitle, - IcoPath = result.IcoPath ?? string.Empty, - PluginID = result.PluginID, - OriginQuery = result.OriginQuery, - ExecutedDateTime = DateTime.Now - }; - - // Aparenta estar duplicando... - var existing = Items.FirstOrDefault(x => x.OriginQuery.RawQuery == item.OriginQuery.RawQuery && x.PluginID == item.PluginID); - if (existing != null) - { - Items.Remove(existing); - } - - Items.Add(item); - - if (Items.Count > MaxHistory) - { - Items.RemoveAt(0); - } - } -} diff --git a/Flow.Launcher/Storage/LastOpenedHistoryItem.cs b/Flow.Launcher/Storage/LastOpenedHistoryItem.cs deleted file mode 100644 index 28bd49a95..000000000 --- a/Flow.Launcher/Storage/LastOpenedHistoryItem.cs +++ /dev/null @@ -1,17 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Flow.Launcher.Plugin; - -namespace Flow.Launcher.Storage; -public class LastOpenedHistoryItem -{ - public string Title { get; set; } = string.Empty; - 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 DateTime ExecutedDateTime { get; set; } -} diff --git a/Flow.Launcher/Storage/QueryHistory.cs b/Flow.Launcher/Storage/QueryHistory.cs deleted file mode 100644 index a5383b179..000000000 --- a/Flow.Launcher/Storage/QueryHistory.cs +++ /dev/null @@ -1,37 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text.Json.Serialization; - -namespace Flow.Launcher.Storage -{ - public class History - { - [JsonInclude] - public List Items { get; private set; } = new List(); - - private int _maxHistory = 300; - - public void Add(string query) - { - if (string.IsNullOrEmpty(query)) return; - if (Items.Count > _maxHistory) - { - Items.RemoveAt(0); - } - - if (Items.Count > 0 && Items.Last().Query == query) - { - Items.Last().ExecutedDateTime = DateTime.Now; - } - else - { - Items.Add(new HistoryItem - { - Query = query, - ExecutedDateTime = DateTime.Now - }); - } - } - } -}