From a6310431679dd207ad85466169da615f47b80aff Mon Sep 17 00:00:00 2001 From: Beingpax Date: Sun, 28 Dec 2025 12:19:36 +0545 Subject: [PATCH] Add error handling for dictionary save operations --- .../Dictionary/EditReplacementSheet.swift | 9 +++++++-- .../Views/Dictionary/VocabularyView.swift | 16 ++++++++++++++-- .../Dictionary/WordReplacementView.swift | 19 +++++++++++++++---- 3 files changed, 36 insertions(+), 8 deletions(-) diff --git a/VoiceInk/Views/Dictionary/EditReplacementSheet.swift b/VoiceInk/Views/Dictionary/EditReplacementSheet.swift index 723b8c5..482c076 100644 --- a/VoiceInk/Views/Dictionary/EditReplacementSheet.swift +++ b/VoiceInk/Views/Dictionary/EditReplacementSheet.swift @@ -159,8 +159,13 @@ struct EditReplacementSheet: View { // Update the replacement replacement.originalText = newOriginal replacement.replacementText = newReplacement - try? modelContext.save() - dismiss() + do { + try modelContext.save() + dismiss() + } catch { + alertMessage = "Failed to save changes: \(error.localizedDescription)" + showAlert = true + } } } \ No newline at end of file diff --git a/VoiceInk/Views/Dictionary/VocabularyView.swift b/VoiceInk/Views/Dictionary/VocabularyView.swift index 5137f23..4ae3f9e 100644 --- a/VoiceInk/Views/Dictionary/VocabularyView.swift +++ b/VoiceInk/Views/Dictionary/VocabularyView.swift @@ -154,12 +154,24 @@ struct VocabularyView: View { let newWord = VocabularyWord(word: normalizedWord) modelContext.insert(newWord) - try? modelContext.save() + + do { + try modelContext.save() + } catch { + alertMessage = "Failed to add word: \(error.localizedDescription)" + showAlert = true + } } private func removeWord(_ word: VocabularyWord) { modelContext.delete(word) - try? modelContext.save() + + do { + try modelContext.save() + } catch { + alertMessage = "Failed to remove word: \(error.localizedDescription)" + showAlert = true + } } } diff --git a/VoiceInk/Views/Dictionary/WordReplacementView.swift b/VoiceInk/Views/Dictionary/WordReplacementView.swift index fc0ccf2..aa8aae6 100644 --- a/VoiceInk/Views/Dictionary/WordReplacementView.swift +++ b/VoiceInk/Views/Dictionary/WordReplacementView.swift @@ -221,15 +221,26 @@ struct WordReplacementView: View { // Add new replacement let newReplacement = WordReplacement(originalText: original, replacementText: replacement) modelContext.insert(newReplacement) - try? modelContext.save() - originalWord = "" - replacementWord = "" + do { + try modelContext.save() + originalWord = "" + replacementWord = "" + } catch { + alertMessage = "Failed to add replacement: \(error.localizedDescription)" + showAlert = true + } } private func removeReplacement(_ replacement: WordReplacement) { modelContext.delete(replacement) - try? modelContext.save() + + do { + try modelContext.save() + } catch { + alertMessage = "Failed to remove replacement: \(error.localizedDescription)" + showAlert = true + } } }