Skip to content

Commit

Permalink
refactor sample Program.cs
Browse files Browse the repository at this point in the history
  • Loading branch information
bezzad committed Aug 27, 2024
1 parent ffb5fbd commit 6cb65e9
Showing 1 changed file with 25 additions and 14 deletions.
39 changes: 25 additions & 14 deletions src/Samples/Downloader.Sample/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ private static void KeyboardHandler()
CancelAll();
return;
}

break;
case ConsoleKey.P:
CurrentDownloadService?.Pause();
Expand Down Expand Up @@ -149,7 +150,7 @@ private static async Task DownloadAll(IEnumerable<DownloadItem> downloadList, Ca
}
}

private static async Task<IDownloadService> DownloadFile(DownloadItem downloadItem)
private static async Task DownloadFile(DownloadItem downloadItem)
{
CurrentDownloadConfiguration = GetDownloadConfiguration();
CurrentDownloadService = CreateDownloadService(CurrentDownloadConfiguration);
Expand Down Expand Up @@ -181,8 +182,6 @@ await ValidateDataAsync(CurrentDownloadService.Package.FileName,
throw new InvalidDataException(message);
}
}

return CurrentDownloadService;
}

private static async Task<bool> ValidateDataAsync(string filename, long size)
Expand Down Expand Up @@ -222,15 +221,15 @@ private static DownloadService CreateDownloadService(DownloadConfiguration confi
// Provide `FileName` and `TotalBytesToReceive` at the start of each downloads
downloadService.DownloadStarted += OnDownloadStarted;

// Provide any information about chunker downloads,
// Provide any information about chunk downloads,
// like progress percentage per chunk, speed,
// total received bytes and received bytes array to live streaming.
// total received bytes and received bytes array to live-streaming.
downloadService.ChunkDownloadProgressChanged += OnChunkDownloadProgressChanged;

// Provide any information about download progress,
// like progress percentage of sum of chunks, total speed,
// average speed, total received bytes and received bytes array
// to live streaming.
// to live-streaming.
downloadService.DownloadProgressChanged += OnDownloadProgressChanged;

// Download completed event that can include occurred errors or
Expand All @@ -243,26 +242,29 @@ private static DownloadService CreateDownloadService(DownloadConfiguration confi
private static async void OnDownloadStarted(object sender, DownloadStartedEventArgs e)
{
await WriteKeyboardGuidLines();
ConsoleProgress = new ProgressBar(10000, $"Downloading {Path.GetFileName(e.FileName)} ", ProcessBarOption);
var progressMsg = $"Downloading {Path.GetFileName(e.FileName)} ";
await Console.Out.WriteLineAsync(progressMsg);
ConsoleProgress = new ProgressBar(10000, progressMsg, ProcessBarOption);
}

private static void OnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
ConsoleProgress?.Tick(10000);
var lastState = " DONE";

if (e.Cancelled)
{
ConsoleProgress.Message += " CANCELED";
lastState = " CANCELED";
}
else if (e.Error != null)
{
lastState = " ERROR";
Console.Error.WriteLine(e.Error);
Debugger.Break();
}
else
{
ConsoleProgress.Message += " DONE";
}

if (ConsoleProgress != null)
ConsoleProgress.Message += lastState;

foreach (var child in ChildConsoleProgresses.Values)
child.Dispose();
Expand All @@ -276,13 +278,22 @@ private static void OnChunkDownloadProgressChanged(object sender, DownloadProgre
ChildProgressBar progress = ChildConsoleProgresses.GetOrAdd(e.ProgressId,
id => ConsoleProgress?.Spawn(10000, $"chunk {id}", ChildOption));
progress.Tick((int)(e.ProgressPercentage * 100));
var activeChunksCount = e.ActiveChunks; // Running chunks count
// var activeChunksCount = e.ActiveChunks; // Running chunks count
}

private static void OnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
ConsoleProgress.Tick((int)(e.ProgressPercentage * 100));
if (sender is DownloadService ds)
e.UpdateTitleInfo(ds.IsPaused);
{
if (OperatingSystem.IsWindows())
{
Console.Title = e.UpdateTitleInfo(ds.IsPaused);

Check failure on line 291 in src/Samples/Downloader.Sample/Program.cs

View workflow job for this annotation

GitHub Actions / build

Cannot implicitly convert type 'void' to 'string'

Check failure on line 291 in src/Samples/Downloader.Sample/Program.cs

View workflow job for this annotation

GitHub Actions / build

Cannot implicitly convert type 'void' to 'string'

Check failure on line 291 in src/Samples/Downloader.Sample/Program.cs

View workflow job for this annotation

GitHub Actions / build

Cannot implicitly convert type 'void' to 'string'

Check failure on line 291 in src/Samples/Downloader.Sample/Program.cs

View workflow job for this annotation

GitHub Actions / build

Cannot implicitly convert type 'void' to 'string'
}
else
{
ConsoleProgress.Message = e.UpdateTitleInfo(ds.IsPaused);

Check failure on line 295 in src/Samples/Downloader.Sample/Program.cs

View workflow job for this annotation

GitHub Actions / build

Cannot implicitly convert type 'void' to 'string'

Check failure on line 295 in src/Samples/Downloader.Sample/Program.cs

View workflow job for this annotation

GitHub Actions / build

Cannot implicitly convert type 'void' to 'string'

Check failure on line 295 in src/Samples/Downloader.Sample/Program.cs

View workflow job for this annotation

GitHub Actions / build

Cannot implicitly convert type 'void' to 'string'

Check failure on line 295 in src/Samples/Downloader.Sample/Program.cs

View workflow job for this annotation

GitHub Actions / build

Cannot implicitly convert type 'void' to 'string'
}
}
}
}

0 comments on commit 6cb65e9

Please sign in to comment.