Extract save separator logic into a helper class

This commit is contained in:
Ben Olden-Cooligan 2016-02-09 22:10:08 -05:00
parent 8cb6949cfd
commit 2961dff8be
9 changed files with 99 additions and 84 deletions

View File

@ -50,46 +50,10 @@ namespace NAPS2.ImportExport
{
bool ok = true;
DateTime now = DateTime.Now;
if (settings.Separator == SaveSeparator.FilePerScan)
int i = 0;
foreach (var imageList in SaveSeparatorHelper.SeparateScans(new [] { images }, settings.Separator))
{
if (!SaveOneFile(settings, now, 0, images))
{
ok = false;
}
}
else if (settings.Separator == SaveSeparator.FilePerPage)
{
for (int i = 0; i < images.Count; i++)
{
if (!SaveOneFile(settings, now, i, new List<ScannedImage> { images[i] }))
{
ok = false;
}
}
}
else if (settings.Separator == SaveSeparator.PatchT)
{
var imageSet = new List<ScannedImage>();
int fileIndex = 0;
foreach (ScannedImage img in images)
{
if (img.PatchCode == PatchCode.PatchT)
{
if (imageSet.Count > 0)
{
if (!SaveOneFile(settings, now, fileIndex++, imageSet))
{
ok = false;
}
imageSet.Clear();
}
}
else
{
imageSet.Add(img);
}
}
if (!SaveOneFile(settings, now, fileIndex, imageSet))
if (!SaveOneFile(settings, now, i++, imageList))
{
ok = false;
}

View File

@ -2,10 +2,11 @@
using System.Collections.Generic;
using System.Linq;
namespace NAPS2.Scan
namespace NAPS2.ImportExport
{
public enum SaveSeparator
{
None,
FilePerPage,
FilePerScan,
PatchT

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAPS2.Scan;
using NAPS2.Scan.Images;
namespace NAPS2.ImportExport
{
public static class SaveSeparatorHelper
{
/// <summary>
/// Given a list of scans (each of which is a list of 1 or more images),
/// split up the images into multiple lists as described by the SaveSeparator parameter.
/// </summary>
/// <param name="scans"></param>
/// <param name="separator"></param>
/// <returns></returns>
public static IEnumerable<List<ScannedImage>> SeparateScans(IEnumerable<IEnumerable<ScannedImage>> scans, SaveSeparator separator)
{
if (separator == SaveSeparator.FilePerScan)
{
foreach (var scan in scans)
{
yield return scan.ToList();
}
}
else if (separator == SaveSeparator.FilePerPage)
{
foreach (var scan in scans)
{
foreach (var image in scan)
{
yield return new List<ScannedImage> { image };
}
}
}
else if (separator == SaveSeparator.PatchT)
{
var images = new List<ScannedImage>();
foreach (var scan in scans)
{
foreach (var image in scan)
{
if (image.PatchCode == PatchCode.PatchT)
{
if (images.Count > 0)
{
yield return images;
foreach (var img2 in images)
{
img2.Dispose();
}
images = new List<ScannedImage>();
}
}
else
{
images.Add(image);
}
}
if (images.Count > 0)
{
yield return images;
}
}
}
else
{
yield return scans.SelectMany(x => x.ToList()).ToList();
}
}
}
}

View File

@ -94,10 +94,11 @@
<Compile Include="ImportExport\IAutoSave.cs" />
<Compile Include="ImportExport\ImportOperation.cs" />
<Compile Include="ImportExport\Pdf\SavePdfOperation.cs" />
<Compile Include="ImportExport\SaveSeparatorHelper.cs" />
<Compile Include="Operation\IOperationFactory.cs" />
<Compile Include="Operation\OperationBase.cs" />
<Compile Include="Recovery\RecoveryImage.cs" />
<Compile Include="Scan\SaveSeparator.cs" />
<Compile Include="ImportExport\SaveSeparator.cs" />
<Compile Include="Scan\Batch\BatchScanPerformer.cs" />
<Compile Include="Scan\Batch\BatchSettings.cs" />
<Compile Include="Scan\Exceptions\NoDuplexSupportException.cs" />

View File

@ -262,50 +262,11 @@ namespace NAPS2.Scan.Batch
}
else if (Settings.OutputType == BatchOutputType.MultipleFiles)
{
if (Settings.SaveSeparator == SaveSeparator.FilePerScan)
int i = 0;
foreach (var imageList in SaveSeparatorHelper.SeparateScans(scans, Settings.SaveSeparator))
{
for (int i = 0; i < scans.Count; i++)
{
Save(now, i, scans[i]);
foreach (var img in scans[i])
{
img.Dispose();
}
}
}
else if (Settings.SaveSeparator == SaveSeparator.FilePerPage)
{
for (int i = 0; i < allImages.Count; i++)
{
Save(now, i, new List<ScannedImage> { allImages[i] });
allImages[i].Dispose();
}
}
else if (Settings.SaveSeparator == SaveSeparator.PatchT)
{
var images = new List<ScannedImage>();
int fileIndex = 0;
foreach (ScannedImage img in allImages)
{
if (img.PatchCode == PatchCode.PatchT)
{
if (images.Count > 0)
{
Save(now, fileIndex++, images);
foreach (var img2 in images)
{
img2.Dispose();
}
images.Clear();
}
}
else
{
images.Add(img);
}
}
Save(now, fileIndex, images);
foreach (var img in images)
Save(now, i++, imageList);
foreach (var img in imageList)
{
img.Dispose();
}

View File

@ -2,11 +2,17 @@
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NAPS2.ImportExport;
namespace NAPS2.Scan.Batch
{
public class BatchSettings
{
public BatchSettings()
{
SaveSeparator = SaveSeparator.FilePerPage;
}
public string ProfileDisplayName { get; set; }
public BatchScanType ScanType { get; set; }

View File

@ -24,6 +24,7 @@ using System.ComponentModel;
using System.Globalization;
using System.Linq;
using System.Xml.Serialization;
using NAPS2.ImportExport;
using NAPS2.Lang.Resources;
namespace NAPS2.Scan
@ -110,6 +111,11 @@ namespace NAPS2.Scan
public class AutoSaveSettings
{
public AutoSaveSettings()
{
Separator = SaveSeparator.FilePerPage;
}
internal AutoSaveSettings Clone()
{
return (AutoSaveSettings) MemberwiseClone();

View File

@ -23,6 +23,7 @@ using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Windows.Forms;
using NAPS2.ImportExport;
using NAPS2.Lang.Resources;
using NAPS2.Scan;
using NAPS2.Scan.Exceptions;

View File

@ -27,6 +27,7 @@ using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NAPS2.Config;
using NAPS2.ImportExport;
using NAPS2.Lang.Resources;
using NAPS2.Scan;
using NAPS2.Scan.Batch;