175 lines
5.3 KiB
Dart
175 lines
5.3 KiB
Dart
import '../../../ui/theme/app_theme.dart';
|
|
import 'package:flutter/cupertino.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../../ui/adaptive/host.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');
|
|
|
|
/// The gallery's truthful states and native catalogue 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) => _body(context);
|
|
|
|
Widget _body(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 usesCupertinoHost(context)
|
|
? _cupertinoList(context)
|
|
: _materialList(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 _materialList(BuildContext context) {
|
|
return ListView.separated(
|
|
key: templatesGalleryKey,
|
|
itemCount: templates.length,
|
|
separatorBuilder: (_, _) => const Divider(height: 1),
|
|
itemBuilder: (context, index) {
|
|
final entry = templates[index];
|
|
return ListTile(
|
|
key: templateRowKey(entry.key),
|
|
title: Text(templateGalleryName(entry)),
|
|
subtitle: _materialSubtitle(entry),
|
|
trailing: const Icon(Icons.chevron_right),
|
|
onTap: () => onOpen(entry.key),
|
|
);
|
|
},
|
|
);
|
|
}
|
|
|
|
Widget _cupertinoList(BuildContext context) {
|
|
return ListView(
|
|
key: templatesGalleryKey,
|
|
children: [
|
|
CupertinoListSection.insetGrouped(
|
|
backgroundColor: AppColors.groupedBackground,
|
|
children: [
|
|
for (final entry in templates)
|
|
CupertinoListTile(
|
|
key: templateRowKey(entry.key),
|
|
title: _cupertinoText(templateGalleryName(entry)),
|
|
subtitle: _cupertinoSubtitle(entry),
|
|
trailing: const CupertinoListTileChevron(),
|
|
onTap: () => onOpen(entry.key),
|
|
),
|
|
],
|
|
),
|
|
],
|
|
);
|
|
}
|
|
|
|
/// The native tile is single-line by default. Let its content wrap at larger
|
|
/// text sizes instead of ellipsizing a template's own name; the row grows
|
|
/// 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'}';
|
|
}
|
|
}
|