File size: 1,189 Bytes
7b2dfc5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
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
    )
  }
}