95 lines
2.6 KiB
C#
95 lines
2.6 KiB
C#
using MarketAlly.Replicate.Maui;
|
|
|
|
namespace Test.Replicate.Services
|
|
{
|
|
/// <summary>
|
|
/// Shared service for tracking prediction history across pages.
|
|
/// </summary>
|
|
public class HistoryService
|
|
{
|
|
private readonly List<TrackedPrediction> _history = new();
|
|
private readonly object _lock = new();
|
|
|
|
/// <summary>
|
|
/// Raised when history changes (prediction added or status updated).
|
|
/// </summary>
|
|
public event Action? HistoryChanged;
|
|
|
|
/// <summary>
|
|
/// Add or update a prediction in history.
|
|
/// </summary>
|
|
public void TrackPrediction(TrackedPrediction prediction)
|
|
{
|
|
lock (_lock)
|
|
{
|
|
var existing = _history.FirstOrDefault(p => p.Id == prediction.Id);
|
|
if (existing != null)
|
|
{
|
|
// Update existing
|
|
var index = _history.IndexOf(existing);
|
|
_history[index] = prediction;
|
|
}
|
|
else
|
|
{
|
|
// Add new
|
|
_history.Insert(0, prediction);
|
|
|
|
// Trim to max 50 items
|
|
while (_history.Count > 50)
|
|
{
|
|
_history.RemoveAt(_history.Count - 1);
|
|
}
|
|
}
|
|
}
|
|
|
|
HistoryChanged?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get all tracked predictions ordered by creation time (newest first).
|
|
/// </summary>
|
|
public IReadOnlyList<TrackedPrediction> GetHistory()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return _history.OrderByDescending(p => p.CreatedAt).ToList();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Get pending predictions only.
|
|
/// </summary>
|
|
public IReadOnlyList<TrackedPrediction> GetPending()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
return _history.Where(p => p.IsPending).OrderByDescending(p => p.CreatedAt).ToList();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear completed predictions.
|
|
/// </summary>
|
|
public void ClearCompleted()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
_history.RemoveAll(p => p.IsCompleted);
|
|
}
|
|
HistoryChanged?.Invoke();
|
|
}
|
|
|
|
/// <summary>
|
|
/// Clear all predictions.
|
|
/// </summary>
|
|
public void ClearAll()
|
|
{
|
|
lock (_lock)
|
|
{
|
|
_history.Clear();
|
|
}
|
|
HistoryChanged?.Invoke();
|
|
}
|
|
}
|
|
}
|