b442c13719
- Add default values to all SwiftData attributes (CloudKit requirement) - Make memos relationship optional (CloudKit requirement) - Add allMemos computed property for safe unwrapping - Add remote-notification background mode for CloudKit push sync Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
43 lines
1.1 KiB
Swift
43 lines
1.1 KiB
Swift
import Foundation
|
|
import SwiftData
|
|
|
|
@Observable
|
|
@MainActor
|
|
final class DiaryViewModel {
|
|
var error: Error?
|
|
|
|
private let summarizationService = SummarizationService()
|
|
|
|
var isSummarizing: Bool {
|
|
summarizationService.isSummarizing
|
|
}
|
|
|
|
func generateSummary(for entry: DiaryEntry) async {
|
|
do {
|
|
let transcript = entry.combinedTranscript
|
|
let date = entry.date
|
|
let summary = try await summarizationService.generateSummary(transcript: transcript, date: date)
|
|
entry.summary = summary
|
|
entry.isSummaryGenerated = true
|
|
entry.updatedAt = .now
|
|
} catch {
|
|
self.error = error
|
|
}
|
|
}
|
|
|
|
func deleteEntry(_ entry: DiaryEntry, context: ModelContext) {
|
|
// Delete audio files
|
|
for memo in entry.allMemos {
|
|
try? FileManager.default.removeItem(at: memo.audioURL)
|
|
}
|
|
context.delete(entry)
|
|
}
|
|
|
|
func deleteMemo(_ memo: VoiceMemo, from entry: DiaryEntry, context: ModelContext) {
|
|
try? FileManager.default.removeItem(at: memo.audioURL)
|
|
entry.memos?.removeAll { $0.persistentModelID == memo.persistentModelID }
|
|
context.delete(memo)
|
|
entry.updatedAt = .now
|
|
}
|
|
}
|