支持现场服务器地址配置
This commit is contained in:
@@ -0,0 +1,319 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_bloc/flutter_bloc.dart';
|
||||
import 'package:go_router/go_router.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import '../../../core/di/injection.dart';
|
||||
import '../../../core/env/env_config.dart';
|
||||
import '../../../core/env/server_configuration.dart';
|
||||
import '../../../core/localization/app_localizations.dart';
|
||||
import '../../../core/network/mqtt/mqtt_manager.dart';
|
||||
import '../../../core/router/route_paths.dart';
|
||||
import '../../auth/presentation/bloc/auth_cubit.dart';
|
||||
import '../../auth/presentation/bloc/auth_state.dart';
|
||||
|
||||
class ServerConfigurationPage extends StatefulWidget {
|
||||
const ServerConfigurationPage({super.key});
|
||||
|
||||
@override
|
||||
State<ServerConfigurationPage> createState() =>
|
||||
_ServerConfigurationPageState();
|
||||
}
|
||||
|
||||
class _ServerConfigurationPageState extends State<ServerConfigurationPage> {
|
||||
final _host = TextEditingController();
|
||||
final _inputs = {
|
||||
for (final service in ServerService.values)
|
||||
service: TextEditingController(),
|
||||
};
|
||||
final _advanced = ExpansibleController();
|
||||
final _invalidServices = <ServerService>{};
|
||||
Map<ServerService, ({Uri address, bool reachable})> _results = {};
|
||||
bool _invalidHost = false;
|
||||
bool _saving = false;
|
||||
bool _testing = false;
|
||||
|
||||
bool get _busy => _saving || _testing;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
final config = EnvConfig.serverConfiguration;
|
||||
_host.text = config.sharedHost;
|
||||
for (final entry in config.overrides.entries) {
|
||||
_inputs[entry.key]!.text = entry.value;
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_host.dispose();
|
||||
for (final controller in _inputs.values) {
|
||||
controller.dispose();
|
||||
}
|
||||
_advanced.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
ServerConfiguration? _readDraft() {
|
||||
final host = _host.text.trim();
|
||||
final invalidHost = host.isNotEmpty && !ServerConfiguration.isHost(host);
|
||||
final effectiveHost = host.isEmpty || invalidHost
|
||||
? ServerConfiguration.defaultHost
|
||||
: host;
|
||||
final invalidServices = <ServerService>{};
|
||||
final inputs = <ServerService, String>{};
|
||||
for (final entry in _inputs.entries) {
|
||||
final input = entry.value.text.trim();
|
||||
inputs[entry.key] = input;
|
||||
try {
|
||||
entry.key.resolve(
|
||||
input,
|
||||
production: EnvConfig.isProduction,
|
||||
commonHost: effectiveHost,
|
||||
);
|
||||
} on FormatException {
|
||||
invalidServices.add(entry.key);
|
||||
}
|
||||
}
|
||||
|
||||
setState(() {
|
||||
_invalidHost = invalidHost;
|
||||
_invalidServices
|
||||
..clear()
|
||||
..addAll(invalidServices);
|
||||
});
|
||||
if (invalidServices.any((service) => !service.isCore)) _advanced.expand();
|
||||
if (invalidHost || invalidServices.isNotEmpty) return null;
|
||||
return ServerConfiguration(
|
||||
production: EnvConfig.isProduction,
|
||||
sharedHost: host,
|
||||
overrides: inputs,
|
||||
);
|
||||
}
|
||||
|
||||
Future<void> _testConnection() async {
|
||||
FocusScope.of(context).unfocus();
|
||||
final draft = _readDraft();
|
||||
if (draft == null) return;
|
||||
setState(() {
|
||||
_testing = true;
|
||||
_results = {};
|
||||
});
|
||||
final results = await Future.wait(
|
||||
ServerService.values.map((service) async {
|
||||
final address = draft.address(service);
|
||||
var reachable = false;
|
||||
try {
|
||||
final socket = await Socket.connect(
|
||||
address.host,
|
||||
address.port,
|
||||
timeout: const Duration(seconds: 3),
|
||||
);
|
||||
socket.destroy();
|
||||
reachable = true;
|
||||
} on SocketException {
|
||||
// A failed reachability check does not prevent saving the address.
|
||||
}
|
||||
return MapEntry(service, (address: address, reachable: reachable));
|
||||
}),
|
||||
);
|
||||
if (!mounted) return;
|
||||
setState(() {
|
||||
_testing = false;
|
||||
_results = Map.fromEntries(results);
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _save() async {
|
||||
FocusScope.of(context).unfocus();
|
||||
final draft = _readDraft();
|
||||
if (draft == null) return;
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final previous = EnvConfig.serverConfiguration;
|
||||
bool changed(ServerService service) =>
|
||||
previous.address(service) != draft.address(service);
|
||||
final auth = context.read<AuthCubit>();
|
||||
final relogin =
|
||||
auth.state is AuthAuthenticated && ServerService.values.any(changed);
|
||||
final reconnectMqtt =
|
||||
relogin ||
|
||||
changed(ServerService.mqtt) ||
|
||||
changed(ServerService.droneMqtt);
|
||||
setState(() => _saving = true);
|
||||
|
||||
Future<void> applyConfiguration() async {
|
||||
try {
|
||||
await EnvConfig.saveServerConfiguration(sl<SharedPreferences>(), draft);
|
||||
} finally {
|
||||
if (reconnectMqtt) await MqttManager().reconnect();
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
if (relogin) {
|
||||
await auth.logoutForServerChange(applyConfiguration);
|
||||
} else {
|
||||
await applyConfiguration();
|
||||
}
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(
|
||||
context,
|
||||
).showSnackBar(SnackBar(content: Text(l10n.translate('servers.saved'))));
|
||||
if (!relogin && context.canPop()) {
|
||||
context.pop();
|
||||
} else {
|
||||
context.go(
|
||||
auth.state is AuthAuthenticated ? RoutePaths.home : RoutePaths.login,
|
||||
);
|
||||
}
|
||||
} catch (_) {
|
||||
if (!mounted) return;
|
||||
ScaffoldMessenger.of(context).showSnackBar(
|
||||
SnackBar(content: Text(l10n.translate('servers.save_failed'))),
|
||||
);
|
||||
} finally {
|
||||
if (mounted) setState(() => _saving = false);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
final l10n = AppLocalizations.of(context);
|
||||
final loggedIn = context.select(
|
||||
(AuthCubit auth) => auth.state is AuthAuthenticated,
|
||||
);
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text(l10n.translate('servers.title'))),
|
||||
body: SafeArea(
|
||||
child: Center(
|
||||
child: ConstrainedBox(
|
||||
constraints: const BoxConstraints(maxWidth: 600),
|
||||
child: SingleChildScrollView(
|
||||
padding: const EdgeInsets.fromLTRB(16, 12, 16, 20),
|
||||
child: Column(
|
||||
crossAxisAlignment: CrossAxisAlignment.stretch,
|
||||
children: [
|
||||
Text(l10n.translate('servers.help')),
|
||||
const SizedBox(height: 12),
|
||||
TextField(
|
||||
controller: _host,
|
||||
enabled: !_busy,
|
||||
keyboardType: TextInputType.url,
|
||||
autocorrect: false,
|
||||
onChanged: (_) => setState(() {
|
||||
_invalidHost = false;
|
||||
_results = {};
|
||||
}),
|
||||
decoration: InputDecoration(
|
||||
border: const OutlineInputBorder(),
|
||||
labelText: l10n.translate('servers.shared_host'),
|
||||
hintText: ServerConfiguration.defaultHost,
|
||||
errorText: _invalidHost
|
||||
? l10n.translate('servers.invalid_host')
|
||||
: null,
|
||||
),
|
||||
),
|
||||
for (final service in ServerService.values.where(
|
||||
(service) => service.isCore,
|
||||
))
|
||||
_serviceField(service, l10n),
|
||||
const SizedBox(height: 12),
|
||||
ExpansionTile(
|
||||
controller: _advanced,
|
||||
title: Text(l10n.translate('servers.advanced')),
|
||||
tilePadding: EdgeInsets.zero,
|
||||
children: [
|
||||
for (final service in ServerService.values.where(
|
||||
(service) => !service.isCore,
|
||||
))
|
||||
_serviceField(service, l10n),
|
||||
],
|
||||
),
|
||||
if (loggedIn) ...[
|
||||
const SizedBox(height: 12),
|
||||
Text(l10n.translate('servers.relogin')),
|
||||
],
|
||||
if (_saving || _testing) ...[
|
||||
const SizedBox(height: 12),
|
||||
const LinearProgressIndicator(),
|
||||
],
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
bottomNavigationBar: SafeArea(
|
||||
minimum: const EdgeInsets.fromLTRB(16, 8, 16, 12),
|
||||
child: Wrap(
|
||||
alignment: WrapAlignment.center,
|
||||
spacing: 12,
|
||||
runSpacing: 8,
|
||||
children: [
|
||||
OutlinedButton(
|
||||
onPressed: _busy ? null : _testConnection,
|
||||
child: Text(l10n.translate('servers.test_ports')),
|
||||
),
|
||||
FilledButton(
|
||||
onPressed: _busy ? null : _save,
|
||||
child: Text(l10n.translate('servers.save')),
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Widget _serviceField(ServerService service, AppLocalizations l10n) {
|
||||
final input = _inputs[service]!;
|
||||
final host = _host.text.trim();
|
||||
final sharedHost = ServerConfiguration.isHost(host)
|
||||
? host
|
||||
: ServerConfiguration.defaultHost;
|
||||
Uri address;
|
||||
try {
|
||||
address = service.resolve(
|
||||
input.text.trim(),
|
||||
production: EnvConfig.isProduction,
|
||||
commonHost: sharedHost,
|
||||
);
|
||||
} on FormatException {
|
||||
address = service.defaultAddress(
|
||||
production: EnvConfig.isProduction,
|
||||
commonHost: sharedHost,
|
||||
);
|
||||
}
|
||||
final result = _results[service];
|
||||
final text = result == null
|
||||
? address.toString()
|
||||
: l10n.translate(
|
||||
result.reachable ? 'servers.reachable' : 'servers.unreachable',
|
||||
{'address': result.address.toString()},
|
||||
);
|
||||
return Padding(
|
||||
padding: const EdgeInsets.only(top: 12),
|
||||
child: TextField(
|
||||
controller: input,
|
||||
enabled: !_busy,
|
||||
keyboardType: TextInputType.url,
|
||||
autocorrect: false,
|
||||
onChanged: (_) => setState(() {
|
||||
_invalidServices.remove(service);
|
||||
_results = {};
|
||||
}),
|
||||
decoration: InputDecoration(
|
||||
border: const OutlineInputBorder(),
|
||||
labelText: l10n.translate('servers.${service.name}'),
|
||||
helperText: text,
|
||||
helperMaxLines: 2,
|
||||
errorText: _invalidServices.contains(service)
|
||||
? l10n.translate('servers.invalid_address')
|
||||
: null,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user