Breakpoints constructor

Breakpoints(
  1. Iterable<Breakpoint> items
)

items normalized: entries with an id another took are dropped, then sorted by min width (one per min width, the first given kept), the first starting at 0. Empty gives defaults.

Implementation

factory Breakpoints(Iterable<Breakpoint> items) {
  final ids = <String>{};
  final unique = [
    for (final b in items)
      if (ids.add(b.id)) b,
  ];
  if (unique.isEmpty) return defaults;
  final sorted = [...unique];
  // A stable sort by min width (insertion sort keeps given order on ties).
  for (var i = 1; i < sorted.length; i++) {
    final b = sorted[i];
    var j = i - 1;
    while (j >= 0 && sorted[j].minWidth > b.minWidth) {
      sorted[j + 1] = sorted[j];
      j--;
    }
    sorted[j + 1] = b;
  }
  final out = <Breakpoint>[];
  for (final b in sorted) {
    if (out.isNotEmpty && out.last.minWidth == b.minWidth) continue;
    out.add(b);
  }
  if (out.first.minWidth != 0) out[0] = out.first.copyWith(minWidth: 0);
  return Breakpoints._(List.unmodifiable(out));
}