@@ -21,7 +21,7 @@ struct ConnectedView: View {
2121 @State private var appError : AppError ?
2222 @State private var failureAlertMessage : String ?
2323 @State private var showFailureAlert = false
24- @State private var selectedTab = ConnectedTab . tables
24+ @AppStorage ( " lastSelectedTab " ) private var selectedTabRaw : String = ConnectedTab . tables. rawValue
2525 @State private var queryHistory : [ QueryHistoryItem ] = [ ]
2626 @State private var historyStorage = QueryHistoryStorage ( )
2727 @State private var databases : [ String ] = [ ]
@@ -40,6 +40,18 @@ struct ConnectedView: View {
4040 case query = " Query "
4141 }
4242
43+ private var selectedTab : ConnectedTab {
44+ get { ConnectedTab ( rawValue: selectedTabRaw) ?? . tables }
45+ set { selectedTabRaw = newValue. rawValue }
46+ }
47+
48+ private var selectedTabBinding : Binding < ConnectedTab > {
49+ Binding (
50+ get: { ConnectedTab ( rawValue: selectedTabRaw) ?? . tables } ,
51+ set: { selectedTabRaw = $0. rawValue }
52+ )
53+ }
54+
4355 private var displayName : String {
4456 connection. name. isEmpty ? connection. host : connection. name
4557 }
@@ -119,7 +131,7 @@ struct ConnectedView: View {
119131 . navigationTitle ( supportsDatabaseSwitching && databases. count > 1 ? " " : displayName)
120132 . navigationBarTitleDisplayMode ( . inline)
121133 . safeAreaInset ( edge: . top) {
122- Picker ( " Tab " , selection: $selectedTab ) {
134+ Picker ( " Tab " , selection: selectedTabBinding ) {
123135 Text ( " Tables " ) . tag ( ConnectedTab . tables)
124136 Text ( " Query " ) . tag ( ConnectedTab . query)
125137 }
@@ -128,10 +140,10 @@ struct ConnectedView: View {
128140 . padding ( . vertical, 8 )
129141 }
130142 . background {
131- Button ( " " ) { selectedTab = . tables }
143+ Button ( " " ) { selectedTabRaw = ConnectedTab . tables. rawValue }
132144 . keyboardShortcut ( " 1 " , modifiers: . command)
133145 . hidden ( )
134- Button ( " " ) { selectedTab = . query }
146+ Button ( " " ) { selectedTabRaw = ConnectedTab . query. rawValue }
135147 . keyboardShortcut ( " 2 " , modifiers: . command)
136148 . hidden ( )
137149 }
@@ -203,12 +215,23 @@ struct ConnectedView: View {
203215 }
204216 }
205217 . onAppear {
218+ let key = connection. id. uuidString
219+ activeDatabase = UserDefaults . standard. string ( forKey: " lastDB. \( key) " ) ?? " "
220+ activeSchema = UserDefaults . standard. string ( forKey: " lastSchema. \( key) " ) ?? " public "
221+
206222 let hasDriver = appState. connectionManager. session ( for: connection. id) ? . driver != nil
207223 if !hasDriver, !isConnecting {
208224 appError = nil
209225 Task { await connect ( ) }
210226 }
211227 }
228+ . onChange ( of: activeDatabase) { _, newValue in
229+ guard !newValue. isEmpty else { return }
230+ UserDefaults . standard. set ( newValue, forKey: " lastDB. \( connection. id. uuidString) " )
231+ }
232+ . onChange ( of: activeSchema) { _, newValue in
233+ UserDefaults . standard. set ( newValue, forKey: " lastSchema. \( connection. id. uuidString) " )
234+ }
212235 . onChange ( of: scenePhase) { _, phase in
213236 if phase == . active, session != nil {
214237 Task { await reconnectIfNeeded ( ) }
@@ -271,18 +294,13 @@ struct ConnectedView: View {
271294
272295 do {
273296 let session = try await appState. connectionManager. connect ( connection)
274- guard !Task. isCancelled else {
275- await appState. connectionManager. disconnect ( connection. id)
276- return
277- }
278297 self . session = session
279298 self . tables = try await session. driver. fetchTables ( schema: nil )
280299 isConnecting = false
281300 hapticSuccess. toggle ( )
282301 await loadDatabases ( )
283302 await loadSchemas ( )
284303 } catch {
285- guard !Task. isCancelled else { return }
286304 let context = ErrorContext (
287305 operation: " connect " ,
288306 databaseType: connection. type,
@@ -324,8 +342,14 @@ struct ConnectedView: View {
324342 guard let session, supportsDatabaseSwitching else { return }
325343 do {
326344 databases = try await session. driver. fetchDatabases ( )
327- // Use session's active database (may differ from connection.database after a switch)
328- if let stored = appState. connectionManager. session ( for: connection. id) {
345+ if !activeDatabase. isEmpty, databases. contains ( activeDatabase) {
346+ let sessionDB = appState. connectionManager. session ( for: connection. id) ? . activeDatabase ?? connection. database
347+ if activeDatabase != sessionDB {
348+ let target = activeDatabase
349+ activeDatabase = sessionDB
350+ await switchDatabase ( to: target)
351+ }
352+ } else if let stored = appState. connectionManager. session ( for: connection. id) {
329353 activeDatabase = stored. activeDatabase
330354 } else {
331355 activeDatabase = connection. database
@@ -339,7 +363,14 @@ struct ConnectedView: View {
339363 guard let session, supportsSchemas else { return }
340364 do {
341365 schemas = try await session. driver. fetchSchemas ( )
342- activeSchema = session. driver. currentSchema ?? " public "
366+ let currentSchema = session. driver. currentSchema ?? " public "
367+ if schemas. contains ( activeSchema) , activeSchema != currentSchema {
368+ let target = activeSchema
369+ activeSchema = currentSchema
370+ await switchSchema ( to: target)
371+ } else if !schemas. contains ( activeSchema) {
372+ activeSchema = currentSchema
373+ }
343374 } catch {
344375 // Silently fail — don't show picker
345376 }
0 commit comments