OF-DL/OF DL.Gui/Services/AvaloniaDownloadEventHandler.cs

128 lines
4.3 KiB
C#

using OF_DL.Models.Downloads;
using OF_DL.Services;
namespace OF_DL.Gui.Services;
internal sealed class AvaloniaDownloadEventHandler(
Action<string> activitySink,
Action<string> progressStatusUpdate,
Action<string, long, bool> progressStart,
Action<long> progressIncrement,
Action progressStop,
Func<bool> isCancellationRequested,
CancellationToken cancellationToken) : IDownloadEventHandler
{
private string _lastProgressDescription = string.Empty;
public CancellationToken CancellationToken { get; } = cancellationToken;
public async Task<T> WithStatusAsync<T>(string statusMessage, Func<IStatusReporter, Task<T>> work)
{
ThrowIfCancellationRequested();
progressStart(statusMessage, 0, false);
try
{
AvaloniaStatusReporter statusReporter = new(progressStatusUpdate, isCancellationRequested);
return await work(statusReporter);
}
finally
{
progressStop();
}
}
public async Task<T> WithProgressAsync<T>(string description, long maxValue, bool showSize,
Func<IProgressReporter, Task<T>> work)
{
ThrowIfCancellationRequested();
_lastProgressDescription = description;
progressStart(description, maxValue, showSize);
try
{
AvaloniaProgressReporter reporter = new(progressIncrement, isCancellationRequested, CancellationToken);
return await work(reporter);
}
finally
{
progressStop();
}
}
public void OnContentFound(string contentType, int mediaCount, int objectCount)
{
ThrowIfCancellationRequested();
progressStatusUpdate($"Found {mediaCount} media from {objectCount} {contentType}.");
}
public void OnNoContentFound(string contentType)
{
ThrowIfCancellationRequested();
progressStatusUpdate($"Found 0 {contentType}.");
}
public void OnDownloadComplete(string contentType, DownloadResult result)
{
ThrowIfCancellationRequested();
progressStatusUpdate(
$"{contentType} complete. Existing: {result.ExistingDownloads}, New: {result.NewDownloads}, Total: {result.TotalCount}.");
}
public void OnUserStarting(string username)
{
ThrowIfCancellationRequested();
activitySink($"Starting scrape for {username}.");
progressStatusUpdate($"Scraping data for {username}...");
}
public void OnUserComplete(string username, CreatorDownloadResult result)
{
ThrowIfCancellationRequested();
activitySink(
$"Completed {username}. PaidPosts={result.PaidPostCount}, Posts={result.PostCount}, Archived={result.ArchivedCount}, Streams={result.StreamsCount}, Stories={result.StoriesCount}, Highlights={result.HighlightsCount}, Messages={result.MessagesCount}, PaidMessages={result.PaidMessagesCount}.");
}
public void OnPurchasedTabUserComplete(string username, int paidPostCount, int paidMessagesCount)
{
ThrowIfCancellationRequested();
activitySink($"Purchased tab complete for {username}. PaidPosts={paidPostCount}, PaidMessages={paidMessagesCount}.");
}
public void OnScrapeComplete(TimeSpan elapsed)
{
ThrowIfCancellationRequested();
string summary = BuildCompletionSummary(elapsed);
activitySink(summary);
}
public void OnMessage(string message)
{
ThrowIfCancellationRequested();
progressStatusUpdate(message);
}
private void ThrowIfCancellationRequested()
{
if (isCancellationRequested())
{
throw new OperationCanceledException("Operation canceled by user.");
}
}
private string BuildCompletionSummary(TimeSpan elapsed)
{
if (string.IsNullOrWhiteSpace(_lastProgressDescription))
{
return $"Download completed in {elapsed.TotalMinutes:0.0} minutes.";
}
string normalized = _lastProgressDescription.Trim().TrimEnd('.');
if (normalized.StartsWith("Downloading ", StringComparison.OrdinalIgnoreCase))
{
string remainder = normalized["Downloading ".Length..].ToLowerInvariant();
return $"Downloaded {remainder} in {elapsed.TotalMinutes:0.0} minutes.";
}
return $"{normalized} in {elapsed.TotalMinutes:0.0} minutes.";
}
}