feat(app): add native template gallery with detail and create
This commit is contained in:
@@ -0,0 +1,208 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
import '../../core/api_gateway.dart';
|
||||
import '../../ui/adaptive/host.dart';
|
||||
import 'data/template_repository.dart';
|
||||
import 'domain/template_entry.dart';
|
||||
import 'ui/template_create_dialog.dart';
|
||||
import 'ui/template_detail.dart';
|
||||
import 'ui/template_gallery.dart';
|
||||
import 'ui/templates_failure.dart';
|
||||
import 'ui/templates_frame.dart';
|
||||
|
||||
export 'ui/template_detail.dart'
|
||||
show
|
||||
templateCreateActionKey,
|
||||
templateDetailKey,
|
||||
templateSourceKey,
|
||||
templateSourceLabel;
|
||||
export 'ui/template_gallery.dart'
|
||||
show
|
||||
templateRowKey,
|
||||
templatesEmptyKey,
|
||||
templatesGalleryKey,
|
||||
templatesLoadingKey;
|
||||
export 'ui/templates_failure.dart'
|
||||
show
|
||||
templatesBannerRetryKey,
|
||||
templatesErrorBannerKey,
|
||||
templatesErrorKey,
|
||||
templatesRetryKey;
|
||||
export 'ui/templates_frame.dart' show templatesBackKey, templatesRefreshKey;
|
||||
|
||||
/// The reusable Templates gallery: the server's actual catalogue, one selected
|
||||
/// template's metadata and verbatim selectable source, and the existing native
|
||||
/// create-only prompt.
|
||||
///
|
||||
/// Loading and selecting only read. The single create action calls
|
||||
/// [showTemplateCreateDialog] unchanged, so validate-then-submit semantics stay
|
||||
/// there; an accepted `detail` is forwarded once through [onCreated]. Nothing
|
||||
/// here publishes, runs, schedules or overwrites a pipeline.
|
||||
///
|
||||
/// Standalone use keeps this screen's own native app bar with Back and Refresh.
|
||||
/// A shell that already owns navigation passes [embedded], which drops the bar
|
||||
/// and keeps Back inside the detail and Refresh at the top of the body.
|
||||
class TemplatesScreen extends StatefulWidget {
|
||||
const TemplatesScreen({
|
||||
super.key,
|
||||
required this.gateway,
|
||||
this.embedded = false,
|
||||
this.onCreated,
|
||||
});
|
||||
|
||||
final ApiGateway gateway;
|
||||
final bool embedded;
|
||||
final ValueChanged<JsonMap>? onCreated;
|
||||
|
||||
@override
|
||||
State<TemplatesScreen> createState() => _TemplatesScreenState();
|
||||
}
|
||||
|
||||
class _TemplatesScreenState extends State<TemplatesScreen> {
|
||||
late TemplateRepository _repository = TemplateRepository(widget.gateway);
|
||||
List<TemplateEntry> _templates = const [];
|
||||
bool _loading = true;
|
||||
bool _loaded = false;
|
||||
String? _error;
|
||||
String? _selectedKey;
|
||||
int _generation = 0;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
unawaited(_load());
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(TemplatesScreen oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
if (oldWidget.gateway == widget.gateway) return;
|
||||
// A different gateway is a different server: never keep this one's rows or
|
||||
// selection while the replacement is read.
|
||||
_repository = TemplateRepository(widget.gateway);
|
||||
_templates = const [];
|
||||
_loaded = false;
|
||||
_selectedKey = null;
|
||||
unawaited(_load());
|
||||
}
|
||||
|
||||
bool _owns(int generation) => mounted && generation == _generation;
|
||||
|
||||
Future<void> _load() async {
|
||||
final generation = ++_generation;
|
||||
setState(() {
|
||||
_loading = true;
|
||||
_error = null;
|
||||
});
|
||||
try {
|
||||
final templates = await _repository.listTemplates();
|
||||
if (!_owns(generation)) return;
|
||||
setState(() {
|
||||
_templates = templates;
|
||||
_loaded = true;
|
||||
_loading = false;
|
||||
final selected = _selectedKey;
|
||||
if (selected != null &&
|
||||
!templates.any((entry) => entry.key == selected)) {
|
||||
_selectedKey = null;
|
||||
}
|
||||
});
|
||||
} catch (error) {
|
||||
if (!_owns(generation)) return;
|
||||
setState(() {
|
||||
_loading = false;
|
||||
_error = _describe(error);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
String _describe(Object error) {
|
||||
final text = error is TemplateRepositoryException
|
||||
? error.message
|
||||
: '$error';
|
||||
final message = text.trim();
|
||||
if (message.isEmpty) return 'The templates could not be loaded.';
|
||||
return message.startsWith('Exception: ') ? message.substring(11) : message;
|
||||
}
|
||||
|
||||
TemplateEntry? get _selected {
|
||||
for (final entry in _templates) {
|
||||
if (entry.key == _selectedKey) return entry;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
void _open(String key) => setState(() => _selectedKey = key);
|
||||
|
||||
void _back() => setState(() => _selectedKey = null);
|
||||
|
||||
void _openCreate(TemplateEntry template) {
|
||||
unawaited(
|
||||
showTemplateCreateDialog(
|
||||
context: context,
|
||||
template: template,
|
||||
repository: _repository,
|
||||
cupertino: usesCupertinoHost(context),
|
||||
onCreated: (detail) => widget.onCreated?.call(detail),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final selected = _selected;
|
||||
final body = selected == null
|
||||
? TemplateGallery(
|
||||
templates: _templates,
|
||||
loaded: _loaded,
|
||||
loading: _loading,
|
||||
error: _error,
|
||||
onRetry: () => unawaited(_load()),
|
||||
onOpen: _open,
|
||||
)
|
||||
: TemplateDetail(
|
||||
entry: selected,
|
||||
showBack: widget.embedded,
|
||||
onBack: _back,
|
||||
onCreate: () => _openCreate(selected),
|
||||
);
|
||||
final framed = TemplatesFrame(
|
||||
body: _withFailure(context, body),
|
||||
embedded: widget.embedded,
|
||||
canGoBack: selected != null,
|
||||
refreshing: _loading,
|
||||
onBack: _back,
|
||||
onRefresh: () => unawaited(_load()),
|
||||
);
|
||||
if (widget.embedded) return framed;
|
||||
// A selected template is this screen's own page: native back returns to
|
||||
// the gallery instead of leaving the destination.
|
||||
return PopScope(
|
||||
canPop: selected == null,
|
||||
onPopInvokedWithResult: (didPop, _) {
|
||||
if (!didPop && _selectedKey != null) _back();
|
||||
},
|
||||
child: framed,
|
||||
);
|
||||
}
|
||||
|
||||
/// A refused refresh stays visible above whichever body is current, so an
|
||||
/// open template never hides a failed read. The rows and selection are kept.
|
||||
Widget _withFailure(BuildContext context, Widget body) {
|
||||
final error = _error;
|
||||
if (error == null || !_loaded) return body;
|
||||
return Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
TemplatesFailureBanner(
|
||||
message: error,
|
||||
retrying: _loading,
|
||||
onRetry: () => unawaited(_load()),
|
||||
),
|
||||
Expanded(child: body),
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user