Migrates vocabulary words and word replacements from UserDefaults to SwiftData for better data management and persistence. Changes: - Create VocabularyWord and WordReplacement SwiftData models - Add dual ModelConfiguration setup (default.store for transcripts, dictionary.store for dictionary data) - Implement DictionaryMigrationService for one-time UserDefaults→SwiftData migration - Rename "Correct Spellings" to "Vocabulary" for clearer terminology - Update all dictionary views to use @Query instead of manager classes - Update all services to fetch from SwiftData using FetchDescriptor - Enhance word replacement duplicate detection (now checks during add AND edit) - Update import/export services to work with SwiftData - Preserve all existing functionality with improved data integrity Technical details: - Separate store files: default.store (transcripts) + dictionary.store (vocabulary + replacements) - Migration flag: "HasMigratedDictionaryToSwiftData_v2" - All CRUD operations properly implemented with duplicate detection
31 lines
889 B
Swift
31 lines
889 B
Swift
import Foundation
|
|
import SwiftUI
|
|
import SwiftData
|
|
|
|
class CustomVocabularyService {
|
|
static let shared = CustomVocabularyService()
|
|
|
|
private init() {}
|
|
|
|
func getCustomVocabulary(from context: ModelContext) -> String {
|
|
guard let customWords = getCustomVocabularyWords(from: context), !customWords.isEmpty else {
|
|
return ""
|
|
}
|
|
|
|
let wordsText = customWords.joined(separator: ", ")
|
|
return "Important Vocabulary: \(wordsText)"
|
|
}
|
|
|
|
private func getCustomVocabularyWords(from context: ModelContext) -> [String]? {
|
|
let descriptor = FetchDescriptor<VocabularyWord>(sortBy: [SortDescriptor(\VocabularyWord.word)])
|
|
|
|
do {
|
|
let items = try context.fetch(descriptor)
|
|
let words = items.map { $0.word }
|
|
return words.isEmpty ? nil : words
|
|
} catch {
|
|
return nil
|
|
}
|
|
}
|
|
}
|