|
| 1 | +// |
| 2 | +// SQLFavoriteManager.swift |
| 3 | +// TablePro |
| 4 | +// |
| 5 | + |
| 6 | +import Foundation |
| 7 | +import os |
| 8 | + |
| 9 | +/// Manages SQL favorites with notifications and sync tracking |
| 10 | +final class SQLFavoriteManager { |
| 11 | + static let shared = SQLFavoriteManager() |
| 12 | + private static let logger = Logger(subsystem: "com.TablePro", category: "SQLFavoriteManager") |
| 13 | + |
| 14 | + private let storage: SQLFavoriteStorage |
| 15 | + |
| 16 | + /// Creates an isolated manager with its own storage. For testing only. |
| 17 | + init(isolatedStorage: SQLFavoriteStorage) { |
| 18 | + self.storage = isolatedStorage |
| 19 | + } |
| 20 | + |
| 21 | + private init() { |
| 22 | + self.storage = SQLFavoriteStorage.shared |
| 23 | + } |
| 24 | + |
| 25 | + // MARK: - Favorites |
| 26 | + |
| 27 | + func addFavorite(_ favorite: SQLFavorite) async -> Bool { |
| 28 | + let result = await storage.addFavorite(favorite) |
| 29 | + if result { |
| 30 | + SyncChangeTracker.shared.markDirty(.favorite, id: favorite.id.uuidString) |
| 31 | + postUpdateNotification() |
| 32 | + } |
| 33 | + return result |
| 34 | + } |
| 35 | + |
| 36 | + func updateFavorite(_ favorite: SQLFavorite) async -> Bool { |
| 37 | + let result = await storage.updateFavorite(favorite) |
| 38 | + if result { |
| 39 | + SyncChangeTracker.shared.markDirty(.favorite, id: favorite.id.uuidString) |
| 40 | + postUpdateNotification() |
| 41 | + } |
| 42 | + return result |
| 43 | + } |
| 44 | + |
| 45 | + func deleteFavorite(id: UUID) async -> Bool { |
| 46 | + let result = await storage.deleteFavorite(id: id) |
| 47 | + if result { |
| 48 | + SyncChangeTracker.shared.markDeleted(.favorite, id: id.uuidString) |
| 49 | + postUpdateNotification() |
| 50 | + } |
| 51 | + return result |
| 52 | + } |
| 53 | + |
| 54 | + func fetchFavorites( |
| 55 | + connectionId: UUID? = nil, |
| 56 | + folderId: UUID? = nil, |
| 57 | + searchText: String? = nil |
| 58 | + ) async -> [SQLFavorite] { |
| 59 | + await storage.fetchFavorites(connectionId: connectionId, folderId: folderId, searchText: searchText) |
| 60 | + } |
| 61 | + |
| 62 | + // MARK: - Folders |
| 63 | + |
| 64 | + func addFolder(_ folder: SQLFavoriteFolder) async -> Bool { |
| 65 | + let result = await storage.addFolder(folder) |
| 66 | + if result { |
| 67 | + SyncChangeTracker.shared.markDirty(.favoriteFolder, id: folder.id.uuidString) |
| 68 | + postUpdateNotification() |
| 69 | + } |
| 70 | + return result |
| 71 | + } |
| 72 | + |
| 73 | + func updateFolder(_ folder: SQLFavoriteFolder) async -> Bool { |
| 74 | + let result = await storage.updateFolder(folder) |
| 75 | + if result { |
| 76 | + SyncChangeTracker.shared.markDirty(.favoriteFolder, id: folder.id.uuidString) |
| 77 | + postUpdateNotification() |
| 78 | + } |
| 79 | + return result |
| 80 | + } |
| 81 | + |
| 82 | + func deleteFolder(id: UUID) async -> Bool { |
| 83 | + let result = await storage.deleteFolder(id: id) |
| 84 | + if result { |
| 85 | + SyncChangeTracker.shared.markDeleted(.favoriteFolder, id: id.uuidString) |
| 86 | + postUpdateNotification() |
| 87 | + } |
| 88 | + return result |
| 89 | + } |
| 90 | + |
| 91 | + func fetchFolders(connectionId: UUID? = nil) async -> [SQLFavoriteFolder] { |
| 92 | + await storage.fetchFolders(connectionId: connectionId) |
| 93 | + } |
| 94 | + |
| 95 | + // MARK: - Keyword Support |
| 96 | + |
| 97 | + func fetchKeywordMap(connectionId: UUID? = nil) async -> [String: (name: String, query: String)] { |
| 98 | + await storage.fetchKeywordMap(connectionId: connectionId) |
| 99 | + } |
| 100 | + |
| 101 | + func isKeywordAvailable( |
| 102 | + _ keyword: String, |
| 103 | + connectionId: UUID?, |
| 104 | + excludingFavoriteId: UUID? = nil |
| 105 | + ) async -> Bool { |
| 106 | + await storage.isKeywordAvailable(keyword, connectionId: connectionId, excludingFavoriteId: excludingFavoriteId) |
| 107 | + } |
| 108 | + |
| 109 | + // MARK: - Notifications |
| 110 | + |
| 111 | + private func postUpdateNotification() { |
| 112 | + DispatchQueue.main.async { |
| 113 | + NotificationCenter.default.post(name: .sqlFavoritesDidUpdate, object: nil) |
| 114 | + } |
| 115 | + } |
| 116 | +} |
0 commit comments