LANCommander/LANCommander.Client/UI/Components/CurrentDownloadItem.razor

88 lines
3 KiB
Text
Raw Permalink Normal View History

@using LANCommander.Client.Models
@using System.Diagnostics
@inject DownloadService DownloadService
@inject SDK.Client Client
@inject GameService GameService
@if (CurrentQueueItem != null)
{
<div class="downloader-current">
@if (ShowIcon)
{
<div class="downloader-current-icon">
<MediaImage Id="@CurrentQueueItem.IconId" />
</div>
}
<div class="downloader-current-info">
<div class="downloader-current-upper">
<div class="downloader-current-upper-status">
@CurrentQueueItem.Status: @CurrentQueueItem.Progress.ToString("0%")
</div>
@if (CurrentQueueItem.TransferSpeed > 0)
{
<div class="downloader-current-upper-time-remaining">
@TimeSpan.FromSeconds((CurrentQueueItem.TotalBytes - CurrentQueueItem.BytesDownloaded) / CurrentQueueItem.TransferSpeed).ToShortTime()
</div>
}
</div>
<div class="downloader-current-progress">
<Progress Percent="(CurrentQueueItem.Progress * 100)" />
</div>
<div class="downloader-current-lower">
<div class="downloader-current-lower-data">
@ByteSizeLib.ByteSize.FromBytes(CurrentQueueItem.BytesDownloaded).ToString() / @ByteSizeLib.ByteSize.FromBytes(CurrentQueueItem.TotalBytes).ToString()
</div>
</div>
</div>
</div>
}
@code {
[Parameter] public bool ShowIcon { get; set; }
IDownloadQueueItem CurrentQueueItem = null;
Stopwatch Stopwatch = new Stopwatch();
protected override async Task OnInitializedAsync()
{
Client.Games.OnArchiveExtractionProgress += (long position, long length, SDK.Models.Game game) =>
{
if (!Stopwatch.IsRunning)
Stopwatch.Start();
if (Stopwatch.ElapsedMilliseconds > 500)
{
CurrentQueueItem = DownloadService.Queue.OrderByDescending(qi => qi.QueuedOn).FirstOrDefault(qi => qi.State);
var bytesThisInterval = position - CurrentQueueItem.BytesDownloaded;
CurrentQueueItem.BytesDownloaded = position;
CurrentQueueItem.TotalBytes = length;
CurrentQueueItem.TransferSpeed = (double)(bytesThisInterval / (Stopwatch.ElapsedMilliseconds / 1000d));
InvokeAsync(StateHasChanged);
Stopwatch.Reset();
}
};
Client.Games.OnArchiveEntryExtractionProgress += (object sender, SDK.ArchiveEntryExtractionProgressArgs e) =>
{
// InvokeAsync(StateHasChanged);
};
DownloadService.OnInstallComplete += async () =>
{
Stopwatch.Stop();
};
DownloadService.OnQueueChanged += async () =>
{
CurrentQueueItem = DownloadService.Queue.OrderByDescending(qi => qi.QueuedOn).FirstOrDefault(qi => qi.State);
await InvokeAsync(StateHasChanged);
};
}
}