From 76ef1123b15e0bfc37ebf840574222ca36cc994f Mon Sep 17 00:00:00 2001 From: KatterMaw Date: Wed, 13 Sep 2023 15:57:51 +0500 Subject: [PATCH] Remove exceed check in DbScreenshotImageLoader; Delete exceed screenshots in more optimized way; Improve(?) image loading; --- .../Annotating/AutoAnnotationViewModel.cs | 6 +++--- .../Annotating/ScreenshotViewModel.cs | 11 ++--------- .../Annotating/AnnotatorScreenshots.axaml | 2 +- .../Views/Annotating/DetectorDrawer.axaml | 2 +- .../Services/DbScreenshotImageLoader.cs | 18 ++++++++++++------ .../Screenshots/ScreenshotsLibrary.cs | 16 +++++++++++++--- 6 files changed, 32 insertions(+), 23 deletions(-) diff --git a/SightKeeper.Avalonia/ViewModels/Annotating/AutoAnnotationViewModel.cs b/SightKeeper.Avalonia/ViewModels/Annotating/AutoAnnotationViewModel.cs index 40be2b63..f62c28f8 100644 --- a/SightKeeper.Avalonia/ViewModels/Annotating/AutoAnnotationViewModel.cs +++ b/SightKeeper.Avalonia/ViewModels/Annotating/AutoAnnotationViewModel.cs @@ -66,9 +66,9 @@ public AutoAnnotationViewModel(Detector detector, SelectedScreenshotViewModel se private async Task Annotate(CancellationToken cancellationToken) { Guard.IsNotNull(_selectedScreenshotViewModel.Value); - var screenshotContent = _selectedScreenshotViewModel.Value.Content; - var content = await screenshotContent; - var items = await _detector.Detect(content, cancellationToken); + var screenshotContent = _selectedScreenshotViewModel.Value.Image; + var image = await screenshotContent; + var items = await _detector.Detect(image.Content, cancellationToken); _selectedScreenshotViewModel.DetectedItems.Clear(); _selectedScreenshotViewModel.DetectedItems.AddRange(items.Select(CreateDetectedItemViewModel)); ClearCommand.NotifyCanExecuteChanged(); diff --git a/SightKeeper.Avalonia/ViewModels/Annotating/ScreenshotViewModel.cs b/SightKeeper.Avalonia/ViewModels/Annotating/ScreenshotViewModel.cs index 79f748c9..05256ca8 100644 --- a/SightKeeper.Avalonia/ViewModels/Annotating/ScreenshotViewModel.cs +++ b/SightKeeper.Avalonia/ViewModels/Annotating/ScreenshotViewModel.cs @@ -1,9 +1,8 @@ using System; -using System.Reactive.Linq; -using System.Reactive.Threading.Tasks; using System.Threading.Tasks; using SightKeeper.Application.Annotating; using SightKeeper.Domain.Model; +using SightKeeper.Domain.Model.Common; namespace SightKeeper.Avalonia.ViewModels.Annotating; @@ -11,13 +10,7 @@ public sealed class ScreenshotViewModel : ViewModel { public Screenshot Item { get; } - public Task Content - { - get - { - return _imageLoader.LoadAsync(Item).ToObservable().Select(item => item.Content).ToTask(); - } - } + public Task Image => _imageLoader.LoadAsync(Item); public bool IsAsset => Item.Asset != null; diff --git a/SightKeeper.Avalonia/Views/Annotating/AnnotatorScreenshots.axaml b/SightKeeper.Avalonia/Views/Annotating/AnnotatorScreenshots.axaml index 6db3bf3a..e6ba0854 100644 --- a/SightKeeper.Avalonia/Views/Annotating/AnnotatorScreenshots.axaml +++ b/SightKeeper.Avalonia/Views/Annotating/AnnotatorScreenshots.axaml @@ -35,7 +35,7 @@ - - + s.Image).Load(); + entry.Reference(s => s.Image).Load(); Guard.IsNotNull(screenshot.Image); return screenshot.Image; } } - public Task LoadAsync(Screenshot screenshot, CancellationToken cancellationToken = default) => - Task.Run(() => + public Task LoadAsync(Screenshot screenshot, CancellationToken cancellationToken = default) + { + // ReSharper disable once ConditionIsAlwaysTrueOrFalseAccordingToNullableAPIContract + if (screenshot.Image != null) + return Task.FromResult(screenshot.Image); + return Task.Run(() => { lock (_dbContext) { - return Load(screenshot); + var entry = _dbContext.Entry(screenshot); + entry.Reference(s => s.Image).Load(); + Guard.IsNotNull(screenshot.Image); + return screenshot.Image; } }, cancellationToken); + } private readonly AppDbContext _dbContext; } \ No newline at end of file diff --git a/SightKeeper.Domain.Model/Screenshots/ScreenshotsLibrary.cs b/SightKeeper.Domain.Model/Screenshots/ScreenshotsLibrary.cs index 6ed5b474..e4fed02c 100644 --- a/SightKeeper.Domain.Model/Screenshots/ScreenshotsLibrary.cs +++ b/SightKeeper.Domain.Model/Screenshots/ScreenshotsLibrary.cs @@ -40,6 +40,15 @@ public void DeleteScreenshot(Screenshot screenshot) screenshot.Asset?.ClearItems(); _screenshotRemoved.OnNext(screenshot); } + + public void DeleteScreenshot(int screenshotIndex) + { + var screenshot = _screenshots[screenshotIndex]; + _screenshots.RemoveAt(screenshotIndex); + HasAnyScreenshots = Screenshots.Any(); + screenshot.Asset?.ClearItems(); + _screenshotRemoved.OnNext(screenshot); + } private readonly List _screenshots; @@ -54,11 +63,12 @@ private void ClearExceed() if (MaxQuantity == null) return; var screenshotsToDelete = Screenshots - .Where(screenshot => screenshot.Asset == null) - .OrderByDescending(screenshot => screenshot.CreationDate) + .Select((item, index) => (item, index)) + .Where(screenshot => screenshot.item.Asset == null) + .OrderByDescending(screenshot => screenshot.item.CreationDate) .Skip(MaxQuantity.Value) .ToList(); foreach (var screenshot in screenshotsToDelete) - DeleteScreenshot(screenshot); + DeleteScreenshot(screenshot.index); } } \ No newline at end of file