diff --git a/lib/backend/api.dart b/lib/backend/api.dart index 7751155..87fd3d6 100644 --- a/lib/backend/api.dart +++ b/lib/backend/api.dart @@ -74,9 +74,11 @@ class Api { await _connection.connect(endpoint.host, endpoint.port); } catch (e) { logger.e('Не удалось подключиться: $e'); - _cleanup(); - _setSessionState(SessionState.disconnected); - _scheduleReconnect(); + if (_sessionState != SessionState.disconnected) { + _cleanup(); + _setSessionState(SessionState.disconnected); + _scheduleReconnect(); + } return; } diff --git a/lib/frontend/screens/auth/server_settings_sheet.dart b/lib/frontend/screens/auth/server_settings_sheet.dart index 9c85651..534338a 100644 --- a/lib/frontend/screens/auth/server_settings_sheet.dart +++ b/lib/frontend/screens/auth/server_settings_sheet.dart @@ -53,9 +53,14 @@ class _ServerSettingsSheetState extends State { await prefs.setString(ServerConfig.prefHostKey, host); await prefs.setInt(ServerConfig.prefPortKey, port); await api.disconnect(); - await api.connect(); + api.connect(); + final online = await api.stateStream + .firstWhere((s) => + s == SessionState.online || s == SessionState.disconnected) + .timeout(const Duration(seconds: 15), + onTimeout: () => SessionState.disconnected); if (!mounted) return; - if (api.state == SessionState.online) { + if (online == SessionState.online) { showCustomNotification(context, l10n.serverSettingsSaved); } else { showCustomNotification(context, l10n.serverReconnectFailed); @@ -74,9 +79,14 @@ class _ServerSettingsSheetState extends State { _hostController.text = ServerConfig.defaultHost; _portController.text = '${ServerConfig.defaultPort}'; await api.disconnect(); - await api.connect(); + api.connect(); + final online = await api.stateStream + .firstWhere((s) => + s == SessionState.online || s == SessionState.disconnected) + .timeout(const Duration(seconds: 15), + onTimeout: () => SessionState.disconnected); if (!mounted) return; - if (api.state == SessionState.online) { + if (online == SessionState.online) { showCustomNotification(context, l10n.serverSettingsSaved); } else { showCustomNotification(context, l10n.serverReconnectFailed);