Skip to content

Commit

Permalink
Don't auto save if prompt cancelled
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Aug 27, 2024
1 parent 33595be commit a716ae9
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 6 deletions.
47 changes: 47 additions & 0 deletions NAPS2.Lib.Tests/Scan/AutoSaverTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,53 @@ public async Task PdfSplitPatchT()
PdfAsserts.AssertImages(Path.Combine(FolderPath, "test2.pdf"), ImageResources.dog_h_n300);
}

[Fact]
public async Task PromptForFilePath()
{
var settings = new AutoSaveSettings
{
FilePath = Path.Combine(FolderPath, "test_a_$(n).pdf"),
PromptForFilePath = true
};
_dialogHelper.PromptToSavePdfOrImage(Arg.Any<string>(), out Arg.Any<string>()).Returns(x =>
{
x[1] = Path.Combine(FolderPath, "test_b_$(n).pdf");
return true;
});

var scanned = CreateScannedImages(ImageResources.dog);
var output = await _autoSaver.Save(settings, scanned.ToAsyncEnumerable()).ToListAsync();

Assert.Single(output);
Assert.False(IsDisposed(output[0]));
Assert.True(IsDisposed(scanned[0]));
Assert.Single(Folder.GetFiles());
PdfAsserts.AssertImages(Path.Combine(FolderPath, "test_b_1.pdf"), ImageResources.dog);
}

[Fact]
public async Task CancelPromptForFilePath()
{
var settings = new AutoSaveSettings
{
FilePath = Path.Combine(FolderPath, "test$(n).pdf"),
PromptForFilePath = true
};
_dialogHelper.PromptToSavePdfOrImage(Arg.Any<string>(), out Arg.Any<string>()).Returns(x =>
{
x[1] = Path.Combine(FolderPath, "test$(n).pdf");
return false;
});

var scanned = CreateScannedImages(ImageResources.dog);
var output = await _autoSaver.Save(settings, scanned.ToAsyncEnumerable()).ToListAsync();

Assert.Single(output);
Assert.False(IsDisposed(output[0]));
Assert.True(IsDisposed(scanned[0]));
Assert.Empty(Folder.GetFiles());
}

// TODO: Finish out tests

//
Expand Down
14 changes: 8 additions & 6 deletions NAPS2.Lib/ImportExport/AutoSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,13 +117,15 @@ private async Task<bool> InternalSave(AutoSaveSettings settings, List<ProcessedI
string subPath = placeholders.Substitute(settings.FilePath, true, i);
if (settings.PromptForFilePath)
{
Invoker.Current.Invoke(() =>
string? newPath = null!;
if (Invoker.Current.InvokeGet(() => _dialogHelper.PromptToSavePdfOrImage(subPath, out newPath)))
{
if (_dialogHelper.PromptToSavePdfOrImage(subPath, out string? newPath))
{
subPath = placeholders.Substitute(newPath!, true, i);
}
});
subPath = placeholders.Substitute(newPath!, true, i);
}
else
{
return (false, null);
}
}
// TODO: This placeholder handling is complex and wrong in some cases (e.g. FilePerScan with ext = "jpg")
// TODO: Maybe have initial placeholders that replace date, then rely on the ops to increment the file num
Expand Down

0 comments on commit a716ae9

Please sign in to comment.