feat: Update Power Mode session dynamically

This commit is contained in:
Beingpax 2025-08-05 21:45:11 +05:45
parent 1db6716539
commit b9fad57d3e
6 changed files with 33 additions and 1 deletions

View File

@ -0,0 +1,5 @@
import Foundation
extension Notification.Name {
static let AppSettingsDidChange = Notification.Name("appSettingsDidChange")
}

View File

@ -56,6 +56,8 @@ class PowerModeSessionManager {
originalState: originalState originalState: originalState
) )
saveSession(newSession) saveSession(newSession)
NotificationCenter.default.addObserver(self, selector: #selector(updateSessionSnapshot), name: .AppSettingsDidChange, object: nil)
await applyConfiguration(config) await applyConfiguration(config)
} }
@ -64,9 +66,28 @@ class PowerModeSessionManager {
guard let session = loadSession() else { return } guard let session = loadSession() else { return }
await restoreState(session.originalState) await restoreState(session.originalState)
NotificationCenter.default.removeObserver(self, name: .AppSettingsDidChange, object: nil)
clearSession() clearSession()
} }
@objc private func updateSessionSnapshot() {
guard var session = loadSession(), let whisperState = whisperState, let enhancementService = enhancementService else { return }
let updatedState = ApplicationState(
isEnhancementEnabled: enhancementService.isEnhancementEnabled,
useScreenCaptureContext: enhancementService.useScreenCaptureContext,
selectedPromptId: enhancementService.selectedPromptId?.uuidString,
selectedAIProvider: enhancementService.getAIService()?.selectedProvider.rawValue,
selectedAIModel: enhancementService.getAIService()?.currentModel,
selectedLanguage: UserDefaults.standard.string(forKey: "SelectedLanguage"),
transcriptionModelName: whisperState.currentTranscriptionModel?.name
)
session.originalState = updatedState
saveSession(session)
}
private func applyConfiguration(_ config: PowerModeConfig) async { private func applyConfiguration(_ config: PowerModeConfig) async {
guard let enhancementService = enhancementService else { return } guard let enhancementService = enhancementService else { return }

View File

@ -17,6 +17,7 @@ class AIEnhancementService: ObservableObject {
if isEnhancementEnabled && selectedPromptId == nil { if isEnhancementEnabled && selectedPromptId == nil {
selectedPromptId = customPrompts.first?.id selectedPromptId = customPrompts.first?.id
} }
NotificationCenter.default.post(name: .AppSettingsDidChange, object: nil)
} }
} }
@ -29,6 +30,7 @@ class AIEnhancementService: ObservableObject {
@Published var useScreenCaptureContext: Bool { @Published var useScreenCaptureContext: Bool {
didSet { didSet {
UserDefaults.standard.set(useScreenCaptureContext, forKey: "useScreenCaptureContext") UserDefaults.standard.set(useScreenCaptureContext, forKey: "useScreenCaptureContext")
NotificationCenter.default.post(name: .AppSettingsDidChange, object: nil)
} }
} }
@ -43,6 +45,7 @@ class AIEnhancementService: ObservableObject {
@Published var selectedPromptId: UUID? { @Published var selectedPromptId: UUID? {
didSet { didSet {
UserDefaults.standard.set(selectedPromptId?.uuidString, forKey: "selectedPromptId") UserDefaults.standard.set(selectedPromptId?.uuidString, forKey: "selectedPromptId")
NotificationCenter.default.post(name: .AppSettingsDidChange, object: nil)
} }
} }

View File

@ -171,6 +171,7 @@ class AIService: ObservableObject {
} }
} }
} }
NotificationCenter.default.post(name: .AppSettingsDidChange, object: nil)
} }
} }
@ -261,6 +262,7 @@ class AIService: ObservableObject {
} }
objectWillChange.send() objectWillChange.send()
NotificationCenter.default.post(name: .AppSettingsDidChange, object: nil)
} }
func saveAPIKey(_ key: String, completion: @escaping (Bool) -> Void) { func saveAPIKey(_ key: String, completion: @escaping (Bool) -> Void) {

View File

@ -22,6 +22,7 @@ struct LanguageSelectionView: View {
// Post notification for language change // Post notification for language change
NotificationCenter.default.post(name: .languageDidChange, object: nil) NotificationCenter.default.post(name: .languageDidChange, object: nil)
NotificationCenter.default.post(name: .AppSettingsDidChange, object: nil)
} }
// Function to check if current model is multilingual // Function to check if current model is multilingual

View File

@ -25,9 +25,9 @@ extension WhisperState {
if model.provider != .local { if model.provider != .local {
self.isModelLoaded = true self.isModelLoaded = true
} }
// Post notification about the model change // Post notification about the model change
NotificationCenter.default.post(name: .didChangeModel, object: nil, userInfo: ["modelName": model.name]) NotificationCenter.default.post(name: .didChangeModel, object: nil, userInfo: ["modelName": model.name])
NotificationCenter.default.post(name: .AppSettingsDidChange, object: nil)
} }
func refreshAllAvailableModels() { func refreshAllAvailableModels() {