feat(app): show template stage counts

This commit is contained in:
2026-10-23 12:20:06 +01:00
parent c089ecd8af
commit 23b8ee8f7c
8 changed files with 117 additions and 19 deletions
+17
View File
@@ -67,6 +67,7 @@ class TemplatesScreen extends StatefulWidget {
class _TemplatesScreenState extends State<TemplatesScreen> {
late TemplateRepository _repository = TemplateRepository(widget.gateway);
List<TemplateEntry> _templates = const [];
Map<String, int?> _stageCounts = const {};
bool _loading = true;
bool _loaded = false;
String? _error;
@@ -91,6 +92,7 @@ class _TemplatesScreenState extends State<TemplatesScreen> {
// selection while the replacement is read.
_repository = TemplateRepository(widget.gateway);
_templates = const [];
_stageCounts = const {};
_loaded = false;
_selectedKey = null;
unawaited(_load());
@@ -107,8 +109,22 @@ class _TemplatesScreenState extends State<TemplatesScreen> {
try {
final templates = await _repository.listTemplates();
if (!_owns(generation)) return;
final stageCounts = <String, int?>{};
for (final entry in templates) {
try {
final graph = await _repository.validate(entry.source);
final stages = graph['stages'];
stageCounts[entry.key] = stages is List ? stages.length : null;
} catch (_) {
// A refusal means the count is unknown, not that the template has
// zero stages. The catalogue remains usable, as it does on the web.
stageCounts[entry.key] = null;
}
if (!_owns(generation)) return;
}
setState(() {
_templates = templates;
_stageCounts = stageCounts;
_loaded = true;
_loading = false;
final selected = _selectedKey;
@@ -164,6 +180,7 @@ class _TemplatesScreenState extends State<TemplatesScreen> {
final body = selected == null
? TemplateGallery(
templates: _templates,
stageCounts: _stageCounts,
loaded: _loaded,
loading: _loading,
error: _error,
@@ -24,6 +24,7 @@ class TemplateGallery extends StatelessWidget {
const TemplateGallery({
super.key,
required this.templates,
required this.stageCounts,
required this.loaded,
required this.loading,
required this.error,
@@ -32,6 +33,9 @@ class TemplateGallery extends StatelessWidget {
});
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;
@@ -98,9 +102,7 @@ class TemplateGallery extends StatelessWidget {
return ListTile(
key: templateRowKey(entry.key),
title: Text(templateGalleryName(entry)),
subtitle: entry.description.trim().isEmpty
? null
: Text(entry.description),
subtitle: _materialSubtitle(entry),
trailing: const Icon(Icons.chevron_right),
onTap: () => onOpen(entry.key),
);
@@ -118,9 +120,7 @@ class TemplateGallery extends StatelessWidget {
CupertinoListTile(
key: templateRowKey(entry.key),
title: _cupertinoText(templateGalleryName(entry)),
subtitle: entry.description.trim().isEmpty
? null
: _cupertinoText(entry.description),
subtitle: _cupertinoSubtitle(entry),
trailing: const CupertinoListTileChevron(),
onTap: () => onOpen(entry.key),
),
@@ -135,4 +135,38 @@ class TemplateGallery extends StatelessWidget {
/// with the content and nothing else about the tile is restyled.
Widget _cupertinoText(String text) =>
DefaultTextStyle.merge(maxLines: 3, child: Text(text));
Widget _materialSubtitle(TemplateEntry entry) {
final description = entry.description.trim().isEmpty
? null
: Text(entry.description);
final count = Text(_stageCountLabel(entry.key));
if (description == null) return count;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [description, count],
);
}
Widget _cupertinoSubtitle(TemplateEntry entry) {
final description = entry.description.trim().isEmpty
? null
: _cupertinoText(entry.description);
final count = _cupertinoText(_stageCountLabel(entry.key));
if (description == null) return count;
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [description, count],
);
}
String _stageCountLabel(String key) {
if (!stageCounts.containsKey(key) || stageCounts[key] == null) {
return 'stage count unavailable';
}
final count = stageCounts[key]!;
return '$count ${count == 1 ? 'stage' : 'stages'}';
}
}