tileCategories function

List<TileCategory> tileCategories(
  1. List<ArticleGroup> groups,
  2. List<PosArticle> articles
)

Implementation

List<TileCategory> tileCategories(List<ArticleGroup> groups, List<PosArticle> articles) {
  final sichtbar = [
    for (final a in articles)
      if (a.visible && a.active) a,
  ];
  final sortiert = [...groups]..sort((a, b) {
      final s = a.sort.compareTo(b.sort);
      return s != 0 ? s : _nachName(a.name, b.name);
    });
  final bekannt = {for (final g in sortiert) g.id};

  List<PosArticle> kachelnJe(bool Function(PosArticle) passt) {
    final aus = [
      for (final a in sichtbar)
        if (passt(a)) a,
    ];
    aus.sort((a, b) {
      final s = a.sort.compareTo(b.sort);
      return s != 0 ? s : _nachName(a.name, b.name);
    });
    return aus;
  }

  final aus = <TileCategory>[];
  for (final g in sortiert) {
    final kacheln = kachelnJe((a) => a.groupId == g.id);
    // Eine leere Kategorie ist kein Angebot, sondern ein Griff ins Leere.
    if (kacheln.isEmpty) continue;
    aus.add(TileCategory(id: g.id, name: g.name, color: g.color, symbol: g.symbol, tiles: kacheln));
  }

  // Artikel ohne (oder mit gelöschter) Gruppe gehen nicht verloren — sie
  // landen hinten, damit der Betrieb sie überhaupt bemerkt.
  final rest = kachelnJe((a) => a.groupId == null || !bekannt.contains(a.groupId));
  if (rest.isNotEmpty) {
    aus.add(TileCategory(id: ungroupedId, name: 'Ohne Gruppe', color: ungroupedColor, tiles: rest));
  }
  return aus;
}