44//
55
66import SwiftUI
7+ import os
78import TableProModels
89import TableProSync
910
1011struct ConnectionListView : View {
12+ private static let logger = Logger ( subsystem: " com.TablePro " , category: " ConnectionListView " )
1113 @Environment ( AppState . self) private var appState
1214 @Environment ( \. horizontalSizeClass) private var sizeClass
1315 @State private var showingAddConnection = false
1416 @State private var editingConnection : DatabaseConnection ?
15- @State private var selectedConnectionId : UUID ?
17+ @AppStorage ( " lastConnectionId " ) private var selectedConnectionIdString : String ?
1618 @State private var columnVisibility : NavigationSplitViewVisibility = . automatic
1719 @State private var showingGroupManagement = false
1820 @State private var showingTagManagement = false
19- @State private var filterTagId : UUID ?
20- @State private var groupByGroup = false
21+ @AppStorage ( " lastFilterTagId " ) private var filterTagIdString : String ?
22+ @AppStorage ( " groupByGroup " ) private var groupByGroup = false
2123 @State private var editMode : EditMode = . inactive
2224 @State private var connectionToDelete : DatabaseConnection ?
2325
@@ -28,6 +30,21 @@ struct ConnectionListView: View {
2830 )
2931 }
3032
33+ private var selectedConnectionId : Binding < UUID ? > {
34+ Binding (
35+ get: { selectedConnectionIdString. flatMap { UUID ( uuidString: $0) } } ,
36+ set: { selectedConnectionIdString = $0? . uuidString }
37+ )
38+ }
39+
40+ private var selectedConnectionUUID : UUID ? {
41+ selectedConnectionIdString. flatMap { UUID ( uuidString: $0) }
42+ }
43+
44+ private var filterTagId : UUID ? {
45+ filterTagIdString. flatMap { UUID ( uuidString: $0) }
46+ }
47+
3148 private var displayedConnections : [ DatabaseConnection ] {
3249 var result = appState. connections
3350 if let filterTagId {
@@ -44,11 +61,12 @@ struct ConnectionListView: View {
4461 }
4562
4663 private var selectedConnection : DatabaseConnection ? {
47- guard let selectedConnectionId else { return nil }
48- return appState. connections. first { $0. id == selectedConnectionId }
64+ guard let id = selectedConnectionUUID else { return nil }
65+ return appState. connections. first { $0. id == id }
4966 }
5067
5168 var body : some View {
69+ let _ = Self . logger. info ( " ConnectionListView.body — selectedConnectionIdString= \( selectedConnectionIdString ?? " nil " ) selectedConnectionUUID= \( selectedConnectionUUID? . uuidString ?? " nil " ) selectedConnection= \( selectedConnection? . name ?? " nil " ) connections.count= \( appState. connections. count) " )
5270 NavigationSplitView ( columnVisibility: $columnVisibility) {
5371 sidebar
5472 . navigationTitle ( " Connections " )
@@ -90,7 +108,7 @@ struct ConnectionListView: View {
90108 . onChange ( of: appState. pendingConnectionId) { _, newId in
91109 navigateToPendingConnection ( newId)
92110 }
93- . onChange ( of: filterTagId ) {
111+ . onChange ( of: filterTagIdString ) {
94112 editMode = . inactive
95113 }
96114 . onChange ( of: groupByGroup) {
@@ -102,9 +120,11 @@ struct ConnectionListView: View {
102120 } detail: {
103121 NavigationStack {
104122 if let connection = selectedConnection {
123+ let _ = Self . logger. info ( " detail: showing ConnectedView for \( connection. name) " )
105124 ConnectedView ( connection: connection)
106125 . id ( connection. id)
107126 } else {
127+ let _ = Self . logger. info ( " detail: no selectedConnection, showing empty state " )
108128 ContentUnavailableView (
109129 " Select a Connection " ,
110130 systemImage: " server.rack " ,
@@ -135,7 +155,7 @@ struct ConnectionListView: View {
135155
136156 @ViewBuilder
137157 private var connectionList : some View {
138- let list = List ( selection: $ selectedConnectionId) {
158+ let list = List ( selection: selectedConnectionId) {
139159 if groupByGroup {
140160 groupedContent
141161 } else {
@@ -201,8 +221,8 @@ struct ConnectionListView: View {
201221 ) {
202222 Button ( String ( localized: " Delete " ) , role: . destructive) {
203223 if let connection = connectionToDelete {
204- if selectedConnectionId == connection. id {
205- selectedConnectionId = nil
224+ if selectedConnectionUUID == connection. id {
225+ selectedConnectionIdString = nil
206226 }
207227 appState. removeConnection ( connection)
208228 }
@@ -222,7 +242,7 @@ struct ConnectionListView: View {
222242 if !appState. tags. isEmpty {
223243 Section ( " Filter by Tag " ) {
224244 Button {
225- filterTagId = nil
245+ filterTagIdString = nil
226246 } label: {
227247 HStack {
228248 Text ( " All " )
@@ -233,7 +253,7 @@ struct ConnectionListView: View {
233253 }
234254 ForEach ( appState. tags) { tag in
235255 Button {
236- filterTagId = tag. id
256+ filterTagIdString = tag. id. uuidString
237257 } label: {
238258 HStack {
239259 Image ( systemName: " circle.fill " )
@@ -339,7 +359,7 @@ struct ConnectionListView: View {
339359 private func navigateToPendingConnection( _ id: UUID ? ) {
340360 guard let id,
341361 appState. connections. contains ( where: { $0. id == id } ) else { return }
342- selectedConnectionId = id
362+ selectedConnectionIdString = id. uuidString
343363 appState. pendingConnectionId = nil
344364 }
345365
0 commit comments