3136e5bb7e
Replace the tabbed Settings flow with a single grouped page that searches and lazily loads server-backed sections. Preserve existing settings behavior while adding inline loading and retry states and updating coverage for the new navigation model.
148 lines
4.5 KiB
Dart
148 lines
4.5 KiB
Dart
import 'dart:async';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import '../../core/api_gateway.dart';
|
|
import 'ntfy_settings_controller.dart';
|
|
import 'settings_group.dart';
|
|
import 'settings_theme.dart';
|
|
|
|
/// The section title for the server's ntfy notification choices.
|
|
const ntfySectionTitle = 'Phone notifications';
|
|
|
|
/// One line explaining where these notifications go.
|
|
const ntfySectionSubtitle =
|
|
'Sent through ntfy to the devices subscribed to this server.';
|
|
|
|
/// The subtitle once the server delivers to the app itself (it reports
|
|
/// `ntfy_enabled`).
|
|
const ntfySectionAppSubtitle =
|
|
'What the server notifies about: shown by the Pipelines app and, when '
|
|
'enabled, also sent through ntfy.';
|
|
|
|
/// Shown instead of loading while the shared connection is down.
|
|
const ntfyOfflineText = 'Connect to a server to change these.';
|
|
|
|
ValueKey<String> ntfySettingKey(String key) => ValueKey<String>('ntfy-$key');
|
|
const ntfyErrorKey = ValueKey<String>('ntfy-settings-error');
|
|
|
|
/// The server-side notification checkboxes in Settings: run finished, chat
|
|
/// needs you and chat reply finished. Values load from the server whenever
|
|
/// [connected] becomes true and every toggle is written at once; a refusal is
|
|
/// shown inline and the checkbox reverts.
|
|
class SettingsNtfySection extends StatefulWidget {
|
|
const SettingsNtfySection({
|
|
super.key,
|
|
required this.gateway,
|
|
required this.connected,
|
|
});
|
|
|
|
final ApiGateway gateway;
|
|
final bool connected;
|
|
|
|
@override
|
|
State<SettingsNtfySection> createState() => _SettingsNtfySectionState();
|
|
}
|
|
|
|
class _SettingsNtfySectionState extends State<SettingsNtfySection> {
|
|
late NtfySettingsController _controller = NtfySettingsController(
|
|
widget.gateway,
|
|
);
|
|
|
|
@override
|
|
void initState() {
|
|
super.initState();
|
|
if (widget.connected) unawaited(_controller.load());
|
|
}
|
|
|
|
@override
|
|
void didUpdateWidget(SettingsNtfySection oldWidget) {
|
|
super.didUpdateWidget(oldWidget);
|
|
if (oldWidget.gateway != widget.gateway) {
|
|
_controller.dispose();
|
|
_controller = NtfySettingsController(widget.gateway);
|
|
}
|
|
if (widget.connected &&
|
|
(!oldWidget.connected || oldWidget.gateway != widget.gateway)) {
|
|
unawaited(_controller.load());
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_controller.dispose();
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
Widget build(BuildContext context) => ListenableBuilder(
|
|
listenable: _controller,
|
|
builder: (context, _) => _body(context),
|
|
);
|
|
|
|
Widget _body(BuildContext context) {
|
|
final values = _controller.values;
|
|
final enabled = widget.connected && _controller.enabled;
|
|
final status = !widget.connected
|
|
? ntfyOfflineText
|
|
: _controller.loading && values == null
|
|
? 'Loading…'
|
|
: '';
|
|
final hasNtfySwitch = values?.containsKey(ntfyEnabledSetting.key) ?? false;
|
|
final tiles = <Widget>[
|
|
for (final setting in [
|
|
...ntfySettings,
|
|
if (hasNtfySwitch) ntfyEnabledSetting,
|
|
])
|
|
_tile(
|
|
setting,
|
|
values?[setting.key] ?? false,
|
|
enabled ? (value) => _controller.toggle(setting.key, value) : null,
|
|
),
|
|
];
|
|
// A failed refresh keeps the last values and offers Retry beside them.
|
|
final retry =
|
|
widget.connected &&
|
|
!_controller.loading &&
|
|
(values == null || _controller.loadFailed);
|
|
return SettingsGroup(
|
|
title: ntfySectionTitle,
|
|
footer: Column(
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
children: <Widget>[
|
|
Text(hasNtfySwitch ? ntfySectionAppSubtitle : ntfySectionSubtitle),
|
|
if (status.isNotEmpty) Text(status),
|
|
if (_controller.error.isNotEmpty)
|
|
Semantics(
|
|
liveRegion: true,
|
|
child: Text(
|
|
_controller.error,
|
|
key: ntfyErrorKey,
|
|
style: TextStyle(color: settingsErrorColor(context)),
|
|
),
|
|
),
|
|
if (_controller.error.isNotEmpty && retry)
|
|
Align(
|
|
alignment: AlignmentDirectional.centerStart,
|
|
child: TextButton(
|
|
onPressed: _controller.load,
|
|
child: const Text('Retry'),
|
|
),
|
|
),
|
|
],
|
|
),
|
|
children: tiles,
|
|
);
|
|
}
|
|
|
|
Widget _tile(NtfySetting setting, bool value, ValueChanged<bool>? onChanged) {
|
|
return CheckboxListTile(
|
|
key: ntfySettingKey(setting.key),
|
|
title: Text(setting.label),
|
|
subtitle: setting.subtitle == null ? null : Text(setting.subtitle!),
|
|
value: value,
|
|
onChanged: onChanged == null ? null : (next) => onChanged(next!),
|
|
);
|
|
}
|
|
}
|