Skip to content

Commit 2490b75

Browse files
committed
fix: favorites selection state, keyboard delete, reliable dialog query passing
1 parent e30abd4 commit 2490b75

4 files changed

Lines changed: 52 additions & 13 deletions

File tree

TablePro/Views/Editor/HistoryPanelView.swift

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ struct HistoryPanelView: View {
2121
@State private var searchTask: Task<Void, Never>?
2222
@State private var copyButtonTitle = "Copy Query"
2323
@State private var copyResetTask: Task<Void, Never>?
24-
@State private var showFavoriteDialog = false
25-
@State private var favoriteQuery: String?
24+
@State private var favoriteDialogQuery: FavoriteDialogQuery?
2625
@FocusedValue(\.commandActions) private var actions
2726

2827
private let dataProvider = HistoryDataProvider()
@@ -51,11 +50,11 @@ struct HistoryPanelView: View {
5150
.onReceive(NotificationCenter.default.publisher(for: .queryHistoryDidUpdate)) { _ in
5251
loadData()
5352
}
54-
.sheet(isPresented: $showFavoriteDialog) {
53+
.sheet(item: $favoriteDialogQuery) { item in
5554
FavoriteEditDialog(
5655
connectionId: UUID(),
5756
favorite: nil,
58-
initialQuery: favoriteQuery,
57+
initialQuery: item.query,
5958
forceGlobal: true
6059
)
6160
}
@@ -197,8 +196,7 @@ private extension HistoryPanelView {
197196
}
198197

199198
Button {
200-
favoriteQuery = entry.query
201-
showFavoriteDialog = true
199+
favoriteDialogQuery = FavoriteDialogQuery(query: entry.query)
202200
} label: {
203201
Label(String(localized: "Save as Favorite"), systemImage: "star")
204202
}

TablePro/Views/Main/Child/MainEditorContentView.swift

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ struct MainEditorContentView: View {
6666
@State private var tabProviderVersions: [UUID: Int] = [:]
6767
@State private var tabProviderMetaVersions: [UUID: Int] = [:]
6868
@State private var cachedChangeManager: AnyChangeManager?
69-
@State private var showFavoriteDialog = false
70-
@State private var favoriteQuery: String?
69+
@State private var favoriteDialogQuery: FavoriteDialogQuery?
7170

7271
// Native macOS window tabs — no LRU tracking needed (single tab per window)
7372

@@ -109,11 +108,11 @@ struct MainEditorContentView: View {
109108
}
110109
.background(.background)
111110
.animation(.easeInOut(duration: 0.2), value: isHistoryVisible)
112-
.sheet(isPresented: $showFavoriteDialog) {
111+
.sheet(item: $favoriteDialogQuery) { item in
113112
FavoriteEditDialog(
114113
connectionId: connectionId,
115114
favorite: nil,
116-
initialQuery: favoriteQuery
115+
initialQuery: item.query
117116
)
118117
}
119118
.onChange(of: tabManager.tabs.count) {
@@ -220,8 +219,8 @@ struct MainEditorContentView: View {
220219
coordinator.aiViewModel?.handleOptimizeSelection(text)
221220
},
222221
onSaveAsFavorite: { text in
223-
favoriteQuery = text.isEmpty ? nil : text
224-
showFavoriteDialog = true
222+
guard !text.isEmpty else { return }
223+
favoriteDialogQuery = FavoriteDialogQuery(query: text)
225224
}
226225
)
227226
}

TablePro/Views/Sidebar/FavoriteEditDialog.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
import SwiftUI
77

8+
/// Wrapper for `.sheet(item:)` to ensure the query is passed reliably
9+
struct FavoriteDialogQuery: Identifiable {
10+
let id = UUID()
11+
let query: String
12+
}
13+
814
/// Dialog for creating or editing a SQL favorite
915
struct FavoriteEditDialog: View {
1016
@Environment(\.dismiss) private var dismiss

TablePro/Views/Sidebar/FavoritesTabView.swift

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import SwiftUI
1010
/// Full-tab favorites view with folder hierarchy and bottom toolbar
1111
struct FavoritesTabView: View {
1212
@State private var viewModel: FavoritesSidebarViewModel
13+
@State private var selectedFavoriteIds: Set<String> = []
1314
@State private var folderToDelete: SQLFavoriteFolder?
1415
@State private var showDeleteFolderAlert = false
1516
let searchText: String
@@ -67,7 +68,7 @@ struct FavoritesTabView: View {
6768
// MARK: - List
6869

6970
private func favoritesList(_ items: [FavoriteTreeItem]) -> some View {
70-
List {
71+
List(selection: $selectedFavoriteIds) {
7172
ForEach(items) { item in
7273
FavoriteTreeItemRow(
7374
item: item,
@@ -78,10 +79,45 @@ struct FavoritesTabView: View {
7879
showDeleteFolderAlert = true
7980
}
8081
)
82+
.tag(item.id)
8183
}
8284
}
8385
.listStyle(.sidebar)
8486
.scrollContentBackground(.hidden)
87+
.onDeleteCommand {
88+
deleteSelectedFavorites()
89+
}
90+
.contextMenu {
91+
if !selectedFavoriteIds.isEmpty {
92+
Button(role: .destructive) {
93+
deleteSelectedFavorites()
94+
} label: {
95+
Label(String(localized: "Delete Selected"), systemImage: "trash")
96+
}
97+
}
98+
}
99+
}
100+
101+
private func deleteSelectedFavorites() {
102+
let allFavorites = collectFavorites(from: viewModel.treeItems)
103+
let toDelete = allFavorites.filter { selectedFavoriteIds.contains("fav-\($0.id)") }
104+
for fav in toDelete {
105+
viewModel.deleteFavorite(fav)
106+
}
107+
selectedFavoriteIds.removeAll()
108+
}
109+
110+
private func collectFavorites(from items: [FavoriteTreeItem]) -> [SQLFavorite] {
111+
var result: [SQLFavorite] = []
112+
for item in items {
113+
switch item {
114+
case .favorite(let fav):
115+
result.append(fav)
116+
case .folder(_, let children):
117+
result.append(contentsOf: collectFavorites(from: children))
118+
}
119+
}
120+
return result
85121
}
86122

87123
// MARK: - Empty States

0 commit comments

Comments
 (0)