74 lines
2.5 KiB
Swift
74 lines
2.5 KiB
Swift
import Foundation
|
|
import SwiftData
|
|
|
|
class LastTranscriptionService: ObservableObject {
|
|
|
|
static func getLastTranscription(from modelContext: ModelContext) -> Transcription? {
|
|
var descriptor = FetchDescriptor<Transcription>(
|
|
sortBy: [SortDescriptor(\.timestamp, order: .reverse)]
|
|
)
|
|
descriptor.fetchLimit = 1
|
|
|
|
do {
|
|
let transcriptions = try modelContext.fetch(descriptor)
|
|
return transcriptions.first
|
|
} catch {
|
|
print("Error fetching last transcription: \(error)")
|
|
return nil
|
|
}
|
|
}
|
|
|
|
static func copyLastTranscription(from modelContext: ModelContext) {
|
|
guard let lastTranscription = getLastTranscription(from: modelContext) else {
|
|
Task { @MainActor in
|
|
NotificationManager.shared.showNotification(
|
|
title: "No transcription available",
|
|
type: .error
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
let success = ClipboardManager.copyToClipboard(lastTranscription.text)
|
|
|
|
Task { @MainActor in
|
|
if success {
|
|
NotificationManager.shared.showNotification(
|
|
title: "Last transcription copied",
|
|
type: .success
|
|
)
|
|
} else {
|
|
NotificationManager.shared.showNotification(
|
|
title: "Failed to copy transcription",
|
|
type: .error
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
static func pasteLastTranscription(from modelContext: ModelContext) {
|
|
guard let lastTranscription = getLastTranscription(from: modelContext) else {
|
|
Task { @MainActor in
|
|
NotificationManager.shared.showNotification(
|
|
title: "No transcription available",
|
|
type: .error
|
|
)
|
|
}
|
|
return
|
|
}
|
|
|
|
// Use enhanced text if available and not empty, otherwise use original text
|
|
let textToPaste: String
|
|
if let enhancedText = lastTranscription.enhancedText, !enhancedText.isEmpty {
|
|
textToPaste = enhancedText
|
|
} else {
|
|
textToPaste = lastTranscription.text
|
|
}
|
|
|
|
// Delay to give the user time to release modifier keys (especially Control)
|
|
DispatchQueue.main.asyncAfter(deadline: .now() + 0.15) {
|
|
CursorPaster.pasteAtCursor(textToPaste + " ", shouldPreserveClipboard: true)
|
|
}
|
|
|
|
}
|
|
} |