Skip to content

Commit

Permalink
Optimize session loading
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Jan 15, 2024
1 parent 3588a92 commit f83aae5
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 12 deletions.
9 changes: 3 additions & 6 deletions NAPS2.Lib/EtoForms/Desktop/DesktopController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -306,13 +306,10 @@ private void ShowRecoveryPrompt()
AutoSessionRestore = _config.Get(c => c.KeepSession),
ThumbnailSize = _thumbnailController.RenderSize
};
if (op.Start(_desktopImagesController.ReceiveScannedImage(), recoveryParams))
if (op.Start(_desktopImagesController.ReceiveScannedImage(), _desktopImagesController.AppendImageBatch,
recoveryParams))
{
if (recoveryParams.AutoSessionRestore)
{
_operationProgress.ShowBackgroundProgress(op);
}
else
if (!recoveryParams.AutoSessionRestore)
{
_operationProgress.ShowProgress(op);
}
Expand Down
7 changes: 7 additions & 0 deletions NAPS2.Lib/EtoForms/Desktop/DesktopImagesController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,11 @@ public Action<ProcessedImage> ReceiveScannedImage()
}
};
}

public void AppendImageBatch(IEnumerable<ProcessedImage> images)
{
_imageList.Mutate(
new ImageListMutation.Append(images.Select(image => new UiImage(image))),
isPassiveInteraction: true);
}
}
12 changes: 12 additions & 0 deletions NAPS2.Lib/Operation/OperationBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,18 @@ protected void RunAsync(Func<bool> action)
StartTask(() => Task.FromResult(action()));
}

protected void RunSync(Func<bool> action)
{
try
{
_tcs.TrySetResult(action());
}
catch (Exception ex)
{
_tcs.TrySetException(ex);
}
}

private void StartTask(Func<Task<bool>> action)
{
Task.Run(async () =>
Expand Down
19 changes: 16 additions & 3 deletions NAPS2.Lib/Recovery/RecoverableFolder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -102,15 +102,17 @@ public void TryDelete()
}
}

public bool TryRecover(Action<ProcessedImage> imageCallback, RecoveryParams recoveryParams,
ProgressHandler progress)
public bool TryRecover(Action<ProcessedImage> imageCallback, Action<IEnumerable<ProcessedImage>> imageBatchCallback,
RecoveryParams recoveryParams, ProgressHandler progress)
{
if (_disposed) throw new ObjectDisposedException(nameof(RecoverableFolder));

int currentProgress = 0;
int totalProgress = ImageCount;
progress.Report(currentProgress, totalProgress);

var recoveredImages = new List<ProcessedImage>();

foreach (RecoveryIndexImage indexImage in _recoveryIndex.Images)
{
if (progress.IsCancellationRequested)
Expand Down Expand Up @@ -145,11 +147,22 @@ public bool TryRecover(Action<ProcessedImage> imageCallback, RecoveryParams reco

var storage = new ImageFileStorage(newPath);
var recoveredImage = CreateRecoveredImage(recoveryParams, storage, indexImage);
imageCallback(recoveredImage);
if (!recoveryParams.AutoSessionRestore)
{
imageCallback(recoveredImage);
}
else
{
recoveredImages.Add(recoveredImage);
}

currentProgress++;
progress.Report(currentProgress, totalProgress);
}
if (recoveryParams.AutoSessionRestore)
{
imageBatchCallback(recoveredImages);
}
// Now that we've recovered successfully, we can safely delete the old folder
TryDelete();
return true;
Expand Down
9 changes: 6 additions & 3 deletions NAPS2.Lib/Recovery/RecoveryOperation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ public RecoveryOperation(IFormFactory formFactory, RecoveryManager recoveryManag
AllowBackground = true;
}

public bool Start(Action<ProcessedImage> imageCallback, RecoveryParams recoveryParams)
public bool Start(Action<ProcessedImage> imageCallback, Action<IEnumerable<ProcessedImage>> imageBatchCallback,
RecoveryParams recoveryParams)
{
Status = new OperationStatus
{
Expand All @@ -35,11 +36,13 @@ public bool Start(Action<ProcessedImage> imageCallback, RecoveryParams recoveryP
switch (recoveryParams.AutoSessionRestore ? RecoverAction.Recover : PromptToRecover(recoverableFolder))
{
case RecoverAction.Recover:
RunAsync(() =>
Action<Func<bool>> runFunc = recoveryParams.AutoSessionRestore ? RunSync : RunAsync;
runFunc(() =>
{
try
{
return recoverableFolder.TryRecover(imageCallback, recoveryParams, ProgressHandler);
return recoverableFolder.TryRecover(imageCallback, imageBatchCallback, recoveryParams,
ProgressHandler);
}
finally
{
Expand Down
7 changes: 7 additions & 0 deletions NAPS2.Lib/Recovery/RecoveryParams.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@

public class RecoveryParams
{
// In case the user has the "Keep images across sessions" option, we want to make the recovery operation feel more
// seamless. This means a few things:
// - Image files are moved instead of copied. This is destructive (higher risk of data loss) but fast.
// - Thumbnails rendering is deferred.
// - No operation progress is displayed.
// - The operation is run synchronously (with only moving files + no thumbnail rendering it should be trivially fast).
// - Images are sent back to the UI as a single batch (speeds up UI rendering).
public bool AutoSessionRestore { get; set; }

public int? ThumbnailSize { get; set; }
Expand Down

0 comments on commit f83aae5

Please sign in to comment.