import 'artifact_groups.dart'; import 'artifact_kind.dart'; import 'artifact_record.dart'; /// One artifact in the viewer's filtered page list, with the stage visit it /// belongs to. [identity] is the recorded url when there is one; otherwise it is /// a visit-scoped key so two url-less records never collapse into one page. class PagedArtifact { const PagedArtifact({ required this.stageKey, required this.visit, required this.artifact, required this.pageIndex, }); final String stageKey; final int visit; final ArtifactRecord artifact; /// Position in the filtered page list. final int pageIndex; String get identity => artifact.hasUrl ? artifact.url : '$stageKey\u0000$visit\u0000$pageIndex\u0000${artifact.name}'; } /// The artifacts the viewer can page through, in gallery order: group by group, /// each group's images, videos and markdown/text documents in recorded order. /// Other artifacts open externally and are not part of this list. Pure: it only /// filters and orders records the gallery already holds. List pagedArtifacts(List groups) { final pages = []; for (final group in groups) { for (final artifact in group.artifacts) { if (!isViewerArtifact(artifact.contentType, artifact.name)) continue; pages.add( PagedArtifact( stageKey: group.stageKey, visit: group.visit, artifact: artifact, pageIndex: pages.length, ), ); } } return List.unmodifiable(pages); } /// Each page's recorded url mapped to its page index, so a tile and the viewer /// derive from the same ordered list. The first occurrence of a url wins. A /// record with no recorded url has no fetchable identity and is omitted. Map viewerIndexByUrl(List groups) { final indices = {}; for (final page in pagedArtifacts(groups)) { if (!page.artifact.hasUrl) continue; indices.putIfAbsent(page.artifact.url, () => page.pageIndex); } return indices; } /// Paging from [current] by [delta] within `count` pages, clamped at both ends /// so a page never wraps and an empty list is index 0. int pagedIndex(int current, int delta, int count) { if (count <= 0) return 0; final wanted = current + delta; if (wanted < 0) return 0; if (wanted >= count) return count - 1; return wanted; } /// The viewer's position label, e.g. `2 of 5 · image/png`. String artifactViewerPositionLabel(int index, int count, String contentType) { final type = contentType.split(';').first.trim(); return '${index + 1} of $count · ${type.isEmpty ? 'artifact' : type}'; }