Files
pipelines/app/lib/features/settings/settings_screen.dart
T

222 lines
7.4 KiB
Dart

import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../core/transport/pipelines_client.dart' as transport;
import '../attention/notify/notification_controller.dart';
import '../attention/notify/notification_models.dart';
import 'default_endpoint.dart';
import 'settings_controller.dart';
import 'settings_controls.dart';
import 'settings_layout.dart';
import 'settings_notifications_section.dart';
import 'settings_schedules_section.dart';
import 'settings_sections.dart';
import 'settings_store.dart';
import 'settings_templates_section.dart';
/// The Settings destination: the server connection and the server-side
/// workspace, in native Material (native platforms) or Cupertino (iOS) controls.
///
/// The shared [transport.PipelinesClient] is supplied by the shell. This screen
/// subscribes to it, connects/disconnects it and pings it on connected, but it
/// never opens a connection on its own and never disposes the client. One
/// controller is created in [initState], so rebuilding the screen cannot start
/// a second connection.
class SettingsScreen extends StatefulWidget {
const SettingsScreen({
super.key,
required this.client,
this.initialEndpoint,
this.store,
this.ping,
this.embedded = false,
this.onOpenTemplates,
this.onOpenSchedules,
this.notifications,
});
final transport.PipelinesClient client;
/// A fixture/preview seed for the URL field. Persisted settings still load;
/// this value takes precedence for the draft and is never written by itself.
final Uri? initialEndpoint;
/// The feature's own persistence. Defaults to the `pipelines.next` keys.
final SettingsStore? store;
/// Test seam for the exact ping request. Production uses `client.call('ping')`.
final PingCallback? ping;
/// The application shell already owns the native navigation bar and safe area.
final bool embedded;
/// Opens the Templates gallery. Null keeps Settings standalone/incomplete
/// without a dead row that would go nowhere.
final VoidCallback? onOpenTemplates;
/// Opens the saved-schedule list above Settings.
final VoidCallback? onOpenSchedules;
/// The app-level notification capability; absent in connection-only fixtures.
final NotificationController? notifications;
@override
State<SettingsScreen> createState() => _SettingsScreenState();
}
class _SettingsScreenState extends State<SettingsScreen> {
final TextEditingController _serverUrlField = TextEditingController();
final TextEditingController _workspaceField = TextEditingController();
late final SettingsController _controller;
@override
void initState() {
super.initState();
_controller = SettingsController(
client: widget.client,
store: widget.store ?? const SharedPreferencesSettingsStore(),
initialEndpoint: widget.initialEndpoint,
defaultEndpoint: defaultServerUrl(),
ping: widget.ping,
);
_controller.addListener(_onSettingsChanged);
unawaited(_controller.initialize());
}
void _onSettingsChanged() {
if (!mounted) return;
_syncField(_serverUrlField, _controller.serverUrl);
_syncField(_workspaceField, _controller.workspaceDraft);
setState(() {});
}
/// Follow the controller's value (a restored value or the normalised URL)
/// without disturbing a field the operator is already typing in.
void _syncField(TextEditingController field, String value) {
if (field.text == value) return;
field.value = TextEditingValue(
text: value,
selection: TextSelection.collapsed(offset: value.length),
);
}
@override
void dispose() {
_controller.removeListener(_onSettingsChanged);
_controller.dispose();
_serverUrlField.dispose();
_workspaceField.dispose();
// The shared client is owned by the shell and deliberately outlives this
// screen, so it is not disposed here.
super.dispose();
}
@override
Widget build(BuildContext context) {
final content = SafeArea(
child: SingleChildScrollView(
// Web `.settings` page insets: 20 above the title, 16 at the sides and
// a clear 32 below the last section.
padding: const EdgeInsetsDirectional.fromSTEB(
settingsPageSidePadding,
settingsPageTopPadding,
settingsPageSidePadding,
settingsPageBottomPadding,
),
child: Center(
child: ConstrainedBox(
constraints: const BoxConstraints(
maxWidth: settingsContentMaxWidth,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
_SettingsSection(
title: 'Connection',
child: SettingsConnectionSection(
controller: _controller,
serverUrlField: _serverUrlField,
),
),
const SizedBox(height: settingsSectionGap),
_SettingsSection(
title: 'Workspace',
child: SettingsWorkspaceSection(
controller: _controller,
workspaceField: _workspaceField,
),
),
if (widget.notifications != null) ...<Widget>[
const SizedBox(height: settingsSectionGap),
_SettingsSection(
title: notificationSectionTitle,
child: SettingsNotificationsSection(
controller: widget.notifications!,
),
),
],
if (widget.onOpenSchedules != null) ...<Widget>[
const SizedBox(height: settingsSectionGap),
_SettingsSection(
title: 'Schedules',
child: SettingsSchedulesSection(
onOpen: widget.onOpenSchedules!,
),
),
],
if (widget.onOpenTemplates != null) ...<Widget>[
const SizedBox(height: settingsSectionGap),
_SettingsSection(
title: 'Templates',
child: SettingsTemplatesSection(
onOpen: widget.onOpenTemplates!,
),
),
],
],
),
),
),
),
);
if (widget.embedded) return content;
if (usesCupertinoControls(context)) {
return CupertinoPageScaffold(
navigationBar: const CupertinoNavigationBar(middle: Text('Settings')),
child: content,
);
}
return Scaffold(
appBar: AppBar(title: const Text('Settings')),
body: content,
);
}
}
/// One grouped Settings section: a compact title followed by its body at the
/// web's 8px title gap. Sections are separated by [settingsSectionGap]. A null
/// title lets a body (the notification tile) draw its own compact heading.
class _SettingsSection extends StatelessWidget {
const _SettingsSection({required this.child, this.title});
final String? title;
final Widget child;
@override
Widget build(BuildContext context) {
final title = this.title;
return Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
if (title != null) ...<Widget>[
Text(title, style: settingsTextStyle(context, title: true)),
const SizedBox(height: settingsTitleGap),
],
child,
],
);
}
}