Skip to content

Commit

Permalink
Modified pdf conversion to actually have correct progress
Browse files Browse the repository at this point in the history
  • Loading branch information
dusrdev committed Apr 24, 2023
1 parent 4e2d6eb commit 8f4b866
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
14 changes: 14 additions & 0 deletions PdfTool/Controller/ImageToPdfConvertAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
namespace PdfTool.Controller;

public readonly struct ImageToPdfConvertAction : IAction<string> {
private readonly ThreadSafe<double> _counter;
private static readonly IModifier<double> Adder = new Adder();
private readonly int _total;
private readonly IProgress<double> _progress;

public ImageToPdfConvertAction(ThreadSafe<double> counter, int total, IProgress<double> progress) {
_counter = counter;
_total = total;
_progress = progress;
}

public void Invoke(string input) {
using var document = new PdfDocument();
document.Info.Title = Path.GetFileNameWithoutExtension(input);
Expand All @@ -26,6 +37,9 @@ public void Invoke(string input) {
if (document.PageCount > 0) {
document.Save(resultPath);
}

var progress = Adder.Modify(_counter.Value, 1d) * 100d / _total;
_progress.Report(progress);
}

/// <summary>
Expand Down
8 changes: 6 additions & 2 deletions PdfTool/Controller/ImageToPdfConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,12 @@ static ImageToPdfConverter() {
/// Batch converts images to pdfs and saves them to the same directory as the first file
/// </summary>
/// <param name="filePaths"></param>
public static Result ConvertImages(string[] filePaths) {
var converter = new ImageToPdfConvertAction();
public static Result ConvertImages(string[] filePaths, IProgress<double> progress) {
var counter = new ThreadSafe<double>(0d);
var converter = new ImageToPdfConvertAction(
counter,
filePaths.Length,
progress);
filePaths.Concurrent().ForEach(converter);

return Result.Ok("Conversion successful.");
Expand Down

0 comments on commit 8f4b866

Please sign in to comment.