Browse Source

Download fixes

pull/109/head
Ionite 1 year ago
parent
commit
05a09e273d
No known key found for this signature in database
  1. 4
      StabilityMatrix.Avalonia/ViewModels/DownloadProgressItemViewModel.cs
  2. 10
      StabilityMatrix.Core/Models/TrackedDownload.cs
  3. 2
      StabilityMatrix.Core/Services/DownloadService.cs
  4. 2
      StabilityMatrix.Core/Services/TrackedDownloadService.cs

4
StabilityMatrix.Avalonia/ViewModels/DownloadProgressItemViewModel.cs

@ -9,14 +9,12 @@ public class DownloadProgressItemViewModel : PausableProgressItemViewModelBase
{
private readonly TrackedDownload download;
/// <inheritdoc />
public override bool SupportsPauseResume => true;
public DownloadProgressItemViewModel(TrackedDownload download)
{
this.download = download;
Name = download.FileName;
State = download.ProgressState;
download.ProgressUpdate += (s, e) =>
{

10
StabilityMatrix.Core/Models/TrackedDownload.cs

@ -28,6 +28,7 @@ public class TrackedDownload
[JsonIgnore]
private CancellationTokenSource? downloadPauseTokenSource;
[JsonIgnore]
private CancellationTokenSource AggregateCancellationTokenSource =>
CancellationTokenSource.CreateLinkedTokenSource(
downloadCancellationTokenSource?.Token ?? CancellationToken.None,
@ -51,6 +52,7 @@ public class TrackedDownload
public ProgressState ProgressState { get; private set; } = ProgressState.Inactive;
[JsonIgnore]
public Exception? Exception { get; private set; }
#region Events
@ -134,6 +136,8 @@ public class TrackedDownload
downloadTask = StartDownloadTask(0, AggregateCancellationTokenSource.Token)
.ContinueWith(OnDownloadTaskCompleted);
ProgressState = ProgressState.Working;
}
public void Resume()
@ -147,6 +151,8 @@ public class TrackedDownload
downloadTask = StartDownloadTask(0, AggregateCancellationTokenSource.Token)
.ContinueWith(OnDownloadTaskCompleted);
ProgressState = ProgressState.Working;
}
public void Pause()
@ -232,10 +238,10 @@ public class TrackedDownload
OnProgressStateChanged(ProgressState);
// Dispose of the task and cancellation token
downloadTask?.Dispose();
/*downloadTask?.Dispose();
downloadTask = null;
downloadCancellationTokenSource?.Dispose();
downloadCancellationTokenSource = null;
downloadCancellationTokenSource = null;*/
}
public void SetDownloadService(IDownloadService service)

2
StabilityMatrix.Core/Services/DownloadService.cs

@ -171,6 +171,8 @@ public class DownloadService : IDownloadService
var buffer = new byte[BufferSize];
while (true)
{
cancellationToken.ThrowIfCancellationRequested();
var bytesRead = await stream.ReadAsync(buffer, cancellationToken).ConfigureAwait(false);
if (bytesRead == 0)
break;

2
StabilityMatrix.Core/Services/TrackedDownloadService.cs

@ -63,6 +63,7 @@ public class TrackedDownloadService : ITrackedDownloadService, IDisposable
// Serialize to json
var json = JsonSerializer.Serialize(download);
jsonFileStream.Write(Encoding.UTF8.GetBytes(json));
jsonFileStream.Flush();
// Add to dictionary
downloads.TryAdd(download.Id, (download, jsonFileStream));
@ -87,6 +88,7 @@ public class TrackedDownloadService : ITrackedDownloadService, IDisposable
var (_, fs) = downloads[download.Id];
fs.Seek(0, SeekOrigin.Begin);
fs.Write(jsonBytes);
fs.Flush();
}
/// <summary>

Loading…
Cancel
Save