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.
87 lines
2.2 KiB
87 lines
2.2 KiB
1 year ago
|
using System;
|
||
|
using System.Threading.Tasks;
|
||
1 year ago
|
using StabilityMatrix.Avalonia.ViewModels.Base;
|
||
|
using StabilityMatrix.Core.Models;
|
||
|
using StabilityMatrix.Core.Models.Progress;
|
||
|
|
||
1 year ago
|
namespace StabilityMatrix.Avalonia.ViewModels.Progress;
|
||
1 year ago
|
|
||
|
public class DownloadProgressItemViewModel : PausableProgressItemViewModelBase
|
||
|
{
|
||
|
private readonly TrackedDownload download;
|
||
|
|
||
1 year ago
|
public DownloadProgressItemViewModel(TrackedDownload download)
|
||
1 year ago
|
{
|
||
|
this.download = download;
|
||
|
|
||
1 year ago
|
Id = download.Id;
|
||
1 year ago
|
Name = download.FileName;
|
||
1 year ago
|
State = download.ProgressState;
|
||
1 year ago
|
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);
|
||
|
}
|
||
1 year ago
|
|
||
1 year ago
|
download.ProgressUpdate += (s, e) =>
|
||
|
{
|
||
|
Progress.Value = e.Percentage;
|
||
|
Progress.IsIndeterminate = e.IsIndeterminate;
|
||
|
};
|
||
|
|
||
|
download.ProgressStateChanged += (s, e) =>
|
||
|
{
|
||
|
State = e;
|
||
1 year ago
|
OnProgressStateChanged(e);
|
||
1 year ago
|
};
|
||
|
}
|
||
|
|
||
1 year ago
|
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";
|
||
|
}
|
||
|
}
|
||
|
|
||
1 year ago
|
/// <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;
|
||
|
}
|
||
|
}
|