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; private readonly TrackedDownload download;
/// <inheritdoc />
public override bool SupportsPauseResume => true;
public DownloadProgressItemViewModel(TrackedDownload download) public DownloadProgressItemViewModel(TrackedDownload download)
{ {
this.download = download; this.download = download;
Name = download.FileName; Name = download.FileName;
State = download.ProgressState;
download.ProgressUpdate += (s, e) => download.ProgressUpdate += (s, e) =>
{ {

10
StabilityMatrix.Core/Models/TrackedDownload.cs

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

2
StabilityMatrix.Core/Services/DownloadService.cs

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

2
StabilityMatrix.Core/Services/TrackedDownloadService.cs

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

Loading…
Cancel
Save