Files
pipelines/app/lib/features/onboarding/discovery/discovery_panel.dart
T

207 lines
5.5 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import '../../../ui/adaptive/host.dart';
import 'discovered_server.dart';
import 'browser_server_discovery.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<DiscoveredServer> onSelect;
final bool enabled;
/// How long one scan browses before it settles on its results.
final Duration scanFor;
@override
State<DiscoveryPanel> createState() => _DiscoveryPanelState();
}
class _DiscoveryPanelState extends State<DiscoveryPanel> {
_Phase _phase = _Phase.idle;
List<DiscoveredServer> _servers = const [];
String? _error;
StreamSubscription<List<DiscoveredServer>>? _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);
},
onDone: () {
_timer?.cancel();
if (mounted && _phase == _Phase.scanning) {
setState(() => _phase = _Phase.done);
}
},
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,
if (widget.discovery is BrowserServerDiscovery)
const Text('Searches this server’s local network.'),
?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,
);
}
}