|
|
@@ -14,13 +14,16 @@ final class PunctuationCheckerViewModel: ObservableObject {
|
|
|
@Published private(set) var errorMessage: String?
|
|
|
|
|
|
private let punctuationCheckService: any PunctuationCheckServicing
|
|
|
+ private let historyStorage: HistoryStoring
|
|
|
private let textExtractionService: any TextExtracting
|
|
|
|
|
|
init(
|
|
|
punctuationCheckService: any PunctuationCheckServicing = PunctuationCheckService(),
|
|
|
+ historyStorage: HistoryStoring = HistoryStorageService.shared,
|
|
|
textExtractionService: any TextExtracting = TextExtractionService()
|
|
|
) {
|
|
|
self.punctuationCheckService = punctuationCheckService
|
|
|
+ self.historyStorage = historyStorage
|
|
|
self.textExtractionService = textExtractionService
|
|
|
}
|
|
|
|
|
|
@@ -95,6 +98,7 @@ final class PunctuationCheckerViewModel: ObservableObject {
|
|
|
correctedText = result.correctedText
|
|
|
issues = result.issues
|
|
|
hasResults = true
|
|
|
+ saveToHistory(text: content, issues: result.issues, correctedText: result.correctedText)
|
|
|
} catch {
|
|
|
errorMessage = error.localizedDescription
|
|
|
}
|
|
|
@@ -103,6 +107,37 @@ final class PunctuationCheckerViewModel: ObservableObject {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+ func restore(from entry: HistoryEntry) {
|
|
|
+ guard case .punctuationChecker(let data) = entry.payload else { return }
|
|
|
+
|
|
|
+ text = data.text
|
|
|
+ issues = data.issues.map {
|
|
|
+ GrammarIssue(original: $0.original, suggestion: $0.suggestion, explanation: $0.explanation)
|
|
|
+ }
|
|
|
+ correctedText = data.correctedText
|
|
|
+ hasResults = true
|
|
|
+ errorMessage = nil
|
|
|
+ }
|
|
|
+
|
|
|
+ private func saveToHistory(text: String, issues: [GrammarIssue], correctedText: String) {
|
|
|
+ let payload = HistoryPayload.punctuationChecker(
|
|
|
+ PunctuationCheckerHistoryData(
|
|
|
+ text: text,
|
|
|
+ issues: issues.map(\.stored),
|
|
|
+ correctedText: correctedText
|
|
|
+ )
|
|
|
+ )
|
|
|
+
|
|
|
+ let entry = HistoryEntry(
|
|
|
+ tool: .punctuationChecker,
|
|
|
+ title: HistoryHelpers.title(from: text, fallback: "Punctuation Check"),
|
|
|
+ preview: HistoryHelpers.preview(from: correctedText.isEmpty ? text : correctedText),
|
|
|
+ payload: payload
|
|
|
+ )
|
|
|
+
|
|
|
+ historyStorage.save(entry)
|
|
|
+ }
|
|
|
+
|
|
|
func copyCorrectedText() {
|
|
|
guard !correctedText.isEmpty else { return }
|
|
|
NSPasteboard.general.clearContents()
|