You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
86 lines
2.2 KiB
86 lines
2.2 KiB
using System; |
|
using System.Threading.Tasks; |
|
using StabilityMatrix.Avalonia.ViewModels.Base; |
|
using StabilityMatrix.Core.Models; |
|
using StabilityMatrix.Core.Models.Progress; |
|
|
|
namespace StabilityMatrix.Avalonia.ViewModels; |
|
|
|
public class DownloadProgressItemViewModel : PausableProgressItemViewModelBase |
|
{ |
|
private readonly TrackedDownload download; |
|
|
|
public DownloadProgressItemViewModel(TrackedDownload download) |
|
{ |
|
this.download = download; |
|
|
|
Id = download.Id; |
|
Name = download.FileName; |
|
State = download.ProgressState; |
|
OnProgressStateChanged(State); |
|
|
|
// If initial progress provided, load it |
|
if (download is {TotalBytes: > 0, DownloadedBytes: > 0}) |
|
{ |
|
var current = download.DownloadedBytes / (double) download.TotalBytes; |
|
Progress.Value = (float) Math.Ceiling(Math.Clamp(current, 0, 1) * 100); |
|
} |
|
|
|
download.ProgressUpdate += (s, e) => |
|
{ |
|
Progress.Value = e.Percentage; |
|
Progress.IsIndeterminate = e.IsIndeterminate; |
|
}; |
|
|
|
download.ProgressStateChanged += (s, e) => |
|
{ |
|
State = e; |
|
OnProgressStateChanged(e); |
|
}; |
|
} |
|
|
|
private void OnProgressStateChanged(ProgressState state) |
|
{ |
|
if (state == ProgressState.Inactive) |
|
{ |
|
Progress.Text = "Paused"; |
|
} |
|
else if (state == ProgressState.Working) |
|
{ |
|
Progress.Text = "Downloading..."; |
|
} |
|
else if (state == ProgressState.Success) |
|
{ |
|
Progress.Text = "Completed"; |
|
} |
|
else if (state == ProgressState.Cancelled) |
|
{ |
|
Progress.Text = "Cancelled"; |
|
} |
|
else if (state == ProgressState.Failed) |
|
{ |
|
Progress.Text = "Failed"; |
|
} |
|
} |
|
|
|
/// <inheritdoc /> |
|
public override Task Cancel() |
|
{ |
|
download.Cancel(); |
|
return Task.CompletedTask; |
|
} |
|
|
|
/// <inheritdoc /> |
|
public override Task Pause() |
|
{ |
|
download.Pause(); |
|
return Task.CompletedTask; |
|
} |
|
|
|
/// <inheritdoc /> |
|
public override Task Resume() |
|
{ |
|
download.Resume(); |
|
return Task.CompletedTask; |
|
} |
|
}
|
|
|