Dolphin3.0-CoreML / examples /ios /DolphinCoreMLTestApp /Sources /DolphinCoreMLTestApp.swift
ales27pm's picture
Add autonomous on-device iOS assistant
7b2dfc5 verified
Raw
History Blame Contribute Delete
1.19 kB
import SwiftUI
@main
@MainActor
struct DolphinCoreMLTestApp: App {
@Environment(\.scenePhase) private var scenePhase
@State private var session = AssistantSession()
var body: some Scene {
WindowGroup {
AssistantAppView(session: session)
.task {
consumeIntentHandoff()
}
.onReceive(
NotificationCenter.default.publisher(
for: .dolphinIntentHandoffAvailable
)
) { _ in
consumeIntentHandoff()
}
.onChange(of: scenePhase) { _, phase in
if phase == .active {
session.enteredForeground()
consumeIntentHandoff()
} else {
session.enteredBackground()
}
}
}
}
private func consumeIntentHandoff() {
guard let request = IntentHandoffStore.consumePendingRequest() else {
return
}
let existingDraft = session.draft.trimmingCharacters(
in: .whitespacesAndNewlines
)
session.draft = existingDraft.isEmpty
? request
: "\(existingDraft)\n\n\(request)"
NotificationCenter.default.post(
name: .dolphinIntentHandoffConsumed,
object: nil
)
}
}