import 'dart:async'; import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../../ui/adaptive/host.dart'; import 'discovered_server.dart'; import 'server_discovery.dart'; enum _Phase { idle, scanning, done, failed } /// "Find servers on this network": browses for a while, lists what answered /// and hands a tapped server to [onSelect]. Manual entry stays beside it. class DiscoveryPanel extends StatefulWidget { const DiscoveryPanel({ super.key, required this.discovery, required this.onSelect, this.enabled = true, this.scanFor = const Duration(seconds: 8), }); final ServerDiscovery discovery; final ValueChanged onSelect; final bool enabled; /// How long one scan browses before it settles on its results. final Duration scanFor; @override State createState() => _DiscoveryPanelState(); } class _DiscoveryPanelState extends State { _Phase _phase = _Phase.idle; List _servers = const []; String? _error; StreamSubscription>? _scan; Timer? _timer; @override void dispose() { _stop(); super.dispose(); } void _stop() { _timer?.cancel(); _timer = null; unawaited(_scan?.cancel()); _scan = null; } void _start() { _stop(); setState(() { _phase = _Phase.scanning; _servers = const []; _error = null; }); _scan = widget.discovery.scan().listen( (servers) { if (mounted) setState(() => _servers = servers); }, onError: (Object error) { _stop(); if (mounted) { setState(() { _phase = _Phase.failed; _error = '$error'; }); } }, ); _timer = Timer(widget.scanFor, () { _stop(); if (mounted) setState(() => _phase = _Phase.done); }); } @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); final scanning = _phase == _Phase.scanning; final label = _phase == _Phase.idle ? 'Find servers on this network' : 'Search again'; final enabled = widget.enabled && !scanning; final button = cupertino ? CupertinoButton( key: const Key('discovery-scan'), onPressed: enabled ? _start : null, child: Text(label), ) : OutlinedButton.icon( key: const Key('discovery-scan'), onPressed: enabled ? _start : null, icon: const Icon(Icons.wifi_find), label: Text(label), ); final status = switch (_phase) { _Phase.scanning => _status( context, cupertino, const Key('discovery-scanning'), 'Looking for Pipelines servers…', busy: true, ), _Phase.done when _servers.isEmpty => _status( context, cupertino, const Key('discovery-empty'), 'No Pipelines servers answered on this network. ' 'Enter the address instead.', ), _Phase.failed => _status( context, cupertino, const Key('discovery-error'), 'Could not search this network: $_error', error: true, ), _ => null, }; return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ button, ?status, for (final server in _servers) _row(context, cupertino, server), ], ); } Widget _status( BuildContext context, bool cupertino, Key key, String text, { bool busy = false, bool error = false, }) { final color = !error ? null : cupertino ? CupertinoColors.systemRed.resolveFrom(context) : Theme.of(context).colorScheme.error; return Padding( key: key, padding: const EdgeInsets.symmetric(vertical: 8), child: Row( children: [ if (busy) ...[ cupertino ? const CupertinoActivityIndicator() : const SizedBox.square( dimension: 16, child: CircularProgressIndicator(strokeWidth: 2), ), const SizedBox(width: 12), ], Expanded( child: Text(text, style: TextStyle(color: color)), ), ], ), ); } Widget _row(BuildContext context, bool cupertino, DiscoveredServer server) { final key = Key('discovered-${server.address}'); final subtitle = Text( server.passwordRequired ? '${server.address} · password' : server.address, ); void tap() => widget.onSelect(server); final onTap = widget.enabled ? tap : null; if (cupertino) { return CupertinoListTile( key: key, leading: const Icon(CupertinoIcons.desktopcomputer), title: Text(server.name), subtitle: subtitle, trailing: const CupertinoListTileChevron(), onTap: onTap, ); } return ListTile( key: key, leading: const Icon(Icons.dns_outlined), title: Text(server.name), subtitle: subtitle, trailing: const Icon(Icons.chevron_right), enabled: widget.enabled, onTap: onTap, ); } }