|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// Handles communication between the main VoiceInk app and the keyboard extension |
| 4 | +/// Uses App Groups + Darwin Notifications for reliable iOS-native communication |
| 5 | +final class AppGroupCoordinator { |
| 6 | + static let shared = AppGroupCoordinator() |
| 7 | + |
| 8 | + // MARK: - Constants |
| 9 | + private let appGroupIdentifier = "group.com.prakashjoshipax.VoiceInk" |
| 10 | + |
| 11 | + // UserDefaults keys for persistent state |
| 12 | + private enum UserDefaultsKeys { |
| 13 | + static let shouldStartRecording = "shouldStartRecording" |
| 14 | + static let shouldStopRecording = "shouldStopRecording" |
| 15 | + static let isRecording = "isRecording" |
| 16 | + static let lastRecordingTimestamp = "lastRecordingTimestamp" |
| 17 | + } |
| 18 | + |
| 19 | + // Darwin notification names for real-time communication |
| 20 | + private enum NotificationNames { |
| 21 | + static let startRecording = "com.prakashjoshipax.VoiceInk.startRecording" |
| 22 | + static let stopRecording = "com.prakashjoshipax.VoiceInk.stopRecording" |
| 23 | + static let recordingStateChanged = "com.prakashjoshipax.VoiceInk.recordingStateChanged" |
| 24 | + } |
| 25 | + |
| 26 | + // MARK: - Properties |
| 27 | + private let sharedDefaults: UserDefaults? |
| 28 | + private let notificationCenter = CFNotificationCenterGetDarwinNotifyCenter() |
| 29 | + |
| 30 | + // Callbacks for the main app |
| 31 | + var onStartRecordingRequested: (() -> Void)? |
| 32 | + var onStopRecordingRequested: (() -> Void)? |
| 33 | + |
| 34 | + // MARK: - Initialization |
| 35 | + private init() { |
| 36 | + sharedDefaults = UserDefaults(suiteName: appGroupIdentifier) |
| 37 | + setupNotificationObservers() |
| 38 | + } |
| 39 | + |
| 40 | + deinit { |
| 41 | + removeNotificationObservers() |
| 42 | + } |
| 43 | + |
| 44 | + // MARK: - Public Interface for Keyboard Extension |
| 45 | + |
| 46 | + /// Call this from the keyboard extension to request recording start |
| 47 | + func requestStartRecording() { |
| 48 | + let timestamp = Date().timeIntervalSince1970 |
| 49 | + |
| 50 | + // Set persistent flag |
| 51 | + sharedDefaults?.set(true, forKey: UserDefaultsKeys.shouldStartRecording) |
| 52 | + sharedDefaults?.set(timestamp, forKey: UserDefaultsKeys.lastRecordingTimestamp) |
| 53 | + |
| 54 | + // Send immediate notification |
| 55 | + postDarwinNotification(NotificationNames.startRecording) |
| 56 | + } |
| 57 | + |
| 58 | + /// Call this from the keyboard extension to request recording stop |
| 59 | + func requestStopRecording() { |
| 60 | + let timestamp = Date().timeIntervalSince1970 |
| 61 | + |
| 62 | + // Set persistent flag |
| 63 | + sharedDefaults?.set(true, forKey: UserDefaultsKeys.shouldStopRecording) |
| 64 | + sharedDefaults?.set(timestamp, forKey: UserDefaultsKeys.lastRecordingTimestamp) |
| 65 | + |
| 66 | + // Send immediate notification |
| 67 | + postDarwinNotification(NotificationNames.stopRecording) |
| 68 | + } |
| 69 | + |
| 70 | + /// Get current recording state (for keyboard UI updates) |
| 71 | + var isRecording: Bool { |
| 72 | + return sharedDefaults?.bool(forKey: UserDefaultsKeys.isRecording) ?? false |
| 73 | + } |
| 74 | + |
| 75 | + // MARK: - Public Interface for Main App |
| 76 | + |
| 77 | + /// Call this from the main app to update recording state |
| 78 | + func updateRecordingState(_ isRecording: Bool) { |
| 79 | + sharedDefaults?.set(isRecording, forKey: UserDefaultsKeys.isRecording) |
| 80 | + |
| 81 | + // Notify keyboard of state change |
| 82 | + postDarwinNotification(NotificationNames.recordingStateChanged) |
| 83 | + } |
| 84 | + |
| 85 | + /// Check and consume start recording flag (returns true if should start) |
| 86 | + func checkAndConsumeStartRecordingFlag() -> Bool { |
| 87 | + guard let defaults = sharedDefaults else { return false } |
| 88 | + |
| 89 | + let shouldStart = defaults.bool(forKey: UserDefaultsKeys.shouldStartRecording) |
| 90 | + if shouldStart { |
| 91 | + // Consume the flag |
| 92 | + defaults.set(false, forKey: UserDefaultsKeys.shouldStartRecording) |
| 93 | + return true |
| 94 | + } |
| 95 | + return false |
| 96 | + } |
| 97 | + |
| 98 | + /// Check and consume stop recording flag (returns true if should stop) |
| 99 | + func checkAndConsumeStopRecordingFlag() -> Bool { |
| 100 | + guard let defaults = sharedDefaults else { return false } |
| 101 | + |
| 102 | + let shouldStop = defaults.bool(forKey: UserDefaultsKeys.shouldStopRecording) |
| 103 | + if shouldStop { |
| 104 | + // Consume the flag |
| 105 | + defaults.set(false, forKey: UserDefaultsKeys.shouldStopRecording) |
| 106 | + return true |
| 107 | + } |
| 108 | + return false |
| 109 | + } |
| 110 | + |
| 111 | + // MARK: - Darwin Notifications (Real-time Communication) |
| 112 | + |
| 113 | + private func setupNotificationObservers() { |
| 114 | + guard let center = notificationCenter else { return } |
| 115 | + |
| 116 | + // Observe start recording notifications |
| 117 | + CFNotificationCenterAddObserver( |
| 118 | + center, |
| 119 | + Unmanaged.passUnretained(self).toOpaque(), |
| 120 | + { (center, observer, name, object, userInfo) in |
| 121 | + guard let observer = observer else { return } |
| 122 | + let coordinator = Unmanaged<AppGroupCoordinator>.fromOpaque(observer).takeUnretainedValue() |
| 123 | + coordinator.handleStartRecordingNotification() |
| 124 | + }, |
| 125 | + NotificationNames.startRecording as CFString, |
| 126 | + nil, |
| 127 | + .deliverImmediately |
| 128 | + ) |
| 129 | + |
| 130 | + // Observe stop recording notifications |
| 131 | + CFNotificationCenterAddObserver( |
| 132 | + center, |
| 133 | + Unmanaged.passUnretained(self).toOpaque(), |
| 134 | + { (center, observer, name, object, userInfo) in |
| 135 | + guard let observer = observer else { return } |
| 136 | + let coordinator = Unmanaged<AppGroupCoordinator>.fromOpaque(observer).takeUnretainedValue() |
| 137 | + coordinator.handleStopRecordingNotification() |
| 138 | + }, |
| 139 | + NotificationNames.stopRecording as CFString, |
| 140 | + nil, |
| 141 | + .deliverImmediately |
| 142 | + ) |
| 143 | + } |
| 144 | + |
| 145 | + private func removeNotificationObservers() { |
| 146 | + guard let center = notificationCenter else { return } |
| 147 | + CFNotificationCenterRemoveEveryObserver(center, Unmanaged.passUnretained(self).toOpaque()) |
| 148 | + } |
| 149 | + |
| 150 | + private func postDarwinNotification(_ name: String) { |
| 151 | + guard let center = notificationCenter else { return } |
| 152 | + CFNotificationCenterPostNotification( |
| 153 | + center, |
| 154 | + CFNotificationName(name as CFString), |
| 155 | + nil, |
| 156 | + nil, |
| 157 | + true |
| 158 | + ) |
| 159 | + } |
| 160 | + |
| 161 | + // MARK: - Notification Handlers |
| 162 | + |
| 163 | + private func handleStartRecordingNotification() { |
| 164 | + DispatchQueue.main.async { [weak self] in |
| 165 | + self?.onStartRecordingRequested?() |
| 166 | + } |
| 167 | + } |
| 168 | + |
| 169 | + private func handleStopRecordingNotification() { |
| 170 | + DispatchQueue.main.async { [weak self] in |
| 171 | + self?.onStopRecordingRequested?() |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + // MARK: - Debug Helpers |
| 176 | + |
| 177 | + /// Clear all shared data (useful for debugging) |
| 178 | + func clearAllSharedData() { |
| 179 | + guard let defaults = sharedDefaults else { return } |
| 180 | + defaults.removeObject(forKey: UserDefaultsKeys.shouldStartRecording) |
| 181 | + defaults.removeObject(forKey: UserDefaultsKeys.shouldStopRecording) |
| 182 | + defaults.removeObject(forKey: UserDefaultsKeys.isRecording) |
| 183 | + defaults.removeObject(forKey: UserDefaultsKeys.lastRecordingTimestamp) |
| 184 | + } |
| 185 | + |
| 186 | + /// Get debug info about current state |
| 187 | + func getDebugInfo() -> [String: Any] { |
| 188 | + guard let defaults = sharedDefaults else { return ["error": "No shared defaults"] } |
| 189 | + |
| 190 | + return [ |
| 191 | + "shouldStartRecording": defaults.bool(forKey: UserDefaultsKeys.shouldStartRecording), |
| 192 | + "shouldStopRecording": defaults.bool(forKey: UserDefaultsKeys.shouldStopRecording), |
| 193 | + "isRecording": defaults.bool(forKey: UserDefaultsKeys.isRecording), |
| 194 | + "lastRecordingTimestamp": defaults.double(forKey: UserDefaultsKeys.lastRecordingTimestamp), |
| 195 | + "appGroupIdentifier": appGroupIdentifier |
| 196 | + ] |
| 197 | + } |
| 198 | +} |
0 commit comments