Skip to content

Commit

Permalink
Remove exceed check in DbScreenshotImageLoader;
Browse files Browse the repository at this point in the history
Delete exceed screenshots in more optimized way;
Improve(?) image loading;
  • Loading branch information
Neakita committed Sep 13, 2023
1 parent 1a3cc02 commit 76ef112
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,16 @@
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;

public sealed class ScreenshotViewModel : ViewModel
{
public Screenshot Item { get; }

public Task<byte[]> Content
{
get
{
return _imageLoader.LoadAsync(Item).ToObservable().Select(item => item.Content).ToTask();
}
}
public Task<Image> Image => _imageLoader.LoadAsync(Item);

public bool IsAsset => Item.Asset != null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
<ListBox.ItemTemplate>
<DataTemplate>
<Panel Name="Panel">
<Image Source="{Binding Content^, Converter={StaticResource BytesToBitmapConverter}}"
<Image Source="{Binding Image^.Content, Converter={StaticResource BytesToBitmapConverter}}"
Margin="1"/>
<avalonia:MaterialIcon Kind="BookmarkCheck"
Width="30" Height="30"
Expand Down
2 changes: 1 addition & 1 deletion SightKeeper.Avalonia/Views/Annotating/DetectorDrawer.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<Panel HorizontalAlignment="Center" VerticalAlignment="Center"
Name="Root">

<Image Name="Image" IsHitTestVisible="True" Source="{Binding SelectedScreenshotViewModel.Value.Content^, Converter={StaticResource BytesToBitmapConverter}}" />
<Image Name="Image" IsHitTestVisible="True" Source="{Binding SelectedScreenshotViewModel.Value.Image^.Content, Converter={StaticResource BytesToBitmapConverter}}" />

<ListBox Name="CanvasItemsControl"
ItemsSource="{Binding Path=SelectedScreenshotViewModel.Items}"
Expand Down
18 changes: 12 additions & 6 deletions SightKeeper.Data/Services/DbScreenshotImageLoader.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using CommunityToolkit.Diagnostics;
using Microsoft.EntityFrameworkCore;
using SightKeeper.Application.Annotating;
using SightKeeper.Domain.Model;
using SightKeeper.Domain.Model.Common;
Expand All @@ -21,21 +20,28 @@ public Image Load(Screenshot screenshot)
lock (_dbContext)
{
var entry = _dbContext.Entry(screenshot);
if (entry.State != EntityState.Detached)
entry.Reference(s => s.Image).Load();
entry.Reference(s => s.Image).Load();
Guard.IsNotNull(screenshot.Image);
return screenshot.Image;
}
}

public Task<Image> LoadAsync(Screenshot screenshot, CancellationToken cancellationToken = default) =>
Task.Run(() =>
public Task<Image> 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;
}
16 changes: 13 additions & 3 deletions SightKeeper.Domain.Model/Screenshots/ScreenshotsLibrary.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Screenshot> _screenshots;

Expand All @@ -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);
}
}

0 comments on commit 76ef112

Please sign in to comment.