import 'package:flutter/foundation.dart'; import '../../core/servers/server_catalog.dart'; import '../../core/servers/server_catalog_store.dart'; /// Saved configurations are committed before switching the app's connection. /// A failed save leaves the old selection and its live socket unchanged. class ServerConnections extends ChangeNotifier { ServerConnections({required this.store, required this.onSelected}); final ServerCatalogStore store; final Future Function(SavedServer?) onSelected; ServerCatalog _catalog = ServerCatalog(); bool _busy = false; String? _error; bool _disposed = false; ServerCatalog get catalog => _catalog; bool get busy => _busy; String? get error => _error; /// Browsers always connect to their page origin, even after Back or a failed /// cross-origin navigation. Do not label another saved profile as active. void alignBrowserOrigin(Uri endpoint) { String origin(Uri uri) => uri.replace(scheme: uri.scheme == 'wss' ? 'https' : 'http').origin; final current = origin(endpoint); String? matched; for (final server in _catalog.servers) { if (server.endpoints.any((url) { final candidate = Uri.parse(url); return origin(candidate) == current && candidate.path == endpoint.path && candidate.query == endpoint.query; })) { matched = server.id; break; } } _catalog = ServerCatalog(servers: _catalog.servers, selectedId: matched); _notify(); } Future persist() => _change(() => _catalog, activate: false); Future load({String legacyEndpoint = ''}) async { try { final loaded = await store.load(legacyEndpoint: legacyEndpoint); if (!_disposed) _catalog = loaded; } catch (error) { if (!_disposed) _error = 'Saved servers could not be read: $error'; } _notify(); } Future save(SavedServer server, {bool activate = true}) => _change( () { final next = _catalog.upsert(server); return _catalog.active == null ? next.select(server.id) : next; }, activate: activate && (_catalog.active?.id == server.id || _catalog.active == null), ); Future select(String id, {bool activate = true}) => _change( () => _catalog.select(id), activate: activate, rollbackSelection: true, ); Future remove(String id) => _change(() => _catalog.remove(id), activate: _catalog.active?.id == id); Future _change( ServerCatalog Function() change, { required bool activate, bool rollbackSelection = false, }) async { if (_busy || _disposed) return false; // A corrupt catalog is never silently replaced by an empty configuration. if (_error?.startsWith('Saved servers could not be read:') ?? false) { return false; } _busy = true; final previous = _catalog; _error = null; _notify(); try { final next = change(); if (!await store.save(next)) { throw StateError('The server configuration could not be saved.'); } if (_disposed) return false; _catalog = next; _notify(); if (activate) { try { await onSelected(next.active); } catch (_) { if (rollbackSelection && await store.save(previous) && !_disposed) { _catalog = previous; } rethrow; } } return true; } catch (error) { if (!_disposed) _error = '$error'; return false; } finally { _busy = false; _notify(); } } void _notify() { if (!_disposed) notifyListeners(); } @override void dispose() { _disposed = true; super.dispose(); } }