Don't auto save if prompt cancelled

#438
This commit is contained in:
Ben Olden-Cooligan 2024-08-26 19:34:34 -07:00
parent 33595be9dc
commit a716ae979c
2 changed files with 55 additions and 6 deletions

View File

@ -199,6 +199,53 @@ public class AutoSaverTests : ContextualTests
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
//

View File

@ -117,13 +117,15 @@ public class AutoSaver
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