Remove dictionary item dependency on local Whisper model

This commit is contained in:
Beingpax 2025-08-23 22:15:13 +05:45
parent abe12d0dfe
commit 73bb3e765a
3 changed files with 4 additions and 46 deletions

View File

@ -198,8 +198,8 @@ class ImportExportService {
} }
if let itemsToImport = importedSettings.dictionaryItems { if let itemsToImport = importedSettings.dictionaryItems {
Task { if let encoded = try? JSONEncoder().encode(itemsToImport) {
await whisperPrompt.saveDictionaryItems(itemsToImport) UserDefaults.standard.set(encoded, forKey: "CustomDictionaryItems")
} }
} else { } else {
print("No dictionary items (for spelling) found in the imported file. Existing items remain unchanged.") print("No dictionary items (for spelling) found in the imported file. Existing items remain unchanged.")

View File

@ -29,20 +29,12 @@ class DictionaryManager: ObservableObject {
if let savedItems = try? JSONDecoder().decode([DictionaryItem].self, from: data) { if let savedItems = try? JSONDecoder().decode([DictionaryItem].self, from: data) {
items = savedItems.sorted(by: { $0.dateAdded > $1.dateAdded }) items = savedItems.sorted(by: { $0.dateAdded > $1.dateAdded })
updatePrompt()
} }
} }
private func saveItems() { private func saveItems() {
if let encoded = try? JSONEncoder().encode(items) { if let encoded = try? JSONEncoder().encode(items) {
UserDefaults.standard.set(encoded, forKey: saveKey) UserDefaults.standard.set(encoded, forKey: saveKey)
updatePrompt()
}
}
private func updatePrompt() {
Task { @MainActor in
await whisperPrompt.saveDictionaryItems(items)
} }
} }

View File

@ -5,8 +5,6 @@ import Foundation
class WhisperPrompt: ObservableObject { class WhisperPrompt: ObservableObject {
@Published var transcriptionPrompt: String = UserDefaults.standard.string(forKey: "TranscriptionPrompt") ?? "" @Published var transcriptionPrompt: String = UserDefaults.standard.string(forKey: "TranscriptionPrompt") ?? ""
private var dictionaryWords: [String] = []
private let saveKey = "CustomDictionaryItems"
private let customPromptsKey = "CustomLanguagePrompts" private let customPromptsKey = "CustomLanguagePrompts"
// Store user-customized prompts // Store user-customized prompts
@ -55,7 +53,6 @@ class WhisperPrompt: ObservableObject {
] ]
init() { init() {
loadDictionaryItems()
loadCustomPrompts() loadCustomPrompts()
updateTranscriptionPrompt() updateTranscriptionPrompt()
@ -76,16 +73,6 @@ class WhisperPrompt: ObservableObject {
updateTranscriptionPrompt() updateTranscriptionPrompt()
} }
private func loadDictionaryItems() {
guard let data = UserDefaults.standard.data(forKey: saveKey) else { return }
if let savedItems = try? JSONDecoder().decode([DictionaryItem].self, from: data) {
let enabledWords = savedItems.filter { $0.isEnabled }.map { $0.word }
dictionaryWords = enabledWords
updateTranscriptionPrompt()
}
}
private func loadCustomPrompts() { private func loadCustomPrompts() {
if let savedPrompts = UserDefaults.standard.dictionary(forKey: customPromptsKey) as? [String: String] { if let savedPrompts = UserDefaults.standard.dictionary(forKey: customPromptsKey) as? [String: String] {
customPrompts = savedPrompts customPrompts = savedPrompts
@ -97,25 +84,13 @@ class WhisperPrompt: ObservableObject {
UserDefaults.standard.synchronize() // Force immediate synchronization UserDefaults.standard.synchronize() // Force immediate synchronization
} }
func updateDictionaryWords(_ words: [String]) {
dictionaryWords = words
updateTranscriptionPrompt()
}
func updateTranscriptionPrompt() { func updateTranscriptionPrompt() {
// Get the currently selected language from UserDefaults // Get the currently selected language from UserDefaults
let selectedLanguage = UserDefaults.standard.string(forKey: "SelectedLanguage") ?? "en" let selectedLanguage = UserDefaults.standard.string(forKey: "SelectedLanguage") ?? "en"
// Get the prompt for the selected language (custom if available, otherwise default) // Get the prompt for the selected language (custom if available, otherwise default)
let basePrompt = getLanguagePrompt(for: selectedLanguage) let basePrompt = getLanguagePrompt(for: selectedLanguage)
let prompt = basePrompt.isEmpty ? "" : basePrompt
// Always include VoiceInk in the prompt
var prompt = basePrompt + "\nVoiceInk, "
// Add dictionary words if available
if !dictionaryWords.isEmpty {
prompt += dictionaryWords.joined(separator: ", ")
}
transcriptionPrompt = prompt transcriptionPrompt = prompt
UserDefaults.standard.set(prompt, forKey: "TranscriptionPrompt") UserDefaults.standard.set(prompt, forKey: "TranscriptionPrompt")
@ -143,13 +118,4 @@ class WhisperPrompt: ObservableObject {
// Force update the UI // Force update the UI
objectWillChange.send() objectWillChange.send()
} }
}
func saveDictionaryItems(_ items: [DictionaryItem]) async {
if let encoded = try? JSONEncoder().encode(items) {
UserDefaults.standard.set(encoded, forKey: saveKey)
let enabledWords = items.filter { $0.isEnabled }.map { $0.word }
dictionaryWords = enabledWords
updateTranscriptionPrompt()
}
}
}