Files
pipelines/app/lib/features/settings/saved_server_sign_in_step.dart
alan a7c41063fa Remove Cupertino branches from Settings
Keep the Material-only settings implementation and its focused tests while preserving public cupertino parameters for later cleanup. This removes unreachable platform-specific widgets and test variants without changing the Material path.
2026-11-11 05:05:57 +00:00

179 lines
4.8 KiB
Dart

import 'package:flutter/material.dart';
import '../../core/servers/machine_access.dart';
import '../../core/servers/server_catalog.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<SavedServerSignInStep> createState() => _SavedServerSignInStepState();
}
class _SavedServerSignInStepState extends State<SavedServerSignInStep> {
final _password = TextEditingController();
MachineAccessResult? _result;
bool _busy = true;
String? _error;
@override
void initState() {
super.initState();
_probe();
}
@override
void dispose() {
_password.dispose();
super.dispose();
}
Future<void> _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<void> _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 name = widget.server.name;
final errorColor = Theme.of(context).colorScheme.error;
final result = _result;
if (result == null) {
return Row(
children: [
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 = TextButton(
key: const Key('server-sign-in-later'),
onPressed: _busy ? null : widget.onDone,
child: const Text('Later'),
);
if (result.needsPassword) {
final field = 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 = 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 = 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]),
],
);
}
}