namespace OpenNest.Data;
///
/// Browse state for Add from Drawing Library: the requested search, sort, page and
/// nest filter, and the last page applied. By default only drawings with the nest's
/// material, grade and thickness are listed; lifts that
/// filter. Paging, superseding and disposal follow .
/// Call from one thread (the UI thread).
///
public sealed class DrawingLibraryBrowseSession : BrowseSession
{
private readonly IDrawingLibrary _library;
/// The nest to filter to, or null to list every active drawing (File > Drawing Library).
public DrawingLibraryBrowseSession(IDrawingLibrary library, Nest? nest, int pageSize = DrawingLibraryQuery.DefaultLimit)
: base(pageSize, DrawingLibraryQuery.MaxLimit, DrawingSortField.SavedAt)
{
ArgumentNullException.ThrowIfNull(library);
_library = library;
MaterialName = (nest?.Material?.Name ?? "").Trim();
MaterialGrade = (nest?.Material?.Grade ?? "").Trim();
// A nest without a thickness has nothing to match; it filters by material only.
Thickness = nest != null && double.IsFinite(nest.Thickness) && nest.Thickness > 0
? DrawingLibraryRules.NormalizeThickness(nest.Thickness)
: null;
}
/// The nest's material name, applied unless ; blank means any.
public string MaterialName { get; }
/// The nest's grade, applied unless ; blank means any.
public string MaterialGrade { get; }
/// The nest's thickness, applied unless ; null means any.
public double? Thickness { get; }
/// True when the nest filter is lifted and every active drawing is listed.
public bool ShowAll { get; private set; }
/// True when the latest request narrows by the nest's material, grade or thickness.
public bool IsFilteredToNest =>
!ShowAll && (MaterialName.Length > 0 || MaterialGrade.Length > 0 || Thickness is not null);
///
public override string Summary =>
PageSummary("drawings",
Search.Length > 0 ? "No drawings match the search."
: IsFilteredToNest ? "No library drawings match this nest's material and thickness. Choose Show all to see every drawing."
: "No drawings in the library.");
/// Lifts or restores the nest filter and returns to the first page.
public Task SetShowAllAsync(bool showAll)
{
ShowAll = showAll;
return FirstPageAsync();
}
protected override Task QueryAsync(CancellationToken cancellationToken) =>
_library.QueryAsync(
new DrawingLibraryQuery
{
Search = Search,
MaterialName = ShowAll ? "" : MaterialName,
MaterialGrade = ShowAll ? "" : MaterialGrade,
Thickness = ShowAll ? null : Thickness,
Sort = Sort,
Descending = Descending,
Offset = Offset,
Limit = PageSize,
},
cancellationToken);
}