import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../core/api_gateway.dart'; import 'ntfy_settings_controller.dart'; import 'settings_layout.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.'; /// Shown instead of loading while the shared connection is down. const ntfyOfflineText = 'Connect to a server to change these.'; ValueKey ntfySettingKey(String key) => ValueKey('ntfy-$key'); const ntfyErrorKey = ValueKey('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 createState() => _SettingsNtfySectionState(); } class _SettingsNtfySectionState extends State { 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 cupertino = usesCupertinoControls(context); final values = _controller.values; final enabled = widget.connected && _controller.enabled; final status = !widget.connected ? ntfyOfflineText : _controller.loading && values == null ? 'Loading…' : ''; final tiles = [ for (final setting in ntfySettings) _tile( cupertino, setting, values?[setting.key] ?? false, enabled ? (value) => _controller.toggle(setting.key, value) : null, ), ]; return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text( ntfySectionSubtitle, style: settingsTextStyle(context, secondary: true), ), if (status.isNotEmpty) ...[ const SizedBox(height: settingsFieldGap), Text(status, style: settingsTextStyle(context, secondary: true)), ], const SizedBox(height: settingsFieldGap), if (cupertino) CupertinoListSection.insetGrouped( margin: EdgeInsets.zero, children: tiles, ) else ...tiles, if (_controller.error.isNotEmpty) ...[ const SizedBox(height: settingsFieldGap), Semantics( liveRegion: true, child: Text( _controller.error, key: ntfyErrorKey, style: settingsTextStyle(context, error: true), ), ), if (values == null && widget.connected && !_controller.loading) Align( alignment: AlignmentDirectional.centerStart, child: cupertino ? CupertinoButton( padding: EdgeInsets.zero, onPressed: _controller.load, child: const Text('Retry'), ) : TextButton( onPressed: _controller.load, child: const Text('Retry'), ), ), ], ], ); } Widget _tile( bool cupertino, NtfySetting setting, bool value, ValueChanged? onChanged, ) { if (cupertino) { return CupertinoListTile( key: ntfySettingKey(setting.key), title: Text(setting.label), trailing: CupertinoCheckbox( value: value, onChanged: onChanged == null ? null : (next) => onChanged(next!), ), onTap: onChanged == null ? null : () => onChanged(!value), ); } return CheckboxListTile( key: ntfySettingKey(setting.key), contentPadding: EdgeInsets.zero, title: Text(setting.label), value: value, onChanged: onChanged == null ? null : (next) => onChanged(next!), ); } }