Files
pipelines/app/lib/features/templates/ui/template_gallery.dart
T

168 lines
5.5 KiB
Dart

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/floating_bar_layout.dart';
import '../../../ui/adaptive/host.dart';
import '../../../ui/layout/app_list_avatar.dart';
import '../../../ui/layout/app_list_row.dart';
import '../domain/template_entry.dart';
import 'template_detail.dart';
import 'templates_failure.dart';
const templatesGalleryKey = ValueKey<String>('template-gallery');
const templatesLoadingKey = ValueKey<String>('templates-loading');
const templatesEmptyKey = ValueKey<String>('templates-empty');
const _loadingLabel = 'Loading templates';
const _loadingMessage = 'Loading templates…';
const _emptyMessage = 'No templates are available on this server.';
Key templateRowKey(String key) => ValueKey<String>('template-row-$key');
Key templateAvatarKey(String key) => ValueKey<String>('template-avatar-$key');
/// The widest the gallery and an open template grow on a wide window: the
/// Settings column's reading width, so the page opened from Settings lines up
/// with it.
const double templatesContentMaxWidth = 720;
/// Caps [child] at [templatesContentMaxWidth], start-aligned under the bar.
Widget templatesColumn(Widget child) => Align(
alignment: AlignmentDirectional.topStart,
child: ConstrainedBox(
constraints: const BoxConstraints(maxWidth: templatesContentMaxWidth),
child: child,
),
);
/// The gallery's truthful states and the app's shared list rows.
///
/// It owns no requests: loading, failure and the actual rows all come from the
/// screen, so opening a row only selects an already-read template.
class TemplateGallery extends StatelessWidget {
const TemplateGallery({
super.key,
required this.templates,
required this.stageCounts,
required this.loaded,
required this.loading,
required this.error,
required this.onRetry,
required this.onOpen,
});
final List<TemplateEntry> templates;
/// Parsed counts; a present null value means the source could not be parsed.
final Map<String, int?> stageCounts;
final bool loaded;
final bool loading;
final String? error;
final VoidCallback onRetry;
final ValueChanged<String> onOpen;
@override
Widget build(BuildContext context) {
if (!loaded) {
if (error != null) {
return TemplatesFailureState(
message: error!,
retrying: loading,
onRetry: onRetry,
);
}
return _loadingState(context);
}
if (templates.isEmpty) return _emptyState(context);
return _list(context);
}
Widget _loadingState(BuildContext context) {
return Center(
key: templatesLoadingKey,
child: Semantics(
liveRegion: true,
label: _loadingLabel,
child: Column(
mainAxisSize: MainAxisSize.min,
children: [
usesCupertinoHost(context)
? const CupertinoActivityIndicator()
: const CircularProgressIndicator(),
const SizedBox(height: 12),
const Text(_loadingMessage),
],
),
),
);
}
Widget _emptyState(BuildContext context) {
return Center(
key: templatesEmptyKey,
child: Padding(
padding: const EdgeInsets.all(24),
child: Text(_emptyMessage, style: templateMutedStyle(context)),
),
);
}
Widget _list(BuildContext context) {
final cupertino = usesCupertinoHost(context);
// The rows scroll behind the bar and the floating tab bar.
return ListView.builder(
key: templatesGalleryKey,
padding: withBarClearance(context, const EdgeInsets.only(bottom: 8)),
itemCount: templates.length,
itemBuilder: (context, index) {
final entry = templates[index];
return templatesColumn(
AppListRow(
key: templateRowKey(entry.key),
cupertino: cupertino,
divider: index < templates.length - 1,
onTap: () => onOpen(entry.key),
leading: ExcludeSemantics(
child: AppListAvatar.initial(
templateDetailName(entry),
key: templateAvatarKey(entry.key),
color: _avatarColor(context, entry.key, cupertino),
),
),
title: Text(templateGalleryName(entry)),
subtitle: _subtitle(entry),
),
);
},
);
}
/// One of the theme's accents, picked by the template key so a row keeps
/// its colour across reloads (the Pipelines list's palette).
Color _avatarColor(BuildContext context, String key, bool cupertino) {
final List<Color> colors;
if (cupertino) {
colors = [
CupertinoTheme.of(context).primaryColor,
CupertinoColors.systemIndigo.resolveFrom(context),
CupertinoColors.systemTeal.resolveFrom(context),
];
} else {
final scheme = Theme.of(context).colorScheme;
colors = [scheme.primary, scheme.secondary, scheme.tertiary];
}
final hash = key.codeUnits.fold<int>(0, (sum, unit) => sum + unit);
return colors[hash % colors.length];
}
/// The description, then the stage count when the server could parse the
/// source. An unknown count is left out rather than announced on every row.
Widget? _subtitle(TemplateEntry entry) {
final count = stageCounts[entry.key];
final parts = [
if (entry.description.trim().isNotEmpty) entry.description.trim(),
if (count != null) '$count ${count == 1 ? 'stage' : 'stages'}',
];
return parts.isEmpty ? null : Text(parts.join(' · '));
}
}