diff --git a/StabilityMatrix.Avalonia/DesignData/MockImageIndexService.cs b/StabilityMatrix.Avalonia/DesignData/MockImageIndexService.cs index d6538b55..39474975 100644 --- a/StabilityMatrix.Avalonia/DesignData/MockImageIndexService.cs +++ b/StabilityMatrix.Avalonia/DesignData/MockImageIndexService.cs @@ -1,5 +1,6 @@ using System.Threading.Tasks; using DynamicData; +using StabilityMatrix.Core.Extensions; using StabilityMatrix.Core.Models; using StabilityMatrix.Core.Models.Database; using StabilityMatrix.Core.Services; @@ -51,7 +52,7 @@ public class MockImageIndexService : IImageIndexService ) }; - indexCollection.ItemsSource.EditDiff(toAdd, LocalImageFile.Comparer); + indexCollection.ItemsSource.EditDiff(toAdd); return Task.CompletedTask; } diff --git a/StabilityMatrix.Avalonia/ViewModels/OutputsPageViewModel.cs b/StabilityMatrix.Avalonia/ViewModels/OutputsPageViewModel.cs index d001b861..730de23b 100644 --- a/StabilityMatrix.Avalonia/ViewModels/OutputsPageViewModel.cs +++ b/StabilityMatrix.Avalonia/ViewModels/OutputsPageViewModel.cs @@ -25,6 +25,7 @@ using StabilityMatrix.Avalonia.ViewModels.Base; using StabilityMatrix.Avalonia.ViewModels.Dialogs; using StabilityMatrix.Avalonia.ViewModels.OutputsPage; using StabilityMatrix.Core.Attributes; +using StabilityMatrix.Core.Extensions; using StabilityMatrix.Core.Helper; using StabilityMatrix.Core.Helper.Factory; using StabilityMatrix.Core.Models; @@ -392,6 +393,6 @@ public partial class OutputsPageViewModel : PageViewModelBase .EnumerateFiles(directory, "*.png", SearchOption.AllDirectories) .Select(file => LocalImageFile.FromPath(file)); - OutputsCache.EditDiff(files, LocalImageFile.Comparer); + OutputsCache.EditDiff(files); } } diff --git a/StabilityMatrix.Core/Extensions/DynamicDataExtensions.cs b/StabilityMatrix.Core/Extensions/DynamicDataExtensions.cs new file mode 100644 index 00000000..d2aa0d65 --- /dev/null +++ b/StabilityMatrix.Core/Extensions/DynamicDataExtensions.cs @@ -0,0 +1,35 @@ +using DynamicData; + +namespace StabilityMatrix.Core.Extensions; + +public static class DynamicDataExtensions +{ + /// + /// Loads the cache with the specified items in an optimised manner i.e. calculates the differences between the old and new items + /// in the list and amends only the differences. + /// + /// The type of the object. + /// The type of the key. + /// The source. + /// The items to add, update or delete. + /// source. + public static void EditDiff( + this ISourceCache source, + IEnumerable allItems + ) + where TObject : IEquatable + where TKey : notnull + { + if (source is null) + { + throw new ArgumentNullException(nameof(source)); + } + + if (allItems is null) + { + throw new ArgumentNullException(nameof(allItems)); + } + + source.EditDiff(allItems, (x, y) => x.Equals(y)); + } +} diff --git a/StabilityMatrix.Core/Models/Database/LocalImageFile.cs b/StabilityMatrix.Core/Models/Database/LocalImageFile.cs index b966c999..1abde8dd 100644 --- a/StabilityMatrix.Core/Models/Database/LocalImageFile.cs +++ b/StabilityMatrix.Core/Models/Database/LocalImageFile.cs @@ -1,4 +1,5 @@ -using StabilityMatrix.Core.Helper; +using DynamicData.Tests; +using StabilityMatrix.Core.Helper; using StabilityMatrix.Core.Models.FileInterfaces; using JsonSerializer = System.Text.Json.JsonSerializer; using Size = System.Drawing.Size; @@ -116,21 +117,4 @@ public record LocalImageFile public static readonly HashSet SupportedImageExtensions = new() { ".png", ".jpg", ".jpeg", ".webp" }; - - private sealed class LocalImageFileEqualityComparer : IEqualityComparer - { - /// - public bool Equals(LocalImageFile x, LocalImageFile y) - { - return x == y; - } - - public int GetHashCode(LocalImageFile obj) - { - return obj.GetHashCode(); - } - } - - public static IEqualityComparer Comparer { get; } = - new LocalImageFileEqualityComparer(); } diff --git a/StabilityMatrix.Core/Services/ImageIndexService.cs b/StabilityMatrix.Core/Services/ImageIndexService.cs index 8543c24f..c07a6e9d 100644 --- a/StabilityMatrix.Core/Services/ImageIndexService.cs +++ b/StabilityMatrix.Core/Services/ImageIndexService.cs @@ -4,6 +4,7 @@ using AsyncAwaitBestPractices; using DynamicData; using Microsoft.Extensions.Logging; using StabilityMatrix.Core.Attributes; +using StabilityMatrix.Core.Extensions; using StabilityMatrix.Core.Helper; using StabilityMatrix.Core.Models; using StabilityMatrix.Core.Models.Database; @@ -79,7 +80,7 @@ public class ImageIndexService : IImageIndexService var indexElapsed = stopwatch.Elapsed; - indexCollection.ItemsSource.EditDiff(toAdd, LocalImageFile.Comparer); + indexCollection.ItemsSource.EditDiff(toAdd); // End stopwatch.Stop(); diff --git a/StabilityMatrix.Tests/Models/LocalImageFileTests.cs b/StabilityMatrix.Tests/Models/LocalImageFileTests.cs deleted file mode 100644 index 725df8cf..00000000 --- a/StabilityMatrix.Tests/Models/LocalImageFileTests.cs +++ /dev/null @@ -1,33 +0,0 @@ -using StabilityMatrix.Core.Models; -using StabilityMatrix.Core.Models.Database; - -namespace StabilityMatrix.Tests.Models; - -[TestClass] -public class LocalImageFileTests -{ - [TestMethod] - public void TestComparer() - { - var comparer = LocalImageFile.Comparer; - - var file1 = new LocalImageFile - { - AbsolutePath = "same", - GenerationParameters = new GenerationParameters { Width = 10 } - }; - - var file2 = new LocalImageFile - { - AbsolutePath = "same", - GenerationParameters = new GenerationParameters { Width = 10 } - }; - - var file3 = new LocalImageFile { AbsolutePath = "different" }; - - Assert.IsTrue(comparer.Equals(file1, file2)); - Assert.AreEqual(comparer.GetHashCode(file1), comparer.GetHashCode(file2)); - Assert.IsFalse(comparer.Equals(file1, file3)); - Assert.AreNotEqual(comparer.GetHashCode(file1), comparer.GetHashCode(file3)); - } -}