Browse Source

Add IEquatable overload for EditDiff

pull/240/head
Ionite 1 year ago
parent
commit
1b3eef75f4
No known key found for this signature in database
  1. 3
      StabilityMatrix.Avalonia/DesignData/MockImageIndexService.cs
  2. 3
      StabilityMatrix.Avalonia/ViewModels/OutputsPageViewModel.cs
  3. 35
      StabilityMatrix.Core/Extensions/DynamicDataExtensions.cs
  4. 20
      StabilityMatrix.Core/Models/Database/LocalImageFile.cs
  5. 3
      StabilityMatrix.Core/Services/ImageIndexService.cs
  6. 33
      StabilityMatrix.Tests/Models/LocalImageFileTests.cs

3
StabilityMatrix.Avalonia/DesignData/MockImageIndexService.cs

@ -1,5 +1,6 @@
using System.Threading.Tasks; using System.Threading.Tasks;
using DynamicData; using DynamicData;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Models; using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Models.Database; using StabilityMatrix.Core.Models.Database;
using StabilityMatrix.Core.Services; 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; return Task.CompletedTask;
} }

3
StabilityMatrix.Avalonia/ViewModels/OutputsPageViewModel.cs

@ -25,6 +25,7 @@ using StabilityMatrix.Avalonia.ViewModels.Base;
using StabilityMatrix.Avalonia.ViewModels.Dialogs; using StabilityMatrix.Avalonia.ViewModels.Dialogs;
using StabilityMatrix.Avalonia.ViewModels.OutputsPage; using StabilityMatrix.Avalonia.ViewModels.OutputsPage;
using StabilityMatrix.Core.Attributes; using StabilityMatrix.Core.Attributes;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Helper; using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Helper.Factory; using StabilityMatrix.Core.Helper.Factory;
using StabilityMatrix.Core.Models; using StabilityMatrix.Core.Models;
@ -392,6 +393,6 @@ public partial class OutputsPageViewModel : PageViewModelBase
.EnumerateFiles(directory, "*.png", SearchOption.AllDirectories) .EnumerateFiles(directory, "*.png", SearchOption.AllDirectories)
.Select(file => LocalImageFile.FromPath(file)); .Select(file => LocalImageFile.FromPath(file));
OutputsCache.EditDiff(files, LocalImageFile.Comparer); OutputsCache.EditDiff(files);
} }
} }

35
StabilityMatrix.Core/Extensions/DynamicDataExtensions.cs

@ -0,0 +1,35 @@
using DynamicData;
namespace StabilityMatrix.Core.Extensions;
public static class DynamicDataExtensions
{
/// <summary>
/// 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.
/// </summary>
/// <typeparam name="TObject">The type of the object.</typeparam>
/// <typeparam name="TKey">The type of the key.</typeparam>
/// <param name="source">The source.</param>
/// <param name="allItems">The items to add, update or delete.</param>
/// <exception cref="System.ArgumentNullException">source.</exception>
public static void EditDiff<TObject, TKey>(
this ISourceCache<TObject, TKey> source,
IEnumerable<TObject> allItems
)
where TObject : IEquatable<TObject>
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));
}
}

20
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 StabilityMatrix.Core.Models.FileInterfaces;
using JsonSerializer = System.Text.Json.JsonSerializer; using JsonSerializer = System.Text.Json.JsonSerializer;
using Size = System.Drawing.Size; using Size = System.Drawing.Size;
@ -116,21 +117,4 @@ public record LocalImageFile
public static readonly HashSet<string> SupportedImageExtensions = public static readonly HashSet<string> SupportedImageExtensions =
new() { ".png", ".jpg", ".jpeg", ".webp" }; new() { ".png", ".jpg", ".jpeg", ".webp" };
private sealed class LocalImageFileEqualityComparer : IEqualityComparer<LocalImageFile>
{
/// <inheritdoc />
public bool Equals(LocalImageFile x, LocalImageFile y)
{
return x == y;
}
public int GetHashCode(LocalImageFile obj)
{
return obj.GetHashCode();
}
}
public static IEqualityComparer<LocalImageFile> Comparer { get; } =
new LocalImageFileEqualityComparer();
} }

3
StabilityMatrix.Core/Services/ImageIndexService.cs

@ -4,6 +4,7 @@ using AsyncAwaitBestPractices;
using DynamicData; using DynamicData;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using StabilityMatrix.Core.Attributes; using StabilityMatrix.Core.Attributes;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Helper; using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Models; using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Models.Database; using StabilityMatrix.Core.Models.Database;
@ -79,7 +80,7 @@ public class ImageIndexService : IImageIndexService
var indexElapsed = stopwatch.Elapsed; var indexElapsed = stopwatch.Elapsed;
indexCollection.ItemsSource.EditDiff(toAdd, LocalImageFile.Comparer); indexCollection.ItemsSource.EditDiff(toAdd);
// End // End
stopwatch.Stop(); stopwatch.Stop();

33
StabilityMatrix.Tests/Models/LocalImageFileTests.cs

@ -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));
}
}
Loading…
Cancel
Save