import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; import '../../core/servers/machine_access.dart'; import '../../core/servers/server_catalog.dart'; import '../../ui/adaptive/host.dart'; /// What the editor does once a machine is saved: ask it whether this device /// may use it, then either sign in with its own password or explain why it /// could not be reached. The machine stays saved whatever happens here. class SavedServerSignInStep extends StatefulWidget { const SavedServerSignInStep({ super.key, required this.server, required this.authenticator, required this.onDone, }); final SavedServer server; final MachineAuthenticator authenticator; final VoidCallback onDone; @override State createState() => _SavedServerSignInStepState(); } class _SavedServerSignInStepState extends State { final _password = TextEditingController(); MachineAccessResult? _result; bool _busy = true; String? _error; @override void initState() { super.initState(); _probe(); } @override void dispose() { _password.dispose(); super.dispose(); } Future _probe() async { setState(() { _busy = true; _error = null; }); MachineAccessResult result; try { result = await widget.authenticator.probe(widget.server); } catch (error) { result = MachineAccessResult(MachineAccess.unreachable, detail: '$error'); } if (!mounted) return; if (result.connected) { widget.onDone(); return; } setState(() { _result = result; _busy = false; }); } Future _signIn() async { if (_password.text.isEmpty) return; setState(() { _busy = true; _error = null; }); String? problem; try { problem = await widget.authenticator.authenticate( widget.server, _password.text, ); } catch (error) { problem = 'Could not sign in: $error'; } if (!mounted) return; if (problem == null) { widget.onDone(); return; } setState(() { _busy = false; _error = problem; }); } @override Widget build(BuildContext context) { final cupertino = usesCupertinoHost(context); final name = widget.server.name; final errorColor = cupertino ? CupertinoColors.systemRed.resolveFrom(context) : Theme.of(context).colorScheme.error; final result = _result; if (result == null) { return Row( children: [ cupertino ? const CupertinoActivityIndicator() : const SizedBox.square( dimension: 16, child: CircularProgressIndicator(strokeWidth: 2), ), const SizedBox(width: 12), Expanded( child: Text('Saved. Checking whether $name needs a sign-in…'), ), ], ); } final later = cupertino ? CupertinoButton( key: const Key('server-sign-in-later'), onPressed: _busy ? null : widget.onDone, child: const Text('Later'), ) : TextButton( key: const Key('server-sign-in-later'), onPressed: _busy ? null : widget.onDone, child: const Text('Later'), ); if (result.needsPassword) { final field = cupertino ? CupertinoTextField( key: const Key('server-profile-password'), controller: _password, obscureText: true, autofocus: true, enabled: !_busy, placeholder: 'Password', onSubmitted: (_) => _signIn(), ) : TextField( key: const Key('server-profile-password'), controller: _password, obscureText: true, autofocus: true, enabled: !_busy, decoration: const InputDecoration( labelText: 'Password', border: OutlineInputBorder(), ), onSubmitted: (_) => _signIn(), ); final signIn = cupertino ? CupertinoButton.filled( key: const Key('server-profile-sign-in'), onPressed: _busy ? null : _signIn, child: Text(_busy ? 'Signing in…' : 'Sign in'), ) : FilledButton( key: const Key('server-profile-sign-in'), onPressed: _busy ? null : _signIn, child: Text(_busy ? 'Signing in…' : 'Sign in'), ); return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text( '$name is saved and asks for its own password: the one chosen ' 'when Pipelines was installed there. Other machines\' sign-ins ' 'are never reused.', ), const SizedBox(height: 12), field, if (_error != null) ...[ const SizedBox(height: 8), Text( _error!, key: const Key('server-profile-error'), style: TextStyle(color: errorColor), ), ], const SizedBox(height: 16), Row(children: [signIn, const SizedBox(width: 8), later]), ], ); } final retry = cupertino ? CupertinoButton.filled( key: const Key('server-profile-retry'), onPressed: _busy ? null : _probe, child: const Text('Try again'), ) : FilledButton( key: const Key('server-profile-retry'), onPressed: _busy ? null : _probe, child: const Text('Try again'), ); return Column( crossAxisAlignment: CrossAxisAlignment.stretch, children: [ Text('$name is saved, but could not be reached.'), if (result.detail != null && result.detail!.isNotEmpty) ...[ const SizedBox(height: 8), Text( result.detail!, key: const Key('server-profile-error'), style: TextStyle(color: errorColor), ), ], const SizedBox(height: 16), Row(children: [retry, const SizedBox(width: 8), later]), ], ); } }