Skip to content

Commit 3d82787

Browse files
authored
Merge pull request #352 from datlechin/fix/filter-state-persistence
fix: persist filter state across tab switches and clear on table replacement
2 parents f550c15 + 5e0ced9 commit 3d82787

4 files changed

Lines changed: 16 additions & 5 deletions

File tree

TablePro/Views/Main/Extensions/MainContentCoordinator+Filtering.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ extension MainContentCoordinator {
5959
self.filterStateManager.saveLastFilters(for: capturedTableName)
6060
}
6161

62+
// Persist filter state to tab so it survives tab switches
63+
self.tabManager.tabs[capturedTabIndex].filterState = self.filterStateManager.saveToTabState()
64+
6265
self.runQuery()
6366
}
6467
}
@@ -107,6 +110,7 @@ extension MainContentCoordinator {
107110
}
108111

109112
self.tabManager.tabs[capturedTabIndex].query = newQuery
113+
self.tabManager.tabs[capturedTabIndex].filterState = self.filterStateManager.saveToTabState()
110114
self.runQuery()
111115
}
112116
}
@@ -146,6 +150,7 @@ extension MainContentCoordinator {
146150
}
147151

148152
self.tabManager.tabs[capturedTabIndex].query = newQuery
153+
self.tabManager.tabs[capturedTabIndex].filterState = self.filterStateManager.saveToTabState()
149154
self.runQuery()
150155
}
151156
}

TablePro/Views/Main/Extensions/MainContentCoordinator+Navigation.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,7 @@ extension MainContentCoordinator {
113113
databaseType: connection.type,
114114
databaseName: currentDatabase
115115
) {
116+
filterStateManager.clearAll()
116117
if let tabIndex = tabManager.selectedTabIndex {
117118
tabManager.tabs[tabIndex].pagination.reset()
118119
toolbarState.isTableTab = true
@@ -180,6 +181,7 @@ extension MainContentCoordinator {
180181
databaseName: databaseName,
181182
isPreview: true
182183
)
184+
previewCoordinator.filterStateManager.clearAll()
183185
if let tabIndex = previewCoordinator.tabManager.selectedTabIndex {
184186
previewCoordinator.tabManager.tabs[tabIndex].showStructure = showStructure
185187
previewCoordinator.tabManager.tabs[tabIndex].pagination.reset()
@@ -206,6 +208,7 @@ extension MainContentCoordinator {
206208
databaseName: databaseName,
207209
isPreview: true
208210
)
211+
filterStateManager.clearAll()
209212
if let tabIndex = tabManager.selectedTabIndex {
210213
tabManager.tabs[tabIndex].showStructure = showStructure
211214
tabManager.tabs[tabIndex].pagination.reset()

TablePro/Views/Main/Extensions/MainContentCoordinator+TabSwitch.swift

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ extension MainContentCoordinator {
1818
isHandlingTabSwitch = true
1919
defer { isHandlingTabSwitch = false }
2020

21-
// Persist the outgoing tab's unsaved changes so they survive the switch
21+
// Persist the outgoing tab's unsaved changes and filter state so they survive the switch
2222
if let oldId = oldTabId,
23-
let oldIndex = tabManager.tabs.firstIndex(where: { $0.id == oldId }),
24-
changeManager.hasChanges
23+
let oldIndex = tabManager.tabs.firstIndex(where: { $0.id == oldId })
2524
{
26-
tabManager.tabs[oldIndex].pendingChanges = changeManager.saveState()
25+
if changeManager.hasChanges {
26+
tabManager.tabs[oldIndex].pendingChanges = changeManager.saveState()
27+
}
28+
tabManager.tabs[oldIndex].filterState = filterStateManager.saveToTabState()
2729
}
2830

2931
if tabManager.tabs.count > 2 {

TablePro/Views/Main/MainContentView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,14 @@ struct MainContentView: View {
336336
// Skip if the user has unsaved changes (in-memory or tab-level).
337337
let hasPendingEdits = changeManager.hasChanges
338338
|| (tabManager.selectedTab?.pendingChanges.hasChanges ?? false)
339+
let isConnected = DatabaseManager.shared.activeSessions[connection.id]?.isConnected ?? false
339340
let needsLazyLoad = tabManager.selectedTab.map { tab in
340341
tab.tabType == .table
341342
&& (tab.resultRows.isEmpty || tab.rowBuffer.isEvicted)
342343
&& (tab.lastExecutedAt == nil || tab.rowBuffer.isEvicted)
343344
&& !tab.query.trimmingCharacters(in: .whitespacesAndNewlines).isEmpty
344345
} ?? false
345-
if needsLazyLoad && !hasPendingEdits {
346+
if needsLazyLoad && !hasPendingEdits && isConnected {
346347
coordinator.runQuery()
347348
}
348349
}

0 commit comments

Comments
 (0)