Files
replicate.maui/Test.Replicate/Pages/HistoryPage.xaml.cs
2025-12-10 21:59:12 -05:00

143 lines
4.5 KiB
C#

using MarketAlly.Replicate.Maui;
using Test.Replicate.Services;
namespace Test.Replicate.Pages
{
public partial class HistoryPage : ContentPage
{
private readonly HistoryService _historyService;
public HistoryPage()
{
InitializeComponent();
_historyService = App.ServiceProvider.GetRequiredService<HistoryService>();
_historyService.HistoryChanged += OnHistoryChanged;
}
protected override void OnAppearing()
{
base.OnAppearing();
RefreshHistory();
}
private void OnHistoryChanged()
{
MainThread.BeginInvokeOnMainThread(RefreshHistory);
}
private void RefreshHistory()
{
var items = _historyService.GetHistory()
.Select(p => new PredictionViewModel(p))
.ToList();
HistoryList.ItemsSource = items;
var pending = items.Count(i => i.IsPending);
var succeeded = items.Count(i => i.IsSucceeded);
var failed = items.Count(i => i.IsFailed);
if (items.Count == 0)
{
SummaryLabel.Text = "No predictions yet";
}
else
{
var parts = new List<string>();
if (pending > 0) parts.Add($"{pending} pending");
if (succeeded > 0) parts.Add($"{succeeded} succeeded");
if (failed > 0) parts.Add($"{failed} failed");
SummaryLabel.Text = $"{items.Count} predictions: {string.Join(", ", parts)}";
}
}
private void OnRefreshClicked(object? sender, EventArgs e)
{
RefreshHistory();
}
private void OnClearCompletedClicked(object? sender, EventArgs e)
{
_historyService.ClearCompleted();
RefreshHistory();
}
private void OnClearAllClicked(object? sender, EventArgs e)
{
_historyService.ClearAll();
RefreshHistory();
}
}
public class PredictionViewModel
{
private readonly TrackedPrediction _prediction;
public PredictionViewModel(TrackedPrediction prediction)
{
_prediction = prediction;
}
public string Id => _prediction.Id;
public string TypeDisplay => _prediction.Type == TransformationType.Image ? "Image Transform" : "Video Generation";
public string StatusDisplay => _prediction.StatusEnum switch
{
PredictionStatus.Starting => "Starting...",
PredictionStatus.Processing => "Processing...",
PredictionStatus.Succeeded => "Completed",
PredictionStatus.Failed => $"Failed: {_prediction.Error ?? "Unknown error"}",
PredictionStatus.Canceled => "Canceled",
_ => _prediction.Status
};
public bool IsPending => _prediction.IsPending;
public bool IsSucceeded => _prediction.IsSucceeded;
public bool IsFailed => _prediction.IsFailed;
public Color StatusColor => _prediction.StatusEnum switch
{
PredictionStatus.Starting => Colors.Orange,
PredictionStatus.Processing => Colors.Blue,
PredictionStatus.Succeeded => Colors.Green,
PredictionStatus.Failed => Colors.Red,
PredictionStatus.Canceled => Colors.Gray,
_ => Colors.Gray
};
public Color StatusTextColor => _prediction.StatusEnum switch
{
PredictionStatus.Starting => Colors.Orange,
PredictionStatus.Processing => Colors.Blue,
PredictionStatus.Succeeded => Colors.Green,
PredictionStatus.Failed => Colors.Red,
PredictionStatus.Canceled => Colors.Gray,
_ => Colors.Gray
};
public string TimeDisplay => _prediction.CreatedAt.ToLocalTime().ToString("HH:mm:ss");
public string ElapsedDisplay
{
get
{
var elapsed = _prediction.Elapsed;
if (elapsed.TotalMinutes >= 1)
return $"Elapsed: {elapsed.Minutes}m {elapsed.Seconds}s";
return $"Elapsed: {elapsed.TotalSeconds:F1}s";
}
}
public string OutputStatus
{
get
{
if (_prediction.IsSucceeded && _prediction.Output != null)
return "Output ready";
if (_prediction.IsPending)
return "In progress...";
return "";
}
}
}
}