Skip to content

Commit 4e2325d

Browse files
committed
refactor: improve code quality and add debug logging
- Extract saveAllTabStates() helper method in AppDelegate - Add DEBUG-only error logging in TabStateStorage - Remove trailing whitespace and improve formatting - Clean up code structure for better readability
1 parent 85e6a7d commit 4e2325d

4 files changed

Lines changed: 19 additions & 25 deletions

File tree

OpenTable/AppDelegate.swift

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -58,42 +58,33 @@ class AppDelegate: NSObject, NSApplicationDelegate {
5858

5959
// Clean up window tracking
6060
configuredWindows.remove(ObjectIdentifier(window))
61-
61+
6262
// Check if main window is being closed
6363
if isMainWindow(window) {
6464
// CRITICAL: Save tab state SYNCHRONOUSLY before any async operations
6565
// Otherwise sessions might be cleared before we save
66-
let sessions = DatabaseManager.shared.activeSessions
67-
68-
for (connectionId, session) in sessions {
69-
// Only save if there are tabs; otherwise clear saved state
70-
if session.tabs.isEmpty {
71-
TabStateStorage.shared.clearTabState(connectionId: connectionId)
72-
} else {
73-
TabStateStorage.shared.saveTabState(
74-
connectionId: connectionId,
75-
tabs: session.tabs,
76-
selectedTabId: session.selectedTabId
77-
)
78-
}
79-
}
80-
66+
saveAllTabStates()
67+
8168
// NOW disconnect sessions asynchronously (after save is complete)
8269
Task { @MainActor in
8370
await DatabaseManager.shared.disconnectAll()
8471
}
85-
72+
8673
// Reopen welcome window after a brief delay
8774
DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
8875
self.openWelcomeWindow()
8976
}
9077
}
9178
}
92-
79+
9380
func applicationWillTerminate(_ notification: Notification) {
9481
// Save tab state synchronously before app terminates (backup mechanism)
95-
let sessions = DatabaseManager.shared.activeSessions
96-
for (connectionId, session) in sessions {
82+
saveAllTabStates()
83+
}
84+
85+
/// Save tab state for all active sessions
86+
private func saveAllTabStates() {
87+
for (connectionId, session) in DatabaseManager.shared.activeSessions {
9788
if session.tabs.isEmpty {
9889
TabStateStorage.shared.clearTabState(connectionId: connectionId)
9990
} else {

OpenTable/Core/Storage/TabStateStorage.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ struct TabState: Codable {
1313
let selectedTabId: UUID?
1414
}
1515

16-
1716
/// Service for persisting tab state per connection
1817
final class TabStateStorage {
1918
static let shared = TabStateStorage()
@@ -36,7 +35,9 @@ final class TabStateStorage {
3635
let key = tabStateKey(for: connectionId)
3736
defaults.set(data, forKey: key)
3837
} catch {
39-
// Silent failure - tab state is not critical
38+
#if DEBUG
39+
print("[TabStateStorage] Failed to encode tab state: \(error.localizedDescription)")
40+
#endif
4041
}
4142
}
4243

@@ -52,6 +53,9 @@ final class TabStateStorage {
5253
let decoder = JSONDecoder()
5354
return try decoder.decode(TabState.self, from: data)
5455
} catch {
56+
#if DEBUG
57+
print("[TabStateStorage] Failed to decode tab state: \(error.localizedDescription)")
58+
#endif
5559
return nil
5660
}
5761
}

OpenTable/Models/QueryTab.swift

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ struct PersistedTab: Codable {
2424
let tableName: String?
2525
}
2626

27-
2827
/// Stores pending changes for a tab (used to preserve state when switching tabs)
2928
struct TabPendingChanges: Equatable {
3029
var changes: [RowChange]
@@ -214,7 +213,6 @@ struct QueryTab: Identifiable, Equatable {
214213
}
215214
}
216215

217-
218216
/// Manager for query tabs
219217
final class QueryTabManager: ObservableObject {
220218
@Published var tabs: [QueryTab] = []

OpenTable/Views/MainContentView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -313,10 +313,11 @@ struct MainContentView: View {
313313
let session = DatabaseManager.shared.activeSessions[sessionId],
314314
!session.tabs.isEmpty {
315315
// Set flag to prevent onChange(tabManager.tabs) from syncing back
316+
// Use defer to ensure flag is always reset even if an error occurs
316317
isRestoringTabs = true
318+
defer { isRestoringTabs = false }
317319
tabManager.tabs = session.tabs
318320
tabManager.selectedTabId = session.selectedTabId
319-
isRestoringTabs = false
320321
}
321322
}
322323
.onChange(of: selectedTables) { oldTables, newTables in

0 commit comments

Comments
 (0)