diff --git a/lib/features/connections/connections_panel.dart b/lib/features/connections/connections_panel.dart index 1bcc9a0c..2b823f63 100644 --- a/lib/features/connections/connections_panel.dart +++ b/lib/features/connections/connections_panel.dart @@ -485,6 +485,7 @@ class ConnectionsPanel extends StatefulWidget { this.onSqliteObjectSelected, this.onSqliteOpenSqlWorkspace, this.onExtensionObjectSelected, + this.onConnectionRemoved, /// When true, [initState] does not call [_loadData]. Widget tests that seed /// SQLite in setUp should call [ConnectionsPanelState.reloadConnectionsFromDb] @@ -565,6 +566,10 @@ class ConnectionsPanel extends StatefulWidget { String name, )? onExtensionObjectSelected; + /// Called after a connection was removed, so the shell can leave its + /// workspace if it was the open one. + final void Function(int connectionId)? onConnectionRemoved; + final bool skipInitialDbLoadForTest; @override @@ -802,9 +807,31 @@ class ConnectionsPanelState extends State { ); } + /// Asks first: removing a connection deletes its saved settings and + /// password and closes its sessions. The database itself is not touched. Future _removeConnection(int id) async { + String? name; + for (final c in _connections) { + if (c.id == id) { + name = c.name; + break; + } + } + final confirmed = await QueryaConfirmDialog.show( + context: context, + title: 'Remove connection?', + message: name == null || name.isEmpty + ? 'The connection and its saved password are removed from Querya. ' + 'The database itself is not changed.' + : '"$name" and its saved password are removed from Querya. ' + 'The database itself is not changed.', + confirmLabel: 'Remove', + isDestructive: true, + ); + if (confirmed != true || !mounted) return; await _closeSessionsOf(id); await LocalDb.instance.removeConnection(id); + widget.onConnectionRemoved?.call(id); await _loadData(); } diff --git a/lib/features/main_screen/main_screen.dart b/lib/features/main_screen/main_screen.dart index 6eb95e71..c80f7c29 100644 --- a/lib/features/main_screen/main_screen.dart +++ b/lib/features/main_screen/main_screen.dart @@ -1754,6 +1754,12 @@ class _ConnectionsPanelSlotState extends State<_ConnectionsPanelSlot> { onSqliteObjectSelected: widget.onSqliteObjectSelected, onSqliteOpenSqlWorkspace: widget.onSqliteOpenSqlWorkspace, onExtensionObjectSelected: widget.onExtensionObjectSelected, + onConnectionRemoved: (id) { + // The open workspace belonged to the removed connection: go home. + if (widget.workspace.value.activeConnection?.id != id) return; + widget.workspace.value = MainScreenWorkspaceState.empty; + QueryaShellStatus.instance.clear(); + }, ); } }