feat: created a base History model

This commit is contained in:
01Dri 2025-10-07 00:33:16 -03:00
parent 7ba4f8de4a
commit e5736567f6
5 changed files with 140 additions and 137 deletions

View file

@ -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<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)
{
AddLastQuery(result);
return;
}
AddLastOpened(result);
}
public List<HistoryItem> GetHistoryItems(Settings settings)
{
if (settings.ShowHistoryQueryResultsForHomePage) return QueryHistoryItems;
if (settings.ShowHistoryLastOpenedResultsForHomePage) return LastOpenedHistoryItems;
return new List<HistoryItem>();
}
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);
}
}
}
}

View file

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

View file

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

View file

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

View file

@ -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<HistoryItem> Items { get; private set; } = new List<HistoryItem>();
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
});
}
}
}
}