Files
pipelines/app/lib/features/artifacts/domain/gallery_navigation.dart
T

100 lines
3.7 KiB
Dart

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.group,
required this.artifact,
required this.pageIndex,
});
final ArtifactGroup group;
final ArtifactRecord artifact;
/// Position in the filtered page list.
final int pageIndex;
String get stageKey => group.stageKey;
int get visit => group.visit;
String get identity => artifact.hasUrl
? artifact.url
: '$stageKey\u0000$visit\u0000$pageIndex\u0000${artifact.name}';
}
/// The gallery row identity of [artifact] inside [group], shared by the row
/// list and the viewer so selecting a row opens exactly that page. The recorded
/// visit is part of the key, so two visits that recorded the same name stay
/// distinct even when their recorded urls match.
String artifactVisitIdentityOf(ArtifactGroup group, ArtifactRecord artifact) {
final recorded = artifact.hasUrl ? artifact.url : artifact.name;
return '${group.stageKey}\u0000${group.visit}\u0000$recorded';
}
/// 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,
/// then the visit's recorded turn documents that open in the renderer. A session
/// export is external-only and never enters the page list unless
/// [includeExternalDocuments] is requested by a search result, which must still
/// land on the exact recorded document in the full-screen viewer. Pure: it only
/// filters and orders records the gallery already holds.
List<PagedArtifact> pagedArtifacts(
List<ArtifactGroup> groups, {
bool includeExternalDocuments = false,
}) {
final pages = <PagedArtifact>[];
void add(ArtifactGroup group, ArtifactRecord artifact) {
pages.add(
PagedArtifact(group: group, artifact: artifact, pageIndex: pages.length),
);
}
for (final group in groups) {
for (final artifact in group.artifacts) {
if (!isViewerArtifact(artifact.contentType, artifact.name)) continue;
add(group, artifact);
}
for (final document in group.documents) {
final artifact = document.record;
if (artifact == null ||
(!document.opensInViewer && !includeExternalDocuments)) {
continue;
}
add(group, artifact);
}
}
return List<PagedArtifact>.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<String, int> viewerIndexByUrl(List<ArtifactGroup> groups) {
final indices = <String, int>{};
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}';
}